Vectors

Vectors are an integral part of game physics. Generally speaking vectors are written as $\vec{a}$ in mathematics, where a unit vector (a vector with a length of 1) is written as $\hat{b}$.

In physics a Vector usually consists consists of an angle $\theta$ and a length $L$, while in game programming we use float fields of the end position of a vector.

Operations

These are some common operations for vectors. Most of these are written as 3-dimensional vectors, but can be easily truncated to be 2-dimensional if necessary (unless otherwise specified).

Addition and Subtraction

Vector addition and subtraction is quite easy. You just take both vectors and perform the operation in a piecewise fashion, ex:

$$ \vec{c} = \vec{a} + \vec{b} = \begin{pmatrix} a_x + b_x \\ a_y + b_y \\ a_z + b_z \end{pmatrix} $$

Length

The length of a vector can be easily calculate using pythagoras’ formula as follows:

$$ \left|\vec{a}\right| = \sqrt{{a_x}^2 + {a_y}^2 + {a_z}^2} $$

Angle

The angle between two vectors can be calculated as follows.

$$ \cos{\theta} = \frac{\vec{a} \times \vec{b}}{\left|\vec{a}\right|\left|\vec{b}\right|} $$

Dot Product

Calculates the relation between two vectors. For computer purposes the following calculation is used, which can of course be truncated for 2-dimensional vectors.

$$ \vec{a} \cdot \vec{b} = a_xb_x + a_yb_y + a_zb_z $$

This is really useful in cases where both $\vec{a}$ and $\vec{b}$ are normalized vectors, in which case the dot product will be $1$ if $\hat{a}$ and $\hat{b}$ point in the same direction. If $\hat{a}$ and $\hat{b}$ are perpendicular the dot product will be $0$. If $\hat{a}$ and $\hat{b}$ point away from each other, the dot product will be $-1$.

Cross Product

A Cross product is an operation that can only be performed on 3-dimensional vectors which returns a new vector that is perpendicular to both given vectors.

We can take a cross product of two vectors by putting the vector elements into a 2x3 matrix and pretending to take the determinant, the relevant elements can then be used to construct a new vector with the result.

$$ \vec{a} \times \vec{b} = \begin{vmatrix} \vec{i} & \vec{j} & \vec{k} \\ a_x & a_y & a_z \\ b_x & b_y & b_z \end{vmatrix} $$

As such the following calculations can be done to determine the cross product ($\vec{c}$) of a 3-dimensional vector:

$$ c_x = \begin{vmatrix} a_y & a_z \\ b_y & b_z \end{vmatrix}\vec{i} = a_yb_z - a_zb_y $$

$$ c_y = \begin{vmatrix} a_x & a_z \\ b_x & b_z \end{vmatrix}\vec{j} = a_xb_z - a_zb_x $$

$$ c_z = \begin{vmatrix} a_x & a_y \\ b_x & b_y \end{vmatrix}\vec{k} = a_xb_y - a_yb_x $$

2-Dimensional Cross Product

There’s such a thing as the 2-dimensional cross product. It takes in two 2-dimensional vectors and returns a single value, which necessarily lies on the z-axis.

This can be calculated using the following calculation (Note that unlike the previous examples, $\vec{a}$ and $\vec{b}$ are 2-dimensional):

$$ c = \vec{a} \times \vec{b} = \begin{vmatrix} a_x & a_y \\ b_x & b_y \end{vmatrix}\vec{k} = a_xb_y - a_yb_x $$