Lab 4: Planetoids with momentum!

Please take this pre-survey before beginning this exercise!

In a typical college physics course you will at some point encounter the idea of conservation of momentum. Momentum conservation is very important! In situations where two objects collide, we often don't know the force or acceleration as a function of time, but we do know the initial velocities. If we know the type of collision that occurs, it turns out that the initial velocities and the masses of the objects are all we need to figure out the final velocities.

Step 0. Read about momentum!

There are three types of collisions: elastic collisions, inelastic collisions and perfectly inelastic collisions. This programming lab focuses on perfectly inelastic collisions, which is the mathematically simplest kind of collision. But for completeness let's review all three different types of collisions and for simplicity let's assume 1-Dimensional collisions (instead of 2D or 3D).

Elastic collisions in 1-Dimension: Kinetic Energy is Conserved!

$$m_1 v_{1i} + m_2 v_{2i} = m_1 v_{1f} + m_2 v_{2f}$$

$$\frac{1}{2} m_1 v_{1i}^2 + \frac{1}{2} m_2 v_{2i}^2 = \frac{1}{2} m_1 v_{1f}^2 + \frac{1}{2} m_2 v_{2f}^2$$

Comments: If you know the masses of both objects ($m_1$ and $m_2$) and if you know the initial velocities of both objects ($v_{1i}$ and $v_{2i}$) then you really can figure out the final velocities ($v_{1f}$ and $v_{2f}$) from the conservation of momentum equation (Eq. 1) and the conservation of energy equation (Eq. 2). It involves a frustrating amount of algebra to calculate $v_{1f}$ and $v_{2f}$, but if you want to be overly-prepared for quizzes and tests, it would be a good idea to be able to know how to do this.

Inelastic collisions in 1-Dimention: Kinetic Energy is Definitely Not Conservered!

The idea here is that there was a collision and a significant amount of energy was lost in the form of heat or sound. Therefore the sum of the initial kinetic energies is more than the sum of the final kinetic energies.

$$m_1 v_{1i} + m_2 v_{2i} = m_1 v_{1f} + m_2 v_{2f}$$

$$\frac{1}{2} m_1 v_{1i}^2 + \frac{1}{2} m_2 v_{2i}^2 > \frac{1}{2} m_1 v_{1f}^2 + \frac{1}{2} m_2 v_{2f}^2$$

Comments: If all you know is the masses ($m_1$ and $m_2$) and the initial velocities ($v_{1i}$ and $v_{2i}$) then you actually can't figure out $v_{1f}$ and $v_{2f}$ without more information. It turns out that the energy conservation equation (Eq. 4) is useless. It's not even an equation, it's an inequality! In a situation like this you would need someone to tell you what $v_{1f}$ is so you can figure out $v_{2f}$ from the conservation of momentum equation (Eq. 3).

Perfectly inelastic collisions in 1-Dimension: Kinetic Energy is Definitely Not Conserved but the objects stick together!

Perfectly inelastic collisions is where two objects collide and stick to each other so that they travel together afterwards. Mathematically, this means that $v_{1f} = v_{2f} = v_f$.

$$m_1 v_{1i} + m_2 v_{2i} = m_1 v_{1f} + m_2 v_{2f} = (m_1 + m_2) v_f $$

$$\frac{1}{2} m_1 v_{1i}^2 + \frac{1}{2} m_2 v_{2i}^2 > \frac{1}{2} m_1 v_{1f}^2 + \frac{1}{2} m_2 v_{2f}^2$$

Once again, the energy conservation equation (Eq. 6) is useless and we just use the conservation of momentum equation (Eq. 5) to figure out the final velocity of the combined (a.k.a. stuck together) object.

Step 1. Open 1D Planetoids code

Click here to open the code in an editor

Step 2. Look at the code and notice the new variables.

If you look at the code you will notice that there are some new variables that weren't in the original planetoids program (highlighted below in yellow). These variables specify the position and speed of the "blob" that we will add to the game.

x = 187;
y = 250;

vx = 0;
vy = 0;

deltaVx = 0;
deltaVy = 0;

theta = 0;

Fthrust = 30.0;
mass = 3.0;
dt = 0.1;

x_blob = 475;
y_blob = 250;
mass_blob = 10;
radius_blob = 50;

vx_blob = 0;
vy_blob = 0;

The goal is to make this a sticky blob so that the ship and the blob stick together when they collide.

Step 3. Draw the blob!

Right after the display() function, use the drawBlob function to draw a circle like this:


After you paste this code into the code editor your program should behave like this.

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

Add something to the "Update location" section that allows the position of the blob (x_blob) to move with a constant velocity (vx_blob). It will look a lot like the update location code for the rocket (x += vx*dt;)

Then, near the beginning of the code where the variables are initialized, set vx_blob=10;  and run the code to see that the blob drifts to the right.

At this point your program should behave like this.

Step 5. Make it sticky

Set vx_blob = 0; so the blob is at rest but change vx = 50; so the rocket moves to the right.

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 drawBlob so that something special happens when this occurs. In the end we will add something like this to the program:

    if (some variable?? < some other variable??) {
vx_blob = vx; // set the velocity of the blob equal to that of the ship
vx = vx_blob; // set the velocity of the ship equal to that of the blob

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. 

Hint: Use abs() for the absolute value.

At this point your program should behave like this.

Step 6. Add physics

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. Solving for $v_f$ we get:

$$v_f = \frac{m_1 v_{1i} + m_2 v_{2i}}{m_1 + m_2}$$

So replace this:

vx_blob = vx;

with this:

vx_blob = (mass*vx + mass_blob*vx_blob)/(mass + mass_blob);

At this point your program should behave like this.

Step 7. Test your program!

You will notice that the velocity of the blob goes from zero before impact to about 11.54 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.54 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.54$$

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 8. Explain why we should leave vx = vx_blob;

At this point, you will have something like this:

if ( something < something else){
vx_blob = (mass*vx + mass_blob*vx_blob)/(mass + mass_blob);
vx = vx_blob;

just for fun, try this instead:

if ( something < something else ){
vx_blob = (mass*vx + mass_blob*vx_blob)/(mass + mass_blob);
vx = (mass*vx + mass_blob*vx_blob)/(mass + mass_blob);

Explain why the above code doesn't work! Why doesn't this give the right answer for the velocity? What happens instead?

Optional: Plot the total momentum versus time

Add this code to the program to add a graph showing the momentum vs time:

  graph1.display();   // forest green
  graph2.display(); // bright green

  graph3.display();   // gray

The total momentum is shown with a gray line. Use this graph to explain why the code highlighted earlier does NOT conserve total momentum whereas using vx = vx_blob; does.

Step 9. Make it 2-Dimensional!

Go ahead and change the collision code back to vx = vx_blob; so that the collisions can happen correctly. The previous step was just a little experiment in what not to do.

In this step you will modify the code until it works in two dimensions, including the collision.

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 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);

to this:

function setup(){
    createCanvas(750, 500);
    vx_blob = random(-20,20);
    vy_blob = random(-20,20);

In the end your program should behave like this

Extra credit:

Make the blob infinitely sticky so the ship can never get away!

Please take this post-survey after completing the exercise!

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

1. You really do have to get the code working as described in steps 1-9

2. You really do need to check that your collision conserves momentum.

This means making sure that you get $v_f$=11.54 when the blob is at rest and the ship collides with it at $v_x$ = 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.

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