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

Here are the controls:

Key | Action
--- | ---

Up, down | Controls the angle of the tank’s cannon.
Right, left | Controls the velocity of the projectile.
G, B | Controls the gravity.
W, E | Controls the wind speed.
Ctrl | Fires the cannon!

### The Evil Head of Friction

The next topic of discussion is friction. Friction is any force that retards or consumes energy from another system. For example, automobiles use internal combustion to operate; however, a whopping 30–40 percent of the energy that is produced is eaten up by thermal conversion or mechanical friction. On the other hand, a bicycle is about 80–90 percent efficient and is probably the most efficient mode of transportation in existence.

#### Basic Friction Concepts

Friction is basically resistance in the opposite direction of motion and hence can be modeled with a force usually referred to as the frictional force. Take a look at Figure 13.17; it depicts the standard frictional model of a mass m on a flat plane.

Figure 13.17 Basic friction model.

If you try to push the mass in a direction parallel to the plane you will encounter a resistance or frictional force that pushes back against you. This force is defined mathematically as:

    Ffstatic = m*g*μs.

where m is the mass of the object, g is the gravitational constant (9.8 m/s2) and μs is the static frictional coefficient of the system that depends on the conditions and materials of the mass and the plane. If the force F you apply to the object is greater than Ff, then the object will begin to move. Once the object is in motion its frictional coefficient usually decreases to another value, which is referred to as the coefficient of kinetic friction, μk.

    Ffkinetic = m*g*μk.

When you release the force, the object slowly decelerates and comes to rest because friction is always present.

To model friction on a flat surface all you need do is apply a constant negative velocity to all your objects that is proportional to the friction that you want. Mathematically, this is

    Velocity New = Velocity Old - friction.

The result is objects that slow down at a constant rate once you stop moving them. Of course, you have to watch out for letting the sign of the velocity go negative or in the other direction, but that’s just a detail. Here’s an example of an object that is moved to the right with an initial velocity of 16 pixels per frame and then slowed down at a rate of 1 pixel per frame due to virtual friction:

    int x_pos      = 0,  // starting position

        x_velocity = 16, // starting velocity

        friction   = -1; // frictional value

    // move object until velocity <= 0

    while(x_velocity > 0)

         {

         // move object

         x_pos+=x_velocity;

         // apply friction

         x_velocity+=friction;

         } // end while

The first thing you should notice is how similar the model for friction is to gravity. They are almost identical. The truth is that both gravitational forces and frictional forces act in the same way. In fact, all forces in the universe can be modeled in the exact same way. Also, you can apply as many frictional forces to an object as you want. Just sum them up.

As an example of friction I have written a little air hockey demo named `DEMO13_5.CPP|EXE`, shown in Figure 13.18. The program lets you fire a hockey puck on a virtual air hockey table in a random direction every time you press the spacebar. The puck then bounces around off the borders of the table until it comes to rest due to friction. If you want to change the frictional coefficient of the table, use the arrow keys. See if you can add a paddle and a computer controlled opponent to the simulation!

Figure 13.18 The hockey demo.

#### Friction on an Inclined Plane (Advanced)

That wasn’t too bad huh? The bottom line is that friction can be modeled as a simple resistive force or a negative velocity on an object each cycle. However, I want to show you the math and derivation of friction on an inclined plane since this will give you the tools you need to analyze much more complex problems. Be warned, though: I’m going to use a lot of vectors, so if you’re still rusty or having trouble then take a look back when I talked about them in Chapter 8, “Vector Rasterization and 2D Transformations,” or pick up a good linear algebra book.

Figure 13.19 shows the problem we’re trying to solve. Basically, there is a mass m on an inclined plane. The plane has frictional coefficients μs and μk for the static and kinetic (moving) cases respectively. The first thing we need to do is write the formulas that describe the mass in its equilibrium position, that is, not moving. In this case, the sum of the forces in the x-axis are 0 and the sum of the forces in the y-axis are 0.

Figure 13.19 The inclined plane problem.

To begin the derivation we must first touch on a new concept called the normal force. This is nothing more than the force that the inclined plane pushes the object back with, or in other words, if you weigh 200 lbs., then there is a normal force of –200 lbs. pushing back (due to the surface tension of ground you’re standing on) at you. We usually refer to the normal force as η, and it is equal in magnitude to

    η = m*g.

Math Note that I use sine and cosine to resolve the x,y components of the force. I’m basically just breaking the force vectors into components, nothing more.

Interesting huh? But if you lay a coordinate system down, then the gravity force must be opposite the normal force, or

    η - m*g = 0.

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.