# 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:

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,

We can find unit vector by this equation

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

#### 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:

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

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

#### 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

#### The Trigonometry of the Vector Product

Below is the trigonometric equation

#### 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

#### 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,

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

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

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

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

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

#### 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

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:

Again the short term for this is

#### 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

And that’s how acceleration is represented:

#### 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:

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

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

and direction by

### 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:

where 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:

Same could be given in terms of acceleration

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:

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

# 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.

# Racing Games AI

This genre is mostly divided into two main groups, vehicular and specialty.

The variants of vehicular games appeared early, and the split stuck. They are differentiated by their camera perspective: the first-third person racing game and an overhead view.

The specialty racing games are mostly fad driven – they involve the hot racing style sport at the time.

One last subtype is the cart racing game, which simplifies the driving portion of the game and adds obstacles, strange trucks, and other action elements.

#### Common AI Elements

##### Track AI

Where the road meets the rubber, track AI is the system needed to keep a CPU-controlled car on racetrack (or city street) at high speed within the rules of the game. This is usually a state-based system with the different vehicle states detailing the main ways that a racer can be on track. As guidelines, most games use combination of physical and optimal lines of travel (which are either data path laid down in track editor, or calculated automatically by a technique known as “finding the path of minimum curvature”, as shown in figure below) that mimic the invisible lines of travel that humans use when they race on tracks and roads.

##### Traffic

Some games use traffic systems that look very realistic with lane changes, cars getting over for police vehicles, proper use of traffic lights and intersections. This is mostly FSM behaviors, with a lot of message passing to ensure that accidents don’t happen and some randomness to ensure that things don’t look repetitive.

##### Pedestrians

A game like GTA 2 has different types of pedestrians for example, one type can stop by and steal your car, other one can pass away while you are moving. Other systems use simple flocking behavior.

##### Enemy and Combat

That’s about adding AI bots with weapons or something to play against the player.

##### Nonplayer Characters

NPCs are folks that you deal with to give you information, clean your car, etc… These characters are most probably scripted.

##### Other competitive behavior

Adding more interesting playing elements to the game, like having bridge fall down while you are racing with the car.

#### Useful AI Techniques

##### Messaging System

The traffic and pedestrians mostly use this technique to communicate and coordinate their movement.

##### Genetic Algorithms

A genetic algorithm is used to tune car performance parameters until it reaches best outcome to be used. Most probably that is done offline.

#### Areas that need improvement

##### Other areas of interest than crime

Not everybody’s mother wants to see her kid running over a prostitute for her wallet.

##### Persisting world

it’d be great if the whole world simulations are computed together.