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

Bellicose Birds (similar to Angry Birds)

Developed by Chris Orban

As a continuation of the [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) game, the Bellicose Birds game further explores projectile motion in a simplified coding context. Just by changing a few lines of code from the [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) game, we can launch the ship/projectile instead of just watching it fall. 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 created by Prof. Chris Orban. Students should complete [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) before working on this exercise. After completing this exercise, the next in the series is [Planetoids with Momentum!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=248&A=PlanetoidsMomentum) and [Planetoids with torque!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=249&A=PlanetoidsTorque)
Subject Area Mechanics
Levels High School and First Year
Learning Objectives
Students will be able to: 1. Create an interactive model of projectile motion by modifying code from the completed Lunar Descent game 2. Understand how force, velocity and acceleration vectors relate to projectile motion
Time to Complete 60 min

Step 0. Open up Lunar Descent in an editor

We need to start where the [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) activity left off: [Click on this link to open the Lunar Descent code in a p5.js editor](http://alpha.editor.p5js.org/ChrisOrban/sketches/SJaBkH-1W) Press play there to run the code. It should look the same as it did [at the end of the lunar descent exercise](http://www.physics.ohio-state.edu/~orban/physics_coding/lunardescent_v3/lunardescent.html)

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

If you do not sign into your account, and if you do not click "Duplicate", then your changes to the code will NOT be saved.

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() {
  createCanvas(750,500);
  x = 0.5*width;
  y = 0.5*height;
}
to this:
function setup() {
  createCanvas(750,500);
  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 change the value of g from 1.67 to 0 at the beginning of the code where the variables are all initialized. Change this:
g = 1.67;
to this:
g = 0;
Now add g = 9.8 inside of the if statement for the spacebar (which begins on line 51). I'm talking about this if statement:
    if (keyIsPressed && key == ' '){ //spacebar
        // Do nothing!
    }
only instead of doing nothing, now it will set g = 9.8

Step 3: Remove old code from Lunar Descent

In order to prevent the rocket from sticking to the ground, remove this statement from [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) that we don't need any more:
  if ( abs(y - 0.03*height)  < 0.1) {
    deltaVx = 0;
    deltaVy = 0;
    vx = 0;
    vy = 0;
    theta = Math.PI/2;
  }
If you're curious about what this code does, look back to the [lunar descent game](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent) where it was introduced Once you remove this statement [the game should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v1/bellicosebirds.html)

Step 4. Give the rocket an initial velocity

Add a variable for the initial speed near the beginning of the code
vinit = 65;
Then add two lines after g = 9.8 to set the initial velocity in the x and y directions:
 
   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](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v2/bellicosebirds.html) 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 5: Show the expected trajectory

You should be able to calculate the trajectory of the rocket using kinematics equations that you've probably already used in worksheets or homeworks by this point. 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 game](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=245&A=Planetoids) 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:
  Npoints=1000;
  for(i=1;i<=Npoints;i+=1)
  {
  t = (i-1)*dt;
  xdraw = x0 + ?????;
  ydraw = y0 + ?????;
  drawPoint(xdraw,ydraw);
  }  
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). Note that we have to add a for loop here so we can show the position of the particle for ALL times t, not just for the particular time in the program that has elapsed thus far. Once you have figured this out [the program should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v3/bellicosebirds.html)

Step 6. Check that 45 degrees gives the longest distance

Add these lines somewhere after display() but not inside any of the if statements:
drawText(x,0.5*width,0.5*height + 20);
drawText(theta,0.5*width,0.5*height + 40);
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](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v4/bellicosebirds.html)

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. After display(); add this:
drawEllipse(x,y,27,20,vx,vy,deltaVx/dt,deltaVy/dt);
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 at the beginning of functions.js 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](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v5/bellicosebirds.html) Make sure to just tap the spacebar as briefly as you can!

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

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

1. Make sure the bird flies through the air like a projectile would (Steps 1-4) 2. Make sure that the drawn trajectory (dotted line) follows the path of the bird (Step 5) It doesn't need to be perfect, but it should be pretty close ( [like this](http://www.physics.ohio-state.edu/~orban/physics_coding/bellicosebirds_v3/bellicosebirds.html) ). 3. Make sure 45 degrees gives the longest distance (Step 6) Make sure to modify the code so that it writes the angle in degrees to the screen instead of radians In the comments of the code write down the exact angle (ex. 45.1274) and the distance the bird travelled before hitting the ground. Say something like "this was the furthest distance that the bird travelled 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

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