Part 2: Lunar Descent

If you have finished tinkering with the Planetoids lab you can continue with this lab where we will now add gravity to the game! You have a little ship that you must fly around and land safely on the moon. If you approach the landing too fast you will crash the ship.

You will need to bring your laptop to the lab or use one of the mac computers in Morril 285. Unfortunately, tablets will probably not work unless you have an external keyboard.

This example will use a programming language that is practically indistinguishable from C and C++ programming. (Note: If you are familiar with C or C++ the main difference you will see is that there is no main() function and instead the draw() function serves this role.)

The code is designed to solve the kinematic equations relevant to this case. $$\sum F_{\rm{net},x} = F_{\rm thrust} \cos \theta = m \, a_x$$ $$\sum F_{\rm{net},y} = F_{\rm thrust} \sin \theta - m g = m \, a_y$$ $$ \Delta v_x = a_x \Delta t$$ $$ \Delta v_y = a_y \Delta t$$ $$ v_x = v_{x0} + \Delta v_x$$ $$ v_y = v_{y0} + \Delta v_y$$ $$ x = x_0 + v_x \cdot \Delta t $$ $$ y = y_0 + v_y \cdot \Delta t $$

The computer program we will work with here computes these equations over and over again, updating $v_x$, $v_y$, $x$ and $y$ depending on whether the thrust is turned on or off. If the thrust is turned off then $a_x = 0$ and $\Delta v_x = 0$ and the ship just continues with the same $v_x$ velocity. With the thrust off the $v_y$ velocity will continue to change with time due to gravity, $\Delta v_y = g \, \Delta t$.

Step 0. Open up 2D planetoids in an editor

Click on this link to open the 2D planetoids code in a p5.js editor

Press play there to run the code. It should look the same as it did with the at the end of the planetoids exercise

Very Important: Log in to your account! Then click "Duplicate" so you can have your own version of the code!!!

Step 1. Add gravity to planetoids

After you click duplicate! edit the beginning of the code, add the gravitational constant for the moon. The moon's gravity is 1/6th of the earth's, so g = 9.8/6 = 1.63 m/s2

Add this line to near the beginning of your code:

g = 1.63;

Right before the display(); function, add this line:

deltaVy += -g*dt;

Note that gravity points in the $-y$ direction, so we need a minus sign in the above code to make sure down is negative (and since $g > 0$).

You've added gravity to our simple planetoids game! Now run the program by clicking . The program should behave like this.

Step 2. Add the Game Over!

Edit your code so that it's game over if the ship falls through the bottom of the page. Since down is positive, this happens if the y value becomes larger than the height of the screen.

Right after display(); add this line:

  if (y < 0) {
  drawText("Game Over!",width/2,height/2);
  noLoop();
  }

Great, now there is a way to lose the game! The program should now behave like this.

Step 3. Add a way to win the game!

We need to add a way for the ship to land on the surface of the moon.

Add this code right after the display(); function to draw a line at the bottom of the page:

drawLine(0,0,width,0);

If you play the game now there should be a black line on the bottom of the screen. You should test it to see if this worked.

Now we need a way for the ship to sit in the surface of the moon. In real life, the surface of the moon would provide a normal force to hold up the ship. We could try to code up the normal force, but a simpler thing to do would be to make it so that if the ship gets very close to the bottom then the acceleration and velocity will be zero in both the x and y direction. The ship will just sit there forever.

Add these line right before the game over in draw()

 if ( abs(y - 0.03*height)  < 0.1) {
  deltaVx = 0;
  deltaVy = 0;
  vx = 0;
  vy = 0;
  theta = PI/2;
  }

Note that the accelerations are set to zero, the velocities are set to zero and the angle of the rocket points straight up.

If you've coded up everything correctly, the end result should behave like this.

Step 4. Customize the game!

There are a number of different options to customize the game. You can change the initial direction of the ship from horizontal to vertical by changing one line of your code.

Near the beginning of your code change this line:

theta = 0; 
to this:
theta = Math.PI/2;

You may also want to have the ship start out higher up so it has further to fall. You can do this by changing initial y position in the setup() function. Change this from

y = height/2; 
to
y = height;

There are plenty of other ways to modify the game. Maybe have a limited amount of fuel, or a limited amount of time. Perhaps add mountains. Projectiles? Enable reverse thrusters? Perhaps display the numerical value of the velocity and height on screen. Something else?

How to get full credit on this programming lab!!!

1. Make sure you can lose the game and win the game

As described in steps 2 and 3

2. Make sure to change the initial angle

Make sure to change the initial angle to PI/2 as described in step 4. Other modifications (as discussed in step 4) are optional and you may get some extra credit if you modify the code in a clever way.