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

Assume these initial conditions: The object is at x=50 pixels, the initial velocity is 4 pixels/frame, and the acceleration is 2 pixels/frame2. Finally, assume that these are the conditions at frame 0. To find the position of the object at any time in C/C++, use the following:

    x = 50 + 4*t + (0.5)*2*t*t;

Where t is simply the frame number. Table 13.2 lists some examples for t = 0,1,2…5.

Table 13.2 An Object Moving with Constant Acceleration

Time/Frame (t) | Position (x) | Delta (x)=xt-xt-1
--- | --- | ---

0 | 50 | 0
1 | 50+4*1+(0.5)*2*12 = 55 | 5
2 | 50+4*2+(0.5)*2*22 = 62 | 7
3 | 50+4*3+(0.5)*2*32 = 71 | 9
4 | 50+4*4+(0.5)*2*42 = 82 | 11
5 | 50+4*5+(0.5)*2*52 = 95 | 13

There’s a lot of interesting data in Table 13.2, but maybe the most interesting data is the change in position each time the frame is constant and equal to 2. Now this doesn’t mean that the object moves 2 pixels per frame, it means that the change in motion each frame gets larger or increases by 2 pixels. Thus on the first frame the object moves 5 pixels, then on the next frame it moves 7, then 9, 11, then 13, and so on. And the delta between each change in motion is 2 pixels, which is simply the acceleration!

> Note: Of course this example is one-dimensional. You can upgrade to two dimensions simply by adding a y position (and y velocity and acceleration if you wish).

The next step is to model acceleration with C/C++ code. Basically, here’s the trick: you set up an acceleration constant and then with each frame you add it to your velocity. This way you don’t have to use the long equation shown earlier—you simply translate your object with the given velocity. Here’s an example:

    int acceleration = 2; // 2 pixels per frame

        velocity     = 0, // start velocity off at 0

        x            = 0; // start x position of at 0 also

    // ...

    // then you would execute this code each

    // cycle to move your object

    // with a constant acceleration

    // update velocity

    velocity+=acceleration;

    // update position

    x+=velocity;

To see acceleration in action, I have created a demo named `DEMO13_2.CPP|EXE` that allows you to fire a missile that accelerates as it moves forward. Press the spacebar to fire the missile, the up and down arrow keys to increase and decrease the acceleration factor, and the A key to toggle the acceleration on and off. Look at the difference acceleration makes to the motion of the missile and how acceleration gives the missile a sense of “mass.”

#### Force (F)

One of the most important concepts in physics is force. Figure 13.8 depicts one way to think of force. If an object with mass m is sitting on a table with gravity pulling it toward the center of the Earth, the acceleration is a=g (force of gravity). This gives the mass m weight and if you try to pick it up you will feel a pain in your lower back.

Figure 13.8 Force and weight.

The relationship between force, mass, and acceleration is Newton’s Second Law:

F=m*a

In other words, the force exerted on an object is equal to its mass times the acceleration of the object. Or, rearranging terms:

a = F/m

This states that an object will accelerate an amount equal to the force you place on it divided by its mass. Now let’s talk about the units of measure. But instead of just blurting it out, let’s see where it comes from in the metric system. Force is equal to mass times acceleration or kilograms multiplied by m/s2 (m stands for meters, not mass). Hence, a possible unit of force is

F = kg*m/s2—kilogram–meters per second squared

This is a bit long, so Newton just called it—a Newton (N). As an example, imagine that a mass m equal to 100 kg is accelerating at a rate of 2 m/s2. The force that is being applied to the mass is exactly equal to F = m*a = 100 kg * 2 m/s2 = 200N.

This gives you a bit of a feel for a Newton. A 100 kg mass is roughly equivalent to the force of 220 lbs. on Earth, and 1 m/s2 is a good accelerating run.

In a video game the concept of force is used for many reasons, but a few that come to mind are

  • You want to apply artificial forces like explosions to an object and compute the resulting acceleration.
  • Two objects collide and you want to compute the forces on each.
  • A game weapon only has a certain force, but it can fire different virtual mass shells and you want to find the acceleration the shells would experience when fired.

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.