+
Planetoids with Momentum!

Developed by Christopher Orban - Published May 7, 2017

As a continuation of the Planetoids game this exercise lets students fly a ship in free space into a "blob" which sticks to the ship in a perfectly inelastic collision. 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 larger set of exercises developed by Prof. Chris Orban. The next exercises in this series is [Planetoids with Torque!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=249&A=PlanetoidsTorque) and [Planetoids with a Spring!](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=251&A=PlanetoidsSpring)
Subject Area Mechanics High School and First Year Javascript Students will be able to: 1. Create an interactive model of two objects colliding and sticking together with a perfectly inelastic collision by modifying the code from the completed Planetoids activity (Steps 1-9) 2. Further modify the code to model 2D perfectly inelastic collisions. This includes modifying an if statement to check if the two objects are close enough to collide (Steps 1-9) 3. Check if the computer program is conserving momentum as expected (Step 10) 60 min

These exercises are not tied to a specific programming language. Example implementations are provided under the Code tab, but the Exercises can be implemented in whatever platform you wish to use (e.g., Excel, Python, MATLAB, etc.).

### Step 2. Look at momentum.js and notice the new variables.

If you look at momentum.js you will notice that there are some new variables that weren't in the original planetoids.js. These variables specify the position and speed of the "blob" that we will add to the game. The goal is to make this a sticky blob so that the ship and the blob stick together when they collide.

### Step 3. Decide on a mass and radius for your blob.

The default values are mass_blob = 10.0 and radius_blob = 50, but go ahead and do your own thing, just don't make the blob any smaller than about 5 units in radius or larger than the size of the screen (about 500 units across).

### Step 4. Decide on an initial position for your blob.

The default values are x_blob = 475 and y_blob = 250. Use y_blob=250 but do your own thing and choose whatever you'd like for x_blob as long as the blob doesn't start on top of the ship.

### Step 5. Draw the blob!

Right after the display() function, use the drawBlob function to draw a circle like this:
    drawBlob(x_blob,y_blob,vx_blob,vy_blob,radius_blob);


At this point [your program should behave like this](http://www.physics.ohio-state.edu/~orban/momentum_files/v1/index.html)

### Step 6. Make sure there's a way for the blob to move!

In other words make sure there is something in the "Update location" section that allows the position of the blob to move with a constant velocity. Test it out by setting vx_blob=10 to see that it works, then set vx_blob=0 so that the blob is initially at rest. Hint: Mimic the code that allows the rocket to move.

### Step 7. Make it sticky.

The purpose of this programming lab is to allow the rocket to have a perfectly inelastic (a.k.a. sticky) collision with the blob. For a computer, a collision is something that happens when the position of the rocket is very close to the position of the blob. We need to add an if statement after the display; function so that something special happens when this occurs. In the end we will add something like this to the program:
if (some number < some other number) {
// do something special
drawText("Collision!",0.3*width,0.3*height);
noLoop(); // Ends the program
}

The thing inside the if() is called a "conditional statement". In 1-Dimension what we really want mathematically is this: $$\Delta x_{\rm blob} = x - x_{\rm blob}$$ $$|\Delta x_{\rm blob}| < R_{\rm blob}$$ Note that the vertical lines in Eq. 8 are an absolute value. Use the variables we've been using (and create new ones if you need to) in order to convert this math into code. If you do it right [your program should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/momentum_files/v2/index.html) Hint: Use abs() for the absolute value and remove noLoop(); which would otherwise stop the program.

### Step 8.

Just for fun, add vx_blob = vx; and see what happens. Why isn't this a physically accurate collision between the ship and the blob?

Let's consider again Eq. 5. $$m_1 v_{1i} + m_2 v_{2i} = (m_1 + m_2) v_f$$ Remember that $v_f$ is the speed of both objects after they stick together. Solve this equation for $v_f$. Explain why the following code should be placed between the curly brackets {} of the if statement: vx_blob = (mass*vx + mass_blob*vx_blob)/(mass + mass_blob); Be sure to explain why vx_blob appears on both sides of the equals sign! Don't forget that the velocity of the rocket needs to change because of the collision? What code should we add after this one to make sure that the rocket changes to the correct velocity? If you do this step correctly, [your program should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/momentum_files/v3/index.html)

### Step 10. Test your program!

Give the rocket an initial speed towards the blob by setting vx to some positive velocity near the beginning of the program. Then add some code to write the velocity of the blob to the screen:
  drawText("vx_blob = ",0.25*width,0.25*height);
drawText(vx_blob,0.4*width,0.25*height);

If you make these changes [your program will behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/momentum_files/v4/index.html) You will notice that the velocity of the blob goes from zero before impact to about 11.538 after impact if the initial velocity of the ship is set to 50 (default value) and the mass of the ship is 3.0 (default value) and the mass of the blob is 10.0 (default value). If the rocket is object 1 and the blob is object 2, this $v_f =$11.538 result makes sense because, according to Eq. 9, $$v_f = \frac{m_1 v_{1i} + m_2 v_{2i}}{m_1 + m_2} = \frac{3.0 \cdot 50 + 10.0 \cdot 0}{3.0 + 10.0} = 11.538$$ Choose a different set of velocities and masses than the default values, run your program to see what the velocity of the blob is after the collision and confirm that your program gives you the same result as the formula above for your numbers. Write the values you chose in the comments to your code or the comments in your code submission!.

### Step 11. Make it 2-Dimensional!

In 2-Dimensions, we end up with two equations for momentum conservation instead of just one. This is true of all three types of collisions, but the equations below will highlight what this looks like for perfectly inelastic collisions. In this case we have an equation for conservation of momentum in the x-direction that looks like this: $$m_1 v_{1xi} + m_2 v_{2xi} = (m_1 + m_2) v_{xf}$$ and we have an equation for conservation of momentum in the y-direction that looks about the same, $$m_1 v_{1yi} + m_2 v_{2yi} = (m_1 + m_2) v_{yf}$$ Go through the program and modify it to allow the rocket to move in two dimensions. (Feel free to look over the earlier planetoids guide if needed.) Then modify the code allow the blob to move in two dimensions. Then modify the code where the collision happens to make sure the collision is correct in 2-D (Hint: use the [pythagorean theorem](https://en.wikipedia.org/wiki/Pythagorean_theorem) to determine the distance between the rocket and the blob.) Finally, just to make it fun, open up functions.js and give the blob a random velocity by adding two lines to the setup() command. Change this:
function setup(){
createCanvas(750, 500);
x = width/4;
y = height/2;
}


To this:

function setup(){
createCanvas(750, 500);
x = width/4;
y = height/2;
vx_blob = random(-20,20);
vy_blob = random(-20,20);
}

In the end [your program should behave like this](http://www.physics.ohio-state.edu/~orban/physics_coding/momentum_files/v5/index.html)

### Extra credit: Customize!

Make some modification to the code to make the game different or more fun. Some ideas: change the type of collision to elastic (instead of perfectly inelastic). Consult the wikipedia page on elastic collisions for advice. Other ideas: projectiles, more blobs, or make the blob infinitely sticky so the rocket can never get away.

### How to get full credit for this programming lab!!!

1. You really do have to get the code working as described in steps 1-11 This is one of the most difficult programming labs because some of the steps don't give much guidance on what to do to the code to get it working. 2. Make sure you comment out or delete the noLoop() The program is no fun if it ends as soon as the ship collides with the blob. 3. You really do need to check that your collision conserves momentum This means making sure that you get $v_f =$11.53 when the blob is at rest and the ship collides with it at vx = 50. And you need to check that some other set of numbers gives you the answer that you expect from a perfectly inelastic collision. In some cases your final velocity will be significantly more or less than you expect. This means that you did something wrong in the coding (probably where you change the velocities because the ship and the blob has collided). Your code really does need to get the right answer here for you to get full credit. 4. Make sure you can fly your ship around the blob without colliding A lot of people don't realize that they can't fly the ship around the blob without colliding with it. In other words, their code thinks there is a collision whenever the x positions of the blob and the ship are the same and even if the y positions are totally different. Try flying around the blob in your code to see if this happens. If it does collide when it's not supposed to, you need to make sure to modify the if statement that determines if a collision has happened. This needs to work correctly for you to get full credit on this programming lab.