In the classic game of Asteroids, there is a little ship that flies around in space, very far away from stars and planets so that the gravitational acceleration is zero.
The code is designed to solve the (rather simple) kinematic equations in this case. If we just consider the x-direction forces the equations look like this: $$\sum F_{\rm{net},x} = F_{\rm thrust} = m \, a_x$$ $$ \Delta v_x = a_x \Delta t$$ $$ v_x = v_{x0} + \Delta v_x$$ $$ x = x_0 + v_x \cdot \Delta t $$
The computer program we will work with here computes these equations over and over again, updating $v_x$ and $x$ depending on whether the thrust is turned on or off. Since the thrust is the only force, if it is turned off then $a_x = 0$ and $\Delta v_x = 0$ and the ship just continues with the same $v_x$ velocity. This is how the code works if the ship only moves in the $x$ direction. The equations are essentially the same for the $y$ direction. In the last step of this lab you will modify the code so that the ship can move in the $y$ direction too.
This is the code used to move the ship around in the 1D version. Take a close look at the code and try to get an idea of what each part does:
function draw() { // Update velocities vx += deltaVx; // Update location x += vx*dt; // velocity is unchanged if there are no forces deltaVx = 0; if (keyIsDown(LEFT_ARROW)) { theta += 0.0; } if (keyIsDown(RIGHT_ARROW)) { theta += 0.0; } if (keyIsDown(UP_ARROW)) { // Rockets on! accelx = Fthrust*cos(theta)/mass; deltaVx = accelx*dt; } if (keyIsDown(DOWN_ARROW)) { // Do nothing } if (keyIsPressed && key == ' '){ //spacebar // Do nothing! } // Draw ship and other stuff display(); } // end draw()
Click on this link to open the 1D planetoids code in a p5.js editor
Press play there to run the code. It should look the same as it did with the link you were given in Step 1
After you click duplicate! to make sure that you're working on your own version of the code, change these lines of code:
if (keyIsDown(LEFT_ARROW)) { theta += 0.0; } else if (keyIsDown(RIGHT_ARROW)) { theta += -0.0;to look like this:
if (keyIsDown(LEFT_ARROW)) { theta += 0.1; } else if (keyIsDown(RIGHT_ARROW)) { theta += -0.1;
The asteroids game is now set up so so that only the x-component of the thrust is what accelerates the ship. Notice that allowing the ship to rotate means that we can speed up and slow down the ship because we can rotate the rocket and apply a force that is opposite to the velocity vector. Our asteroids game is already a lot more fun! For reference, the game should now behave like this.
Now take the code you've written and modify it so that the ship can move in the y direction. It will take a few different changes to get it to work.
Look at the "Update location" section. Right now it looks like this:
// Update location x += vx*dt;
As you can see, only the x
position is updated. The y
position is not updated. Change the "Update location" section to this:
// Update location x += vx*dt; y += vy*dt;
If you did step 4a correctly, we can now update both the x position and the y position, but only if $v_x$ and $v_y$ are non-zero! This will only happen if we change the "Update velocities" section so that both vx
and vy
are updated.
Change this:
\\ Update velocities vx += deltaVx;
to this:
\\ Update velocities vx += deltaVx; vy += deltaVy;
Newton realized that objects move in a straight line with constant velocity if there are no forces acting on the object. For our computer program, we need to make sure that the velocity only changes when we push buttons on the keyboard. If we don't push any buttons then the change in velocity ($\Delta v_x$ and $\Delta v_y$) had better be zero.
This is the logic behind this part of the code:
// velocity is unchanged if there are no forces deltaVx = 0;
Change this to make sure deltaVy
is set to zero too:
// velocity is unchanged if there are no forces deltaVx = 0; deltaVy = 0;
This is an important step because if you do the next step correctly, but you forget to do this the ship will accelerate uncontrollably.
If you've done all the steps up to this point correctly, the ship still won't move in the y direction because we haven't told the ship how to accelerate in the y direction when the thrusters are turned on. Nowhere in the program do we set deltaVy
equal to anything except zero!
Look closely at this part of the code:
if (keyIsDown(UP_ARROW)) { // Rockets on! accelx = Fthrust*cos(theta)/mass; deltaVx = accelx*dt; }
You'll need to change this to something like this:
if (keyIsDown(UP_ARROW)) { // Rockets on! accelx = Fthrust*cos(theta)/mass; deltaVx = accelx*dt; accely = ???? deltaVy = accely*dt; }
It's your job to replace the ???? with something that actually gives the correct acceleration in the y direction. (Hint: it is very similar to the code for accelx
but the trig function might be different! Should you use cosine, sine or tangent for the trig function? If you're not sure just try one and see what happens!)
If you make all these changes your code should behave like this and your ship should be able to move in 2 dimensions!
Potential pitfall: Which trig function did you use to determine the y-component acceleration?
After display();
you can add this code and it will show the path of the ship:
for( i = 0; i < xhistory.length ; i+= 1) { drawPoint(xhistory[i],yhistory[i]); }
Change the ship so that it comes in from the left with an initial velocity. Modify the beginning of the code to this:
x = 0; y = 250; vx = 25; vy = 0;
and just for fun change the angle of the ship so that it points straight up:
theta = 3.141/2;
As you can see, the angle is being changed from 0 to $\pi/2$ radians. If converted to degrees this would be 90 degrees. This should make the ship point straight up (click here if curious about why).
Let the ship drift a little bit and then fire the thrusters. How does the trajectory (the curve) look?
Play around with the ship for 2-3 minutes! Change the initial velocity, position and angle of the ship. See what happens!
If your code behaves as expected, then take all your modifications to the 1D code and add them to this version of the code which includes planetoids!
Change the values for the mass
until you find the best combination to fly around and avoid the planetoids!
This is one of the easier challenges. Notice that your code has a section for when you press the down arrow:
if (keyIsDown(DOWN_ARROW)) { // Do nothing }
Where it says "Do nothing" how would you add code to fire the thrusters in reverse?
Hint: You want to accelerate in the opposite direction of the acceleration you get when the thrusters are turned on.
You can add another if statement to check to see if you've pressed the spacebar:
if (keyIsPressed && key == ' ') { // spacebar is pressed // Do nothing }
How would you modify the code so that the spacebar shoots a projectile from the nose of the ship? Add two new float variables called xprojectile and yprojectile to the beginning of the program and use the "drawPoint" function to show the position of the projectile:
drawPoint(xprojectile,yprojectile);
Advice: Place this function just after display(); so that it is drawn every time but set xprojectile and y projectile to zero initially so that the point is drawn at the top left corner of the grid and you can't see it. When the spacebar is pressed set the xprojectile and yprojectile equal to the position of the ship. That at least will put a dot on the screen where the ship is. How would you make it move?
0. Take the pre-survey
Make sure you take the pre-survey at the beginning of the activity in order to tell us about yourself and whether you'd like your responses to be used in the research project
1. Complete steps 1-4 and make absolutely sure that the ship can turn and move in two dimensions
Make sure your program behaves exactly like it does at this link to be sure you did steps 1-4 correctly
2. Add code to follow the path of the ship (Step 6)
Just copy the code to the clipboard and paste it into the code editor.
3. Complete one of the challenges
Most people choose the reverse thrusters, which is probably the easiest
4. Find the best value of the mass of the ship to survive!
This requires modifying a version of the code that includes planetoids
5. Submit your code before the deadline and take the post survey
Talk to your teacher about where to submit the code