Part 3: Bellicose birds!

If you have finished tinkering with the Lunar descent lab you can continue with this lab where we will configure the game to shoot a bird-shaped projectile. We call this game Bellicose Birds.

From a physics perspective, bellicose birds is essentially the same as lunar descent except that there is no rocket thrust. The kinematic equations look like this: $$\sum F_{\rm{net},x} = 0 = m \, a_x$$ $$\sum F_{\rm{net},y} = - 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 Lunar Descent in an editor

Click on this link to open the Lunar Descent 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 lunar descent exercise

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

Step 1. Change the starting point

After you click Duplicate, instead of starting the ship's position in the middle of the screen you'll want to configure it to start at the bottom left. Edit the code to change the setup() function from this:

function setup() {
  x = width/2;
  y = height/2;
to this:
function setup() {
  x = 0.2*width;
  y = 0.05*height;

The ship should now start at the bottom of the screen. Check to see that this works.

Step 2: Configure the game so that gravity is turned off until you press spacebar

The easiest way to do this is to set g = 0 near the beginning of the code where the variables are all initialized. Then set g = 9.8 inside of the if statement for the spacebar.

Finally, in order to prevent the rocket from sticking to the ground, remove this statement:

  if ( abs(y - 0.03*height)  < 0.1) {
    deltaVx = 0;
    deltaVy = 0;
    vx = 0;
    vy = 0;
    theta = PI/2;
Once you do this the game should behave like this.

Step 3. Give the rocket an initial velocity

Add a variable for the initial velocity near the beginning of the code

vinit = 65;

Then add two lines after g = 9.8 to set the initial velocity:

   if (keyIsPressed && key == ' ') { // spacebar
      g = 9.8;
      vx = ?????;
      vy = ?????;

It is up to you to figure out what goes in the question marks (think: trigonometry). When finished the game should behave like this

Note: For a more authentic bellicose birds experience you can turn off the thrusters by setting Fthrust = 0.0; at the top of the page.

Step 4: Show the expected trajectory

You should be able to calculate the trajectory of the rocket from the kinematics equations we used towards the beginning of the course. In this step you will calculate this trajectory and draw it on the screen using drawPoint(xdraw,ydraw); You may recognize this as the same function we used to do the projectile in the planetoids lab.

Just after drawLine(0,0,width,0); add these variables which are the initial x and y position of the rocket:

x0 = 0.2*width;
y0 = 0.05*height;

Immediately after this write:

  t = (i-1)*dt;
  xdraw = x0 + ?????;
  ydraw = y0 + ?????;

Fill in the ???? with the terms that give the right trajectory. These will of course depend on t the time variable. Note that to square a variable in this context simply multiply it by itself (for example: t*t). Expressions like t^2 won't work in this case (or in any other C/C++ context).

Once you have figured this out the program should behave like this

Step 5. Check that 45 degrees gives the longest distance

Add these lines after the "Game Over" but before the noLoop()

drawText(x,width/2,height/2 + 20);
drawText(theta,width/2,height/2 + 40);

This will show two pieces of information when the rocket falls below the page. The first line tells you the x value where the rocket landed. The second line gives the angle in radians.

Modify the code to write the angle in degrees instead of radians! Then check to see that 45 degrees gives the farthest distance. (Helpful hint: change the line where the angle of the ship changes so that it doesn't rotate so much every time you press the arrow.)

When finished, the game should behave like this

Final step

Chose one or both of these two options: (1) Replacing the ship with an bellicose bird, or (2) configure the game so that pressing the spacebar only works the first time.

Note that Option 1 is easier because there is a step-by-step guide.

How to replace the ship with an bellicose bird

Make the lab more fun by replacing the rocket with an bellicose bird. The easiest way to do this is with a red ellipse. Add this after display(); add this:


The problem is that the oval is drawn on top of the ship. We can fix this by making the size of the ship zero. Look below the "DO NOT EDIT THIS EVER" line and change this:

//Size of the ship
r = 12;
to this:
//Size of the ship
r = 0;

Once you have made these changes, the game should behave like this. Make sure to just tap the spacebar as briefly as you can!

Challenge: Step 6: Figure out a way so that pressing the spacebar only works the first time

You may notice that the rocket/bird only follows the trajectory if you tap the spacebar as briefly as you can. Configure the game so that pressing the spacebar only works the first time.

Once you have made this change, the game should behave like this

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

1. Make sure the bird flies through the air like a projectile would (Steps 1-3)

2. Make sure that the drawn trajectory (dotted line) follows the path of the bird (Step 4)

It doesn't need to be perfect, but it should be pretty close (like this).

3. Make sure 45 degrees gives the longest distance (Step 5)

Make sure to modify the code so that it writes the angle in degrees to the screen instead of radians

In the comments on the dropbox in carmen write down the exact angle (ex. 45.1274) and the distance the bird traveled before hitting the ground. Say something like "this was the furthest distance that the bird traveled for any angle".

4. Make sure to replace the ship with a picture of a bellicose bird (Final Step)

There are step by step directions for how to do this. Just copy and paste into your code.

5. If you want extra credit figure out how to make the spacebar only work the first time as discussed in step 6