Modeling Collisions

 < Day Day Up > 

The last section took a one-sided approach by looking at the effects of a collision on just one object using the impulse-momentum theorem. Now let's look at the effects on both objects involved in the collision. If two objects collide, and both can move, they both apply an impulse to each other due to the collision. Let's look at the impulse-momentum theorem one more time:

Ft = ( D p )

Let's divide both sides by time:

F = ( D p )/ t

Newton's Third Law states that for every action there's an equal and opposite reaction. In this case, one object places a force on another object, and at the same time, the second object places an equal and opposite force on the first object:

F 1 = F 2

Now let's substitute the impulse-momentum theorem on both sides:

( D p 1 )/ t = ( D p 2 )/ t , or

D p 1 = D p 2

Now let's insert the definition of momentum on both sides:

m 1 v 1f m 1 v 1i = ( m 2 v 2f m 2 v 2i )

m 1 v 1f m 1 v 1i = m 2 v 2f + m 2 v 2i

m 1 v 1i + m 2 v 2i = m 1 v 1f + m 2 v 2f

This last line has a special name : conservation of momentum theorem . This theorem states that the total amount of momentum stays the same when two objects collide. It simply transfers from one object to the other.

Conservation of Momentum Theorem

m 1 v 1i + m 2 v 2i = m 1 v 1f + m 2 v 2f

where the 1 subscript represents object 1 and the 2 subscript represents object 2.


This theorem applies to any isolated collision between two objects. If an outside force interferes with the collision, the theorem no longer holds up. However, due to the speed at which a collision occurs, very rarely does another force interfere.

NOTE

Remember that momentum is a vector quantity, so this theorem works the same way whether the collision occurs in 1D, 2D, or 3D.


Example 13.7: Conservation of Momentum Theorem

Suppose you're coding a 3D pool game, and you're focused on the cue ball hitting another ball at rest. If the cue ball has a mass of 0.5kg and an initial velocity of [50 10 30], and it completely stops when it hits the other ball with a mass of 0.45kg, what should the final velocity of the second ball be as a result of the collision?

Solution

Go straight to the conservation of momentum theorem, and fill in everything you know:

m 1 v 1i + m 2 v 2i = m 1 v 1f + m 2 v 2f

graphics/13equ13.gif


You can see that all the cue ball's momentum is transferred to the second ball when they collide.

Let's see this converted into a programming function:

 vector3D conserveMomentum(vector3D object1Start,vector3D  object1Stop, vector3D object2Start, float mass1, float mass2)      {          vector3D temp,temp2,sum,result;          //First calculate the left hand side of the equation.          temp  =  scalarMultiply(mass1,object1Start);          temp2 =  scalarMultiply(mass2,object2Start);          sum   =  sumVectors(temp,temp2);          //Divide the left hand side by the second object's mass to    //get the vector;          result = scalarDivide(mass2,sum);          return result;      } 

Using this function, we can find out the final vector of the second object after the collision. This function is used in the provided sample code and is capable of taking different start and stop vectors for the objects.

This is a simple example. However, it's quite possible that both objects will move together as a result of the collision. Let's look at such an example.

Example 13.8: Perfectly Inelastic Collision

Suppose you're coding a 2D football game with a top-down view. The player with the ball weighs 180lbs and is running down the field toward the end zone with a velocity of [0 30]. One of his opponents, who weighs 220lbs, is on the other side of the field when he starts running at [25 5] to make the tackle. After he makes the tackle, what is the players' final velocity as they go flying across the field together as a result of the tackle?

Solution
  1. The players' weights are approximated in pounds , but the theorem requires mass in kilograms. Convert the two weights to mass, as you did in Chapter 11:

    graphics/13equ14.gif


  2. Now that you have both masses, plug everything you know into the conservation of momentum theorem:

    m 1 v 1i + m 2 v 2i = m 1 v 1f + m 2 v 2f

    graphics/13equ15.gif


  3. The trick to this example is that the two players go down together, which means that they both have the same final velocity. In other words, v 1fx = v 2fx and v 1fy = v 2fy , so just call them v fx and v fy :

    graphics/13equ16.gif


The only reason you can calculate the final velocities is because they are the same. There's one more case to considerwhen both objects are moving as a result of the collision but they have different final velocities. If you have two unknown variables , v 1f and v 2f , you need a second equation to calculate both. This takes you back to Chapter 1, where you looked at linear combination and substitution. If you think of v 1f as x and v 2f as y , it's actually the same process. Use linear combination or substitution to find one final velocity, and then plug it in to find the other final velocity. To help you find that second equation, we need to discuss different types of collisions.

Every collision falls somewhere along a spectrum where one extreme is the elastic collision and the other extreme is the perfectly inelastic collision . An elastic collision is one in which no kinetic energy is lost when the two objects hit. The result is a collision where the objects appear to just bounce off each other like billiard balls. Example 13.8 showed a perfectly inelastic collision, where two objects stuck together and had the same final velocity. In real life, most collisions fall somewhere in between: Some energy is lost, but the two objects have different final velocities. You control how much energy is lost by defining a variable called the coefficient of restitution ( ). For an elastic collision, = 1, and for a perfectly inelastic collision, = 0. For most realistic collisions, falls somewhere between 0 and 1.

Linear Collision

( v 1f v 2f ) = ( v 1i v 2i )

where = the coefficient of restitution (0 < < 1).


Let's look at this formula more closely. If you plug in 0 for , the whole right side disappears, and essentially v 1f must equal v 2f . This is exactly what you saw happen in Example 13.8, so it makes sense. If you plug in 1 for , notice how the equation quickly simplifies to just ( v 1f v 2f ) = ( v 1i v 2i ). This is why most programmers often opt to just make all collisions elastic. However, you as the programmer get to choose the value of if you want to make the collision more realistic. The closer is to 0, the more energy that is lost in the collision. The closer it is to 1, the more it resembles an elastic collision such as billiard balls.

Example 13.9: Elastic Collision

Suppose you're coding a 2D pool game with a top-down view, and you're focused on the elastic collision of two identical billiard balls. If one ball has an initial velocity of [30 20] and the other has an initial velocity of [40 10] when they hit on center, what must their final velocities be?

Solution
  1. In this case, you don't know either final velocity, so you need to use both equations. First, fill in all you know for the conservation of momentum theorem:

    m 1 v 1i + m 2 v 2i = m 1 v 1f + m 2 v 2f

    graphics/13equ17.gif


    Because the balls are identical ( m 1 = m 2 ), the mass cancels out of every term :

    graphics/13equ18.gif


  2. Set up the second equation. Because it's an elastic collision, = 1.

    ( v 1f v 2f ) = ( v 1i v 2i )

    ( v 1f v 2f ) = 1( v 1i v 2i )

    ( v 1f v 2f ) = v 1i + v 2i

    graphics/13equ19.gif


  3. Here's where you can use linear combination or substitution. Either method will work. For this example, use linear combination:

    graphics/13equ20.gif


    Subtract the bottom equation from the top:

    graphics/13equ21.gif


    Substitute the top equation to find v 1f :

    graphics/13qeu23.gif


Note that the two balls transfer momentum because it is elastic. Imagine what might happen if you made = 0.9 instead of 1. Rightthe two final velocities would be a little slower.

Here is a look at elastic collision in code. This function will process the final velocity of two objects with the same mass given the current vectors of each. In this particular example, we have defined restitution at 1 like this:

 #define RESTITUTION 1 void elasticCollision(vector2D object1, vector2D object2, float mass)     {     vector2D object1Temp,object2Temp, sum, sum2,     object1Velocity,object2Velocity,temp;         //First calculate the left hand side of the equation.         object1Temp  =  scalarMultiply(mass,object1);         object2Temp =  scalarMultiply(mass,object2);         sum   =  sumVectors(object1Temp,object2Temp);        //setup right side of equation; multiply through by        //restitution value.        //This is only for illustration purposes in this example.         object1Temp = scalarMultiply(RESTITUTION,object1Temp);         object2Temp = scalarMultiply(RESTITUTION,object2Temp);        //for example purposes, since the equation switched to        // the right side.         object1Temp = scalarMultiply(-1,object1Temp);         sum2 = sumVectors(object1Temp,object2Temp);        //Now we have the two vectors we will need to complete the        //solution and calculate the vectors.         temp = subtractVectors(sum,sum2);         object2Velocity = scalarDivide(2,temp);         object1Velocity = subtractVectors(sum,object2Velocity);         //print the resulting velocities         cout<<"The first object's resulting velocity vector is ";         printVector(object1Velocity);         cout<<"The second object's resulting velocity vector is \n";         printVector(object2Velocity);     } 

Here, we immediately multiply both vectors by the coefficient of restitution. As you can see, we then follow through completing the equation to solve for the final velocity. This function will give us our answer but is fairly limited to solving one kind of problem.

At this point, you have a solid foundation to start experimenting with different types of collisions. The next section shows you how one programmer took this foundation and ran with it to make a remarkably realistic pool simulation.

Self-Assessment

1.

Suppose you're coding a 3D pool game, and you're focused on the cue ball hitting another ball at rest. The cue ball has a mass of 0.5kg and an initial velocity of [40 50 20]. It completely stops when it hits the other ball with a mass of 0.45kg. What is the second ball's final velocity as a result of the collision?

2.

Suppose you're coding a car crash in a 3D racing game. A car with a mass of 1500kg has a velocity of [30 20 40] when it hits another car of mass 1800kg going [50 40 20]. If the two cars become entangled in the crash, what's their final velocity?

3.

Suppose you're coding a car crash in a 3D racing game, but this time the cars don't get stuck together. A car with a mass of 1500kg has a velocity of [30 20 40] when it hits another car of mass 1800kg going [50 40 20]. Using e = 0.5, what should their final velocities be?


 < Day Day Up > 


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net