HW08 - james-bern/CS345 GitHub Wiki

Starter Code
float timestep = 0.1;
float spaceship_mass = 2;
float gravitational_constant = 10;
float current_position = 256;
float current_velocity = 0;
float error = 0.0;
void setup() {
size(512, 512);
}
void draw() {
// // jim's setup
// origin at center of screen
// x-axis points right
// y-axis points up
scale(1, -1);
translate(width / 2, -height / 2);
float transformedMouseX = mouseX - width / 2;
float transformedMouseY = -mouseY + height / 2;
// // controller
float target_position = transformedMouseY;
// TODO: float proportional_gain = ???;
// TODO: float derivative_gain = ???;
// TODO: set thruster_force
float thruster_force = 0.0;
{
// TODO: error = ???;
}
// // actuator limits
// thruster_force = clamp(thruster_force, 0.0, 100.0);
if (thruster_force < 0.0) {
thruster_force = 0.0;
}
if (thruster_force > 100.0) {
thruster_force = 100.0;
}
// // physics
// semi-implicit euler
float current_acceleration = -gravitational_constant + thruster_force / spaceship_mass; // a = F / m
current_velocity += timestep * current_acceleration;
current_position += timestep * current_velocity;
// // draw
background(255);
fill(255);
line(-width / 2, 0.0, width / 2, 0.0);
line(-width / 8, target_position, width / 8, target_position);
circle(0, current_position, 16.0);
line(0.0, current_position, 0.0, current_position - thruster_force);
}