Part 4: Planetoids with momentum!

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$$

Comments: 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 Momentum.js, For convenience this code has been renamed momentum.js (instead of planetoids.js) and some minor modifications have been made to both functions.js and momentum.js.

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

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:

At this point your program should behave like this.

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

Step 9. 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. 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.

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

If you make these changes your program will behave like this. 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 to determine the distance between the rocket and the blob.) Finally, just to make it fun, give the blob a random velocity near the beginning of the program using this code:

vx_blob = random(-20,20);
vy_blob = random(-20,20);

In the end your program should behave like this.

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.