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. Copy planetoids files

Create a folder called lunardescent on your computer. We will start where the planetoids game left off.

For your convenience I have taken planetoids.pde from the planetoids exercised and changed the name of the file to lunardescent.pde to provide a starting point for the programming lab today. Download both lunardescent.pde and functions.pde and place them both in the folder called lunardescent.

Now go into the lunardescent folder and double click on the lunardescent file to see if it works. The Processing Development Environment should open up and the program should behave exactly like the planetoids game from the previous exercise.

Step 1. Add gravity to planetoids

Edit the beginning of lunardescent.pde to 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 lunardescent.pde

float g = 1.63;

Now, close to the very end of lunardescent.pde and 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 lunardescent.pde 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.

In lunardescent.pde 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 a line in lunardescent.pde.

In lunardescent.pde change this line

float theta = 0; 
to this:
float theta = 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. Complete steps 1-3 and submit your code to the dropbox on carmen

Just submit your lunardescent.pde file on carmen.

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

As described in step 2 and 3

3. 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.

Optional: Share your program!

If you want to share your program with a friend, send them three files: Your lunardescent.pde, a copy of functions.pde and right-click and download the file lunardescent.html. As long as these three files are in the same folder on your or someone else's computer you should be able to double click on lunardescent.html and play the game on a web browser. You don't need to install the processing interactive environment to get this to work.

If this seems overly complicated the other way you can share your program is by clicking "Create sketch" at openprocessing.org. Upload your code, set up an account (for free!) and then give your friends a link to the sketch.