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

**represents a change, whose straight-line distance is always 1, in the same direction as the vector**

*n***.**

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

- Normalize the starting vector
**a**. - Find vector
**c**by performing the cross-product**c**=**a**x**b**. - If vector
**c**has a zero magnitude, then give up: this means that**a**and**b**are parallel. - Normalize vector
**c**. - 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: