Home | AI | Physics | Mathematics | Compiler Design | Games | Downloads | About Me
You are visitor number 147050 since February 2003
Physics
Introduction
Velocity
Acceleration
Momentum
Collisions
Gravity
Friction
 
Free Books


Velocity

Velocity is a physical quantity that is probably familiar to everyone. It is defined as the rate of change of distance per unit time. Velocity is usually used in computer games to compute the position of our objects. In other words, it is used to move our objects in the game. For instance, when we move a ball from left to right on the screen, that ball is being moved by the velocity, which in games is measured in pixels per second.

The formula used to calculate the position of the object is the following:

object_x = object_x + velocity_x
object_y = object_y + velocity_y
object_z = object_z + velocity_z

The formula indicates that with every game cycle the object moves velocity_x units in the x–axis, velocity_y units in the y–axis, and velocity_z units in the z–axis. Note that the formula above is used in 3–D. For 2–D calculations, we ommit the z component.

A velocity is represented by a vector quantity v, and as we know from the vectors tutorial, this quantity may have 2 components (x, y) in 2–D and 3 components (x, y, z) in 3–D. Since the position of an object can also be represented by a vector p, the above formula can be simplified as follows:

p = p + v, (where p is the position of the object and v is the velocity at which it moves.)

You will usually use this formula in your game loop as follows:

for each frame
     p = p + v
     draw_object(p)

For each frame of animation, the object will be moved on the screen by V = (vx, vy, vz) units and then drawn in the screen.

As an example, I will show you a car moving across the screen in the x-axis with constant velocity. The car is wrapped up around the screen. When it reaches the end of the right border, it automatically shows up at the left border. And the same for up and bottom borders. Note that the car could be moving up. This example just shows you how the object moves with velocity in the x-axis. Our velocity is set to 6 pixels per frame.

In this example we have shown an object moving at constant velocity. To see an example of an object moving at varying velocity, see the acceleration tutorial, where you can play around with the car.

Hope this tutorial was useful to you. If you have any query, please do not hesitate to contact me.

Best Regards

Fidel


Books



© 2002, 2003 Fidel Viegas. All rights reserved.