Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Kinetic energy is like momentum, but is independent of direction. It’s like a magnitude of sorts that gauges the amount of total energy that a system has. Now, energy is the ability to do work and that’s all I’m going to say, we are getting a little too quantum here. However, computing kinetic energy is trivial, the formula is

Equation 2: Kinetic Energy

Momentum was just m*v, so you should see that kinetic energy is very similar, but it’s always positive and measured in kg*m2/s2, which in the Meter-Kilogram-Second system we just call Joules (J). The cool part is that the kinetic energy of any system is always the same before and after a collision, elastic, or not. Of course you would have to compute the energies lost due to deformation, heat, etc. to account for all the energy, but when assuming a perfectly elastic collision, the kinetic energy before and after can be computed by just knowing the velocities of the objects:

Equation 3: Total kinetic energy of collision

Combining this with equation 1 results in:

At this point, you have two equations and two unknowns and both vaf and vbf can be computed; however, the math is rather complex, so I will just give you the results:

Equation 4: The final velocities of each ball

    vaf = (2*mb*vbi + vai*(ma – mb))/(ma + mb)

    vbf = (2*ma*vai - vbi*(ma – mb))/(ma + mb)

Finally, referring back to Figure 13.11, you can figure out the final velocities after the collision of the blocks:

    ma = 2 kg

    mb = 3 kg

    vai = 4 m/s

    vbi = -2 m/s

Therefore,

    vaf = (2*3*(-2) + 4*(2 – 3))/(2 + 3)

        = (2*mb*vbi + vai*(ma – mb))/(ma + mb)

        = 1.6 m/s

    vbf = (2*ma*vai - vbi*(ma – mb))/(ma + mb)

        = (2*2*4 – (-2)*(2 – 3))/(2 + 3)

        =  2.4 m/s

Interestingly enough, object A had so much momentum it turned object B around and they both went off in the positive X direction as shown in part B of Figure 13.11.

What you just did shows how to use momentum and kinetic energy to help solve kinetic/dynamic problems. However, they get much more complex in 2 and 3 dimensions. The study of such collisions is called collision response, and it’s covered later in the chapter, along with the complete 2D results for perfect and imperfect collisions! For now, though, just think about momentum.

### Modeling Gravity Effects

One of the most common effects that a game programmer needs to model in a game is that of gravity. Gravity is the force that attracts every object in the universe to every other. It is an invisible force and unlike magnetic fields can’t be blocked.

In reality, gravity isn’t really a force. That’s simply how we perceive it. Gravity is caused by the curvature of space. When any object is positioned in space it creates a bending of the surrounding space, as shown in Figure 13.12. This bending creates a potential energy difference and hence any object near the gravity well “falls down” toward the object—weird, huh? That’s really what gravity is. It’s a manifestation of the bending of the space-time fabric.

Figure 13.12 Gravity and space-time.

You won’t need to worry about space-time curvature and what gravity really is; you just want to model it. There are really two cases that you need to consider when modeling gravity, as shown in Figure 13.13:

  • Case 1: Two or more objects with relatively the same mass.
  • Case 2: Two objects where the mass of one object is much greater than the other.

Figure 13.13 The two general cases of gravity.

Case 2 is really a sub-case of Case 1. For example, in school you may have learned that if you drop a baseball and a refrigerator off a building they both fall at the same rate. The truth of the matter is they don’t, but the difference is so infinitesimal (on the order 10-24) that you could never see the difference. Of course, there are other forces that might make a difference, like wind shear and friction, hence a baseball is going to fall faster than a piece of paper because the paper is going to feel a lot of wind resistance.

Math You may assume that any spherical mass of radius r is a point mass as long as the matter the sphere is made of is homogenous and any calculations must place the other object at a distance greater than or equal to r.

Now that you know a little bit about what gravity is, let’s take a look at the math behind it. The gravitational force between any two objects with mass m1 and m2 is

    F = G*m1*m2 / r2.

where G is the gravitational constant of the universe equal to 6.67×10-11 N*m2 * kg -2. Also, the masses must be in kilograms and the distance r in meters. Say that you want to find out what the gravitational attraction is between two average sized people of 70 kg (155 lbs.) at a distance of 1 meter:

    F = 6.67×10-11*70kg*70kg/(1 m)2 = 3.26×10-7 N.

That’s not much is it? However, let’s try the same experiment with a person and the planet Earth at 1 meter given that the Earth has a mass of 5.98×1024 kg:

    F = 6.67×10-11*70 kg*5.98×1024 kg/(1 m)2 = 2.79×1016 N.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.