This Exercise Set has been submitted for peer review, but it has not yet been accepted for publication in the PICUP collection.

Lunar descent game (similar to Lunar Lander)

Developed by Chris Orban

As a continuation of the Planetoids Game, the Lunar Descent game adds gravity to the code. The result is something that closely resembles the classic game Lunar Lander. This example will use a programming language called [p5.js](http://p5js.org) that is very similar to 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.) **Importantly, this exercise can be completed using any computer or chromebook without downloading any software!** This exercise is designed for an algebra-based physics class at the college or high school level. It may also be useful for calculus-based physics for non-majors (e.g. engineering & science majors). This exercise is part of a series of exercises developed by Prof. Chris Orban. The next exercise is [Bellicose Birds (similar to angry birds)](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=247&A=BellicoseBirds) which involves predicting the trajectory of a projectile. Students should complete the [Planetoids game!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=245&A=Planetoids) before starting Lunar Descent. There are pre-and-post assessment questions associated with this exercise (not available here) that are being used in an educational research study. If interested to collaborate on that study please e-mail Prof. Chris Orban (orban@physics.osu.edu). The first paper from this study [is available at this link](https://arxiv.org/abs/1701.01867), the second paper which discusses the electromagnetism exercises [is available at this link](https://arxiv.org/abs/1707.00185)
Subject Area Mechanics
Levels High School and First Year
Learning Objectives
Students will be able to: 1. Create an interactive model of a ship falling towards the ground by modifying code from the completed Planetoids game 2. Understand how force, velocity and acceleration vectors relate to the motion of a ship falling towards the ground
Time to Complete 60 min

Step 0. Open up 2D planetoids in an editor

[Click on this link to open the 2D planetoids code in a p5.js editor](https://alpha.editor.p5js.org/ChrisOrban/sketches/HyLzq6g1Z) Press play there to run the code. It should look the same as it did with the [at the end of the Planetoids game](http://www.physics.ohio-state.edu/~orban/physics_coding/planetoids2d/planetoids.html)

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 play [the program should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/lunardescent_v1/lunardescent.html)

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](http://www.physics.ohio-state.edu/~orban/physics_coding/lunardescent_v2/lunardescent.html)

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 = Math.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](http://www.physics.ohio-state.edu/~orban/physics_coding/lunardescent_v3/lunardescent.html)

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.

Download Options

Credits and Licensing

The instructor materials are ©2017 Chris Orban.

The exercises are released under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license