The Laws of Motion

Game development community call point masses with particles.

The Particle

A particle has a position, but no orientation. In other words, we can’t tell what direction a particle is pointing: it either doesn’t matter or it doesn’t make sense.

The First Two Laws

There are three laws of motion put forward by Newton; The first two laws of motion follow:

  1. An object continues with a constant velocity unless a force acts upon it.
  2. A force acting on an object produces acceleration that is proportional to the object’s mass.

The First Law

The first law tells us what happens if there are no forces around. The object will continue to move with a constant velocity. In the real world, we can’t get away from forces acting on a body; the closest phenomenon that we can imagine is the movement of objects in space.

In a physics engine we could simply assume that there are no forces at work and use Newton-1 directly. To simulate drag, we could add special drag forces. This is fine for the simple engine, but can cause problems with more complex systems. The problem arises because the processor that performs the physics calculations isn’t completely accurate. This inaccuracy can lead to objects getting faster of their own accord.

A better solution is to incorporate a rough approximation of drag directly into the engine. If we need complicated drag (such as aerodynamic drag in a flight simulator or racing game) we can still dot hat the long way, by creating a special drag force.

The Second Law

The second law tells us how forces alter the motion of an object. A force is something that changes the acceleration of an object (i.e., the rate of change of velocity).

The Force Equations

The formula relating the force to the acceleration is the famous

image

The vector form of that is given by

image

Adding Mass to Particles

It is often useful, however, to simulate infinite masses. These are objects that no force of any magnitude can move. They might be just as physically impossible, but they are very useful for immovable objects in a game: the walls or floor, for example, cannot be moved. If we feed an infinite mass into the force equation, then the acceleration will be zero, as we want. As long as such an object has zero initial velocity, it will always stay in the same place.

Unfortunately, we cannot represent a true infinity in most computer languages, and the optimized mathematics instructions on all common game processors do not cope well with infinities.

Note that in last equation we use 1 over the mass to calculate our acceleration. we can speed up our calculations by storing 1 over the mass. We call this the inverse mass. This solves our problem for representing objects of zero or infinite mass: infinite mass objects have a zero inverse mass, which is easy to set. Objects of zero mass would have an infinite inverse mass,which cannot be specified in most programming languages.

Momentum and Velocity

Momentum is the product of velocity and mass. Since mass is normally constant, we can assume that velocity is therefore constant by Newton-1. In the event that a traveling object is changing mass, then its velocity would also be changing, even with no force.

This will be an important distinction when we consider rotations later, however, because rotating objects can change the way their mass is distributed. Under the rotational form of Newton-1, that means a change in rotational speed with no other forces acting.

The Force of Gravity

The formula Newton developed is called the law of universal gravitation:

image

where m1 and m2 are the masses of the two objects, r is the distance between their centers, f is the resulting force, and G is the “universal gravitational constant,” a scaling factor derived from observation of planetary motion.

Because we are only interested in the pull of the Earth, we can simplify the previous equation. First, we can assume that m1 is always constant (i.e., the Earth doesn’t change mass). Second, and less obviously, we can assume that r is also constant. This is due to the huge distances involved. The distance from the surface of the Earth to its center is so huge (6400 km) that there is almost no difference in gravity between standing at sea level and standing on the top of a mountain. For the accuracy we need in a game, we can therefore assume the r parameter is constant. With these assumptions, the equation is simplified to:

image

where m is the mass of the object we are simulating, f is the force, as before, and g is a constant that includes the universal gravitational constant, the mass of the Earth,and its radius:

image

Creating simulations with a g value of 10 ms^−2 can look dull and insipid. Most developers use higher values, from around 15 ms^−2 for shooters (to avoid projectiles having arcs that are too curved) up to 20 ms^−2 typical of driving games. Some developers go further and incorporate the facility to tune the g value on an object-by-object basis.

The acceleration due to gravity can therefore be represented as a vector with the following form:

image

The Integrator

The integrator consists of two parts—one to update the position of the object, and the other to update its velocity. The position will depend on the velocity and acceleration, while the velocity will depend only on the acceleration.

The Mathematics of Particles

Vectors

There are many different kinds of vector spaces with wildly different properties, but for our purposes the only vector spaces we’re interested in are regular (called Euclidean) 2D and 3D space.

Some of the work in a slightly different way to scalar values, and some operations that make sense for scalars (such as division) aren’t defined for vectors.

The Handedness of Space

You can tell which is which using your hands: make a gun shape with your hand,thumb and extended forefinger at right angles to one another. Then, keeping your ring finger and pinky curled up, extend your middle finger so that it is at right angles to the first two. If you label your fingers with the axes in order (thumb is X, forefinger Y, and middle finger Z), then you have a complete set of axes, whether right- or left-handed.

DirectX is left-handed and OpenGL is right handed.

Vectors and Directions

A change in position, given as a vector, can be split into two elements:

image

1- Magnitude (d): the straight-line distance of the change.

2- Unit vector (n): the direction of the change. The vector n represents a change, whose straight-line distance is always 1, in the same direction as the vector a.

We can find d using the 3D version of Pythagoras’s theorem, which has the formula,

image

We can find unit vector by this equation

image

The process of finding just the direction n from a vector is called “normalizing”.

The result of decomposing a vector into its two components is sometimes called
the normal form of the vector”.

Multiplying Vectors

In algebra for scalar values, there is only one kind of multiplication. We write this in various ways, either with no symbol at all (ab), with a dot (a . b), or with a multiplication symbol (a x b).

With vectors these three notations have different meanings, and we have to be more precise. Using no symbol usually denotes a type of multiplication (vector direct product) is not used commonly. The two other notations that we will encounter are called the scalar product (a . b) and the vector product (a x b). First, however, we’ll meet a fourth way of multiplying vectors that uses none of these symbols.

The Component Product

The most obvious product is the least useful. It is used in several places in a physics engine, but despite being quite obvious, it is rarely mentioned in books on vector mathematics. This is because it doesn’t have a simple geometric interpretation—if the two vectors being multiplied together represent positions, then it isn’t clear geometrically how their component product is related to their locations. This isn’t true of the other types of product, as we’ll see.

The Scalar Products (Dot Product)

The dot product is calculated with the following formula

image

The Trigonometry of the Scalar Product

The dot product relates the scalar product to the length of the two vectors and the angle between them:

image

So if we have two normalized vectors then the angle between them is given by Equation

image

If a and b are just regular vectors then the angle would be given by

image

The Geometry of the Scalar Product

If one vector is not normalized, then the size of the scalar product is multiplied by its length. In most cases at least one vector, and often both, will be normalized before performing a scalar product.

The range of a dot product is between 1 and –1 where 1 means that two vectors are identical and –1 means they are totally in opposite direction.

The dot product is most likely be as part of a calculation that needs to find how much one vector lies in the direction of another.

The Vector Product (Cross Product)

The cross product is calculated with this formula

image

The Trigonometry of the Vector Product

Below is the trigonometric equation

image

Commutativity of the Vector Product

You may have noted in the derivation of the vector product that it is not commutative.
In other words, a x b = b x a.

In fact, by comparing the components in the previous equation, we can see that

image

The Geometry of the Vector Product

For a pair of vectors normal of a and b, the magnitude of the vector product represents the component of b that is not in the direction of normal of a.

Because it is easier to calculate the scalar product than the vector product, if we need to know the component of a vector not in the direction of another vector, we are better performing the scalar product and then using the Pythagoras theorem to give the result,

image

where c is the component of b not in the direction of a, and s is the scalar product normal of a . b.

In fact, the vector product is very important geometrically not for its magnitude, but for its direction. In three dimensions, the vector product will point in a direction that is at right angles (i.e., 90 degrees, also called orthogonal) to both of its operands

image

This interpretation shows us an important feature of the vector product: it is only defined in three dimensions. In two dimensions, there is no possible vector at right angles to two nonparallel vectors.

The Orthonormal Basis

In some cases we want to construct a triple of mutually orthogonal vectors, where each vector is at right angles to the other two. Typically we want each of the three vectors to be normalized. This kind of triple vector that is both orthogonal and normalized is called an orthonormal basis.

There are a few ways of doing this. The simplest is to use the cross-product to generate the orthogonal vectors.

The process starts with two nonparallel vectors. The first of these two will not have its direction changed at all: call this a.We cannot change its direction during the process, but if it is not normalized, we will change its magnitude. The other vector, b, may not already be at right angles to a, so it may need to have its direction as well as magnitude changed. One constraint on vector b, however, is that it must not be parallel to vector a. If it is parallel, then we cannot find a unique third vector that is at right angles to both a and b—there are an infinite number of such vectors. The third vector, c, is not given at all, as it is determined entirely from the first two. The algorithm proceeds as follows:

  1. Normalize the starting vector a.
  2. Find vector c by performing the cross-product ca x b.
  3. If vector c has a zero magnitude, then give up: this means that a and b are parallel.
  4. Normalize vector c.
  5. Now we need to ensure that a and b are at right angles to one another. We can do this by recalculating b based on a and c using the cross-product, b = c x a (note the order). The resulting vector b must already be unit length, because both c and a were and we know these are orthogonal.

Calculus

There are two ways of understanding changing quantities: we describe the change itself, or we describe the results of the change. If an object is changing position with time, we need to be able to understand how it is changing position (i.e., its speed, the direction it is moving in, whether it is accelerating or slowing), and the effects of the change (i.e.,where it will be when we come to render it at the next frame of the game).

These two viewpoints are represented by the differential and integral calculus, respectively.

Differential Calculus

we are interested in the rate a quantity is changing with respect to time. This is sometimes informally called its “speed,” but that term is ambiguous. We will call it by the more specific term, “velocity.”

Velocity

it’s computed from this formula

image

where v is the velocity of the object, p’ and p are its positions at the first and second measurements. and delta t is the time that has passed between the two.

If we want to calculate the exact velocity of an object, we could reduce the gap between the first and second measurement. In mathematics, this is written using “limit” notation, as in

image

Rather than use this limit notation, this is more commonly written with a lowercase “d” in place of the delta:

image

Because it is so common in mechanics to be talking about the change with respect to time, this is often simplified even further:

image

Acceleration

This is the change of velocity over time. A positive value for acceleration represents speeding up, a zero acceleration means no change in velocity at all, and negative acceleration represents slowing down. Below is the acceleration formula

image

velocity is the first differential of position, and if we differentiate again we get acceleration, so acceleration is the second differential. Mathematicians often write it in this way:

image

Again the short term for this is

image

Vector Differential Calculus

So far we’ve looked at differentiation purely in terms of a single scalar quantity. For full 3D physics, we need to deal with vector positions rather than scalars. That’s how velocity is represented in 3D

image

And that’s how acceleration is represented:

image

Velocity, Direction and Speed:

The velocity of an object, as we’ve seen, is a vector giving the rate that its position is changing.

The speed of an object is the magnitude of this velocity vector, irrespective of the direction it is moving in.

By decomposing the velocity vector, we can get the speed and the direction of movement:

image

where s is the speed of the object, and image is its direction of movement.

Using the equations for the magnitude and direction of any vector, the speed is given by:

image

and direction by

image

Integral Calculus

In the same way that we obtained velocity from the position using differentiation, we go the other way in integration. If we know the velocity, then we integrate to work out the position at some point in the future. If we know the acceleration, we can find the velocity at any point in time.

If we know that an object is moving with a constant velocity (i.e., no acceleration), and we know this velocity along with how much time has passed, we can update the position of the object using the formula:

image

where image is the constant velocity of the object over the whole time interval.

In the same way, we could update the object’s velocity in terms of its acceleration using the formula:

image

Same could be given in terms of acceleration

image

In game development, integrate means to perform the position or velocity updates.

Vector Integral Calculus

The previous equations can be expressed in vectors like this:

image

and perform the calculation on a component-by-component basis:

image

Introduction to Game Physics

What is Game Physics?

When we talk about physics in a game, we really mean classical mechanics, that is,
the laws that govern how large objects move under the influence of gravity and other
forces.

In games, classical mechanics is used to give game objects the feel of being solid
things, with mass, inertia, bounce, and buoyancy.

As processing power became available, we saw crates that could be moved around
or stacked, and walls that could be destroyed and crumble into their constituent
blocks. This is rigid-body physics, which rapidly expanded to include softer objects:
clothes, flags, and rope. Then came the rise of the ragdoll: a physical simulation
of the human skeleton that allows more realistic trips, falls, and death throes. And
recently we’ve seen a lot of effort focused on simulating fluid flow: water, fire, and
smoke.

What is Physics Engine?

A physics engine is a common piece of code that knows about physics in general, but isn’t programmed with the specifics of each game.

The physics engine is basically a big calculator: it does the mathematics needed to simulate physics. But it doesn’t know what needs to be simulated. In addition to the engine we also need game-specific data that represents the objects in our level.

The advantages of using physics engine are well known. On the other hand, the main disadvantage is that a general-purpose physics engine is quite processor-intensive. Because it has to be general, it can make no assumptions about the kinds of objects it is simulating.

Approaches to Physics Engine

Types of Objects

The first distinction is between engines that simulate full rigid bodies or so-called “mass aggregate” engines. Rigid-body engines treat objects as a whole, and work out the way they move and rotate. A crate is a single object, and can be simulated as a whole. Mass aggregate engines treat objects as if they were made up of lots of little masses. A box might be simulated as if it were made up of eight masses, one at each corner, connected by rods.

Mass aggregate engines are easier to program because they don’t need to understand rotations.

Mass aggregate engines treat each mass as if it were located at a single point, and the equations of motion can be expressed purely in terms of linear motion. The whole object rotates naturally as a result of the connections between masses.

Contact Resolution

The second distinction involves the way in which touching objects are processed. a lot of the difficulty in writing a rigid-body physics engine is simulating contacts—locations where two objects touch or are connected.

There are different ways to resolve this problems:

1. Handle each contact one by one, making sure each works well on its own.

2. Use Jacobian-based approach: Calculate the exact interaction between different contacts and calculate an overall set of effects to apply to all objects at the same time.

3. Calculate a set of equations based on the contacts and constraints between objects.

Impulses and Forces

The third distinction is in how the engine actually resolves contacts.

Impulse is the integral of a force with respect to time. When a force is applied to a rigid body it changes the momentum of that body.