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

#### Velocity (v)

Velocity is the instantaneous rate of speed of an object and is usually measured in meters per second (m/s) or in the case of the automobile, miles per hour, or mph. Whatever units you prefer, velocity is the change in position per change in time. Stated mathematically in a 1 dimensional case, this reads:

Velocity = v = ds/dt.

In other words, the instantaneous change in position (ds) with respect to time (dt). As an example, say you are driving down the road and you just drove 100 miles in one hour, then your average velocity would be

v = ds/dt = 100 miles/1 hour = 100 mph.

In a video game the concept of velocity is used all the time, but again the units are arbitrary and relative. For example, in a number of the demos I have written I usually move objects at 4 units in the x- or y-axis per frame with code something like the following:

    x_position = x_position + x_velocity;

    y_position = y_position + y_velocity;

That translates to 4 pixels/frame. But frames aren’t time, are they? Actually, they are as long as the frame rate stays constant. In the case of 30 fps, which is equal to 1/30th of a second per frame, the 4 pixels per frame translate to:

    Virtual Velocity = 4 pixel / (1/30) seconds

                     = 120 pixels per second.

I always amaze my friends by telling them how long it will take to get to an off-ramp or some other location when we’re in the car. The Trick is simple; just look at the speed and use the fact that at 60 mph it takes 1 minute to go 1 mile. So if the driver is driving 60 and the off ramp is in 2 miles then it will take 2 minutes. On the other hand, if the on ramp is in 3.5 miles then it would take 3 minutes and 30 seconds. And if the driver isn’t driving 60 mph then use the closest plus or minus 30 mph. For example, if they’re going 80 then do you calculations with 90 mph (1.5 miles per minute) and then shrink your answer a bit.

Hence, the objects in our game have been moving with velocities measured in pixels/second. If you wanted to get crazy then you could estimate how many virtual meters were in one pixel in your game world and perform the computation in meters/second in cyberspace. In either case, now you know how to gauge where an object will be at any given time or frame if you know the velocity. For example, if an object was currently at position x0 and it was moving at 4 pixels/frame, and 30 frames go by, the object would be at

    new position = x0 + 4 * 30 = x0 + 120 pixels.

This leads us to our first important basic formula for motion:

    New Position = Old Position + Velocity * Time

                 = xt = x0 + v*t.

This formula states that an object moving with velocity v that starts at location x0 and moves for t seconds will move to a position equal to its original position plus the velocity times the time. Take a look at Figure 13.5 to see this more clearly. As an example of constant velocity I have created a demo `DEMO13_1.CPP|EXE` that moves a fleet of choppers from left to right on the screen.

Figure 13.5 Constant velocity motion.

#### Acceleration (a)

Acceleration is similar to velocity, but it is the measure of the rate of change of velocity rather than the velocity itself. Take a look at Figure 13.6; it illustrates an object moving with a constant velocity and one with a changing velocity. The object moving with a constant velocity has a flat line (slope of 0) for its velocity as a function of time, but the accelerating object has a slope of non-zero because its velocity is changing as a function of time.

Figure 13.6 Velocity versus acceleration.

Figure 13.6 illustrates constant acceleration. There is also non-constant acceleration. In this case the line would be a curve in part C of Figure 13.6. Pressing the accelerator in your car will give you the feeling of non-constant acceleration and jumping off a cliff will give you the feeling of constant acceleration. Mathematically, acceleration is the rate of change of velocity with respect to time:

    Acceleration = a = dv/dt.

The units of acceleration are a little weird. Since velocity is already in units of distance per second, acceleration is in units of distance per second*second, or in the metric system, m/s2. If you think about this it makes sense because acceleration is the change of velocity (m/s) per second. Furthermore, our second motion law relates the velocity, time, and acceleration, which states that the new velocity at some time, t, in the future equals the starting velocity plus the acceleration times the amount of time the object has been accelerating for:

    New Velocity  = Old Velocity + Acceleration * Time

                  = vt = v0 + a*t.

Acceleration is a fairly simple concept and can be modeled in a number of ways, but let’s take a look at a simple example. Imagine that an object is located at (0,0) and it has a starting velocity of 0. If you were to accelerate it at a constant velocity of 2 m/s, you could figure out the new velocity each second simply by adding the acceleration to the last velocity, as shown in Table 13.1.

Table 13.1 Velocity as a Function of Time for Acceleration 2 m/s2

Time (t = s) | Acceleration (a = m/s2) | Velocity (v = m/s)
--- | --- | ---

0 | 2 | 0
1 | 2 | 2
2 | 2 | 4
3 | 2 | 6
4 | 2 | 8
5 | 2 | 10

Taking the data in the table into consideration, the next step is to figure out the relationship between position, velocity, acceleration, and time. Unfortunately, this takes a bit of calculus, so I’ll just give you the results in terms of position at some time t:

    xt = x0 + v0*t + 1/2*a*t2

This equation states that the position of an object at some time t is equal to its initial position, plus its initial velocity, times time, plus one half the acceleration, times time squared. The 1/2*a*t2 term is basically the time integral of the velocity. Let’s see if you can use the equation in your game world of pixels and frames. Refer to Figure 13.7.

Figure 13.7 An acceleration in pixels/frame2.

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.