Simple physics

From DmWiki

The physics section of a game engine controls how game objects move around and interact with one another. It is responsible for realistically simulating things like gravity, the recoil from a weapon, objects slipping and sliding down hills, vehicles that can be driven, and a host of other effects. This article will teach you the very basic concepts of physics, in preparation for understanding more advanced articles, both here at DevMaster and elsewhere.

Table of contents

Concepts

There are four basic quantities associated with any object that obeys the laws of physics in a game world. The first three are vectors and the last one is a scalar (a single real number).

  • First, you have position. This is obvious - it's just a vector that gives the location of the object in space.
  • The velocity vector tells how fast an object is moving, and in what direction. It is distinct from speed, which is just a scalar, the length of the velocity vector. For example, an object might have a speed of 100 kph, but its velocity vector might be [60, -80, 0], indicating that it's moving at 100 kph to the southeast. Mathematically, velocity is the time derivative of position.
  • The next step beyond velocity is acceleration, which is also a vector, and tells how the velocity is changing. Acceleration corresponds to the direction that the object is being pulled by any forces, such as gravity, that are acting on the object. Acceleration is the time derivative of velocity.
  • Finally, every object that is controlled by physics must have a mass. This tells how heavy the object is - for example, how many kilograms it weighs.

Straight-Line Motion

You have to walk before you can run, and it's the same idea in physics - you have to be able to make an object move at a constant speed in a straight line before trying anything more complicated.

This is more tricky than it might sound. The obvious way to do this would be to just add the velocity to the position each frame, but that means that the object's speed will depend on the framerate. Things would move twice as fast when the framerate doubled! To ensure proper time-based motion, the velocity must be multiplied by the time in seconds taken to render the last frame.

// Engine calls this function, passing the time taken to render the last frame
void UpdatePhysics (float frametime)
{
     for (each object)
          object.vecPosition += object.vecVelocity * frametime;
}

This way, when the framerate doubles, the variable frametime will be halved and so the velocity per frame will be halved. That way the velocity that the player sees - the velocity with respect to real time - will be constant.

For more information on this issue, see timing.

Newton's Laws

You've probably heard of Newton's three laws of motion. Even though they are theoretical, these three laws form the basis for game physics, and are so important that it's worth it to go over them briefly here.

  • First Law: objects with no forces acting on them keep moving in a straight line at constant speed.
  • Second Law: when a force acts on an object, the acceleration equals the force divided by the object's mass.
  • Third Law: if object A exerts a force on object B, then object B exerts the opposite force on object A.

These three laws will be built into the physics routines in your engine. The first one is already there in the UpdatePhysics function above. To incorporate the second law, we can do the following:

// Engine calls this function, passing the time taken to render the last frame
void UpdatePhysics (float frametime)
{
     for (each object)
     {
          vec3 force = GetForces(object);                 // Return the sum of all forces acting on this object
          object.vecAcceleration = force / object.mass;   // Calculate acceleration using Second Law
          object.vecVelocity += object.vecAcceleration * frametime;
          object.vecPosition += object.vecVelocity * frametime;
     }
}

Note that both the acceleration and the velocity must be multiplied by the frame time before adding them to the velocity and the position, respectively.

This gives you the basic physics framework. You can implement the GetForces function to do whatever you want; all physics-based motion is the result of different forces. Some simple forces you can implement are gravity and drag. For more information on implementing physics, see the articles linked below.

See Also

Intermediate Topics

Advanced Topics


DevMaster navigation