Speed and Velocity

 < Day Day Up > 

Any time an object is moving, it has some speed. Speed measures how fast an object is moving. If an object has speed, it also has a velocity , which is simply the vector version of speed. You might want to flip back to Chapter 4, "Vector Operations," for a quick refresher on the difference between a vector and a scalar quantity. Velocity is speed with a direction. In one dimension, the direction is given by positive or negative. (Chapter 10, "Motion in Two and Three Dimensions," address how to deal with direction in 2D and 3D.) For now, just consider the motion of an object along a straight line, where positive velocity is forward and negative velocity is backward.

NOTE

The terms "speed" and "velocity" may be used interchangeably in dialogue. Whenever speed is used, the direction is ignored.


Let's first look at an object moving with a constant velocity. For example, suppose I set the cruise control on my car to 60mi/hr. If I maintain that constant speed for an hour , I should go 60 miles. If I keep the cruise control set, I should go 120 miles in 2 hours, 180 miles in 3 hours, and so forth. This leads to the following statement.

Displacement with Constant Velocity

displacement = velocity * time ( D = v * t )

for any constant velocity v .


NOTE

If you chose to ignore direction because the road is not perfectly straight, the same formula applies to the scalar equivalents: distance = speed * time for any constant speed.


Example 8.1: Finding Displacement with Constant Velocity

Suppose you're programming a portion of a game in which the main character jumps into a vehicle with a constant velocity of 150px/s. How far has he moved after 1 second, 2 seconds, and 3 seconds?

Solution
  1. Set up a formula to calculate displacement:

    D = v * t = (150px/s) * t

  2. Plug in each time interval:

    After 1s : D = (150px/s) * t = (150px/s) * (1s) = 150px

    After 2s : D = (150px/s) * t = (150px/s) * (2s) = 300px

    After 3s : D = (150px/s) * t = (150px/s) * (3s) = 450px

Let's put this in programming terms. As I flip through my game frame by frame, I want to know how far my car moves between each frame. Remember that displacement is the change in position: new position, old position. If I plug that into the preceding formula, I get an equation that can go directly in the code:

 //Passing through floats for velocity and time we return the resulting displacement. float calcDisplacement(float vel, float time)      {        return vel*time;      } 

Some game architectures try only to pass the relative change in an object's position over a network to reduce the overall size and amount of network data. This results in sending a net displacement of the object as opposed to the raw firing of the absolute position of an object in the game. Using this method, it is possible to calculate the displacement locally and then fire off a change to the server only if needed. Try to expand this example to work with multiple time periods and calculate an object's total displacement over a larger measure of time.

Displacement Between Frames

New_position = Old_position + Velocity * Time

where Time is one frame (usually 1/30th of a second).


NOTE

The framerate will most likely not stay constant at 30fps. Most developers calculate a "delta time" by subtracting the system clock's value at the last frame simulation from the value at this simulation and then use that as the time factor. This way, the simulations aren't thrown out of whack by the changes in framerate that naturally occur when new AIs are spawned or particle systems are introduced.


Example 8.2: Finding a New Position with Constant Velocity

Let's revisit Example 8.1. You really don't care how far the vehicle has moved after 1 whole second. What you really want to know is how far it moves between frames ( assuming that the frame rate is 30fps). If the vehicle is at the 50-pixel mark in one frame, where should it be in the next frame?

Solution
  1. Set up a formula to calculate the new position:

    New_position = Old_position + Velocity * Time = 50px + (150px/s) * Time

  2. Because the frame rate is 30fps, the time interval of one frame is 1/30th of a second. Calculate the new position with a time of 1/30th of a second:

    New_position = 50px + (150px/s) * (1/30s) = 55px

Creating a function to handle this calculation works like this:

 //Passing through floats for velocity and time we return the resulting //position at the time specified. float calcPosition(float oldPosition, float vel, float time)      {        return oldPosition + (vel*time);      } 

All this works if you know the object is moving at a constant velocity. But how often do you drive around with the cruise control on? Certainly in some games , it's easy to set up sections in which a vehicle moves at a constant speed, but you probably wouldn't want to do this for a racing game. Next time you're in a car, watch the speedometer for a while; you'll probably notice that the speed constantly changes. Therefore, we need to discuss two different types of velocity: average and instantaneous. Let's look at average velocity first.

Suppose you take a road trip. You clock your miles using the odometer and find that you travel 200 miles in 4 hours. You might say that your average speed for the trip is 200 miles/4 hours, or 50mi/hr. However, you probably didn't maintain a constant speed of 50mi/hr (unless you had the cruise control on). Most likely you sped up at different times or even came to a stop for a red light, so your speed probably kept changing during the trip, but the average speed was 50mi/hr. The line over the v indicates that we're using the average velocity, as shown next.

Average Velocity

graphics/08equ01.gif


for any displacement D x and time interval t .


Example 8.3: Calculating Average Velocity

Suppose a police officer sets up a speed trap on a straight section of a busy road. He marks a



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