# Guide to Game Development/Theory/Mathematics/Vectors

Example of a 3D vector

## Notation

Vectors are used to store multiple components of a variable, usually the axes of spacial dimensions. As a result they are usually either used in 2D or 3D.

There are technically two ways to represent a vector:

2D:
This could be defined as "a rotation and a magnitude" or as a "displacement in x and a displacement in y".
3D:
This could be defined as "a rotation in xz, a rotation in y and a magnitude" or as a "displacement in x, a displacement in y and a displacement in z".

Generally for games we use the displacement in dimensions version rather than the magnitude and rotation.

Vectors can come in a variety of forms:

i, j, k form - Where i means 1 unit in the x-axis, j means 1 unit in the y-axis and if you're in 3D, k means 1 unit in the z-axis

In 2D it looks like: ${\displaystyle 3i+4j}$  where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.

In 3D it looks like: ${\displaystyle 6i-3j+5k}$  where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.

Column vector - This is the most common vector form.

In 2D, it looks like: ${\displaystyle {\begin{pmatrix}3\\4\end{pmatrix}}}$  where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.

In 3D, it looks like: ${\displaystyle {\begin{pmatrix}6\\-3\\5\end{pmatrix}}}$  where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.

Row vector - This is the least common vector form.

In 2D, it looks like: ${\displaystyle {\begin{pmatrix}3&4\end{pmatrix}}}$  where 3 is 3 units in the positive x-axis and 4 is 4 units in the positive y-axis.

In 3D, it looks like: ${\displaystyle {\begin{pmatrix}6&-3&5\end{pmatrix}}}$  where 6 is 6 units in the positive x-axis, -3 is 3 units in the negative y-axis and 5 is 5 units in the positive z-axis.

When writing vectors you can denote it like this:

${\displaystyle {\overrightarrow {V}}}$  or just ${\displaystyle {\textbf {V}}}$

When going from one location to another (from A to B), it can be written like:

${\displaystyle {\overrightarrow {AB}}}$

${\displaystyle {\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}+{\begin{pmatrix}x_{2}\\y_{2}\\z_{2}\end{pmatrix}}={\begin{pmatrix}x_{1}+x_{2}\\y_{1}+y_{2}\\z_{1}+z_{2}\end{pmatrix}}}$
Subtraction:
${\displaystyle {\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}-{\begin{pmatrix}x_{2}\\y_{2}\\z_{2}\end{pmatrix}}={\begin{pmatrix}x_{1}-x_{2}\\y_{1}-y_{2}\\z_{1}-z_{2}\end{pmatrix}}}$

Note: The addition/subtraction of a constant to a vector is undefined.

### Vector Multiplication and division with a constant (scalar multiples)

What do you do when there is a constant multiplied/divided by a constant? You simply apply the multiplication/division to all components of the vector:

Multiplication:
${\displaystyle c{\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}={\begin{pmatrix}cx_{1}\\cy_{1}\\cz_{1}\end{pmatrix}}}$
Division:
${\displaystyle {\frac {\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}{c}}={\frac {1}{c}}{\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}={\begin{pmatrix}{\frac {x_{1}}{c}}\\{\frac {y_{1}}{c}}\\{\frac {z_{1}}{c}}\end{pmatrix}}}$

What about when there is a vector multiplied by another vector? There are two ways that are defined for vector multiplication: vector dot product and vector cross product.

### Unit length vector

The unit vector in 2D is based around this, meaning that x and y can't both be 1, else this wouldn't make a circle, it would make a square and the magnitude would be greater than 1 (a unit).

Unit length vectors are used when the magnitude isn't important, only the direction is. In 2D it's based around the unit circle and in 3D around the unit sphere; the values for the x, y and z will be between -1 and 1.

The notation for this is: ${\displaystyle {\hat {\textbf {V}}}}$

To turn a vector into a unit length vector, use the following equation: ${\displaystyle {\hat {\textbf {V}}}={\frac {\textbf {\overrightarrow {V}}}{{\Big |}{\textbf {\overrightarrow {V}}}{\Big |}}}}$ , this is known as normalising the vector.

### Null vector

A null vector (sometimes called a zero vector) is a vector where the contents are all 0s. If the null vector was denoted by ${\displaystyle {\overrightarrow {V}}}$ ;

... and it was in 2D, it would look like: ${\displaystyle {\overrightarrow {V}}={\begin{pmatrix}0\\0\end{pmatrix}}}$

... and it was in 3D, it would look like: ${\displaystyle {\overrightarrow {V}}={\begin{pmatrix}0\\0\\0\end{pmatrix}}}$

... and it was in ${\displaystyle N}$ D, it would look like: ${\displaystyle {\overrightarrow {V}}={\begin{pmatrix}0\\0\\\vdots \\0\end{pmatrix}}}$

An example of a null position vector would be the origin; this is because it has an origin of point ${\displaystyle (0,0)}$  or ${\displaystyle (0,0,0)}$ .

Another example would be if you wanted to stop an object from moving, you'd set the velocity to be a null vector.

## Magnitude

As a vector is made up of many parts, the numbers used for each dimension show the distance in each direction. In order to find the distance from point to point diagonally across the dimensions, you need to find the magnitude of the vector.

The magnitude of a 2D vector is given by (according to the Pythagorean Theorem):

${\displaystyle |{\overrightarrow {V}}|={\sqrt {V_{x}^{2}+V_{y}^{2}}}}$

The magnitude of a 3D vector is given by:

${\displaystyle |{\overrightarrow {V}}|={\sqrt {V_{x}^{2}+V_{y}^{2}+V_{z}^{2}}}}$

## Position vectors

A position vector is a vector that is treated like a coordinate, so it's a vector from the origin to a point. The letter O is often used to refer to the origin.

A position vector can be written as:

${\displaystyle {\overrightarrow {OA}}}$

${\displaystyle {\overrightarrow {OA}}={\begin{pmatrix}1\\0\\-3\end{pmatrix}}}$  is the same as saying there's a point ${\displaystyle A}$  which has coordinates: ${\displaystyle (1,0,-3)}$

### Calculating the vector between two position vectors

${\displaystyle {\overrightarrow {ba}}={\overrightarrow {a}}-{\overrightarrow {b}}}$

${\displaystyle {\overrightarrow {AB}}={\overrightarrow {OB}}-{\overrightarrow {OA}}}$

${\displaystyle {\overrightarrow {BA}}={\overrightarrow {OA}}-{\overrightarrow {OB}}}$

 Exercise: Find the vector between these position vectors Given that ${\displaystyle {\overrightarrow {OA}}={\begin{pmatrix}1\\0\\-3\end{pmatrix}}}$  and ${\displaystyle {\overrightarrow {OB}}={\begin{pmatrix}3\\2\\7\end{pmatrix}}}$ , find ${\displaystyle {\overrightarrow {AB}}}$  Answer: ${\displaystyle {\overrightarrow {AB}}={\begin{pmatrix}3\\2\\7\end{pmatrix}}-{\begin{pmatrix}1\\0\\-3\end{pmatrix}}}$  ${\displaystyle {\overrightarrow {AB}}={\begin{pmatrix}2\\2\\10\end{pmatrix}}}$

## Dot Product

There are two formulas for the dot product:

${\displaystyle {\overrightarrow {A}}\cdot {\overrightarrow {B}}=|{\overrightarrow {A}}||{\overrightarrow {B}}|\cos(\theta )}$  where ${\displaystyle \theta }$  is the angle between the two vectors.

${\displaystyle {\overrightarrow {A}}\cdot {\overrightarrow {B}}={\overrightarrow {A_{x}}}{\overrightarrow {B_{x}}}+{\overrightarrow {A_{y}}}{\overrightarrow {B_{y}}}+{\overrightarrow {A_{z}}}{\overrightarrow {B_{z}}}}$

### Angle between two vectors

From these, you can create another formula to calculate the angle between them:

${\displaystyle \theta =\cos ^{-1}{\Bigg (}{\frac {{\overrightarrow {A}}\cdot {\overrightarrow {B}}}{|{\overrightarrow {A}}||{\overrightarrow {B}}|}}{\Bigg )}=\cos ^{-1}{\Bigg (}{\frac {{\overrightarrow {A_{x}}}{\overrightarrow {B_{x}}}+{\overrightarrow {A_{y}}}{\overrightarrow {B_{y}}}+{\overrightarrow {A_{z}}}{\overrightarrow {B_{z}}}}{|{\overrightarrow {A}}||{\overrightarrow {B}}|}}{\Bigg )}}$

If ${\displaystyle {\overrightarrow {A}}\cdot {\overrightarrow {B}}=0}$ , then this means that the two vectors are perpendicular.

You should note that ${\displaystyle {\overrightarrow {A}}\cdot {\overrightarrow {B}}={\overrightarrow {B}}\cdot {\overrightarrow {A}}}$ .

## Scalar projection

Scalar projection

The Scalar projection (or scalar component) finds the length of the projection of one vector (A) onto another (B).

${\displaystyle {\bigg |}proj_{\overrightarrow {B}}{\overrightarrow {A}}{\bigg |}=|{\overrightarrow {A}}|\cos \theta ={\frac {{\overrightarrow {A}}\cdot {\overrightarrow {B}}}{|{\overrightarrow {B}}|}}}$

where θ is the angle between A and B[1].

The vector of this projection (A projected onto B) can also be found using the following formula[1]:

${\displaystyle proj_{\overrightarrow {B}}{\overrightarrow {A}}={\frac {{\overrightarrow {A}}\cdot {\overrightarrow {B}}}{|{\overrightarrow {B}}|^{2}}}{\overrightarrow {B}}}$

## Vector Equations

A basic vector equation consists of a vector and a variable coefficient of another vector. The standalone vector is known as the position vector and the vector with the variable coefficient is known as the direction vector. The position vector indicates where in the 3D space the vector is located, and the direction vector shows which way the vector pointing. The vector for the position vector can be the position vector of any point on the line.

A typical equations might look like this: ${\displaystyle {\textbf {r}}={\textbf {a}}+x{\textbf {b}}}$  or ${\displaystyle {\textbf {r}}={\begin{pmatrix}2\\2\\2\end{pmatrix}}+x{\begin{pmatrix}1\\-2\\3\end{pmatrix}}}$

### Checking if lines are parallel

Lines are parallel when their direction vectors are scalar multiples of each other.

Example:

Line 1: ${\displaystyle {\textbf {r}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+\lambda {\begin{pmatrix}1\\-2\\3\end{pmatrix}}}$  Line 2: ${\displaystyle {\textbf {s}}={\begin{pmatrix}2\\5\\7\end{pmatrix}}+\mu {\begin{pmatrix}-2\\4\\-6\end{pmatrix}}}$

They are parallel as ${\displaystyle -2{\begin{pmatrix}1\\-2\\3\end{pmatrix}}={\begin{pmatrix}-2\\4\\-6\end{pmatrix}}}$ .

### Checking if a point lies on a vector line equation

So if the point has the position vector ${\displaystyle {\textbf {P}}={\begin{pmatrix}2\\4\\4\end{pmatrix}}}$  and the line equation is ${\displaystyle {\textbf {r}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+t{\begin{pmatrix}2\\4\\3\end{pmatrix}}}$  and you want to see if P lies on the line, then you'll need to do the following:

Set the point equal to the equation:

${\displaystyle {\begin{pmatrix}2\\4\\4\end{pmatrix}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+t{\begin{pmatrix}2\\4\\3\end{pmatrix}}}$

From this you can then make 3 separate equations from each row:

${\displaystyle 2=1+2t}$
${\displaystyle 4=2+4t}$
${\displaystyle 4=3+3t}$

Then solve each equation to find t:

${\displaystyle 2=1+2t}$ ${\displaystyle t={\frac {1}{2}}}$
${\displaystyle 4=2+4t}$ ${\displaystyle t={\frac {1}{2}}}$
${\displaystyle 4=3+3t}$ ${\displaystyle t={\frac {1}{3}}}$

Only when all 3 values for t are the same is the point on the line, as one value of t is different to the other two, the point isn't on the line.

### Checking if two vector line equations intersect

So for this example, you'll need two lines, line 1: ${\displaystyle {\textbf {r}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+t{\begin{pmatrix}2\\4\\2\end{pmatrix}}}$  and line 2:${\displaystyle {\textbf {s}}={\begin{pmatrix}4\\4\\2\end{pmatrix}}+u{\begin{pmatrix}1\\-2\\-3\end{pmatrix}}}$

You then need to set the equations equal to each other.

${\displaystyle {\begin{pmatrix}1\\2\\3\end{pmatrix}}+t{\begin{pmatrix}2\\4\\2\end{pmatrix}}={\begin{pmatrix}4\\4\\2\end{pmatrix}}+u{\begin{pmatrix}1\\-2\\-3\end{pmatrix}}}$

Then subtract one set of constants to keep the constants on one side.

${\displaystyle t{\begin{pmatrix}2\\4\\2\end{pmatrix}}={\begin{pmatrix}3\\2\\-1\end{pmatrix}}+u{\begin{pmatrix}1\\-2\\-3\end{pmatrix}}}$

From this you can then make an equation from each row:

${\displaystyle 2t=3+u}$
${\displaystyle 4t=2-2u}$
${\displaystyle 2t=-1-3u}$

When finding the values of t and u, you'll only want to use two of the equations, that way you can check for a match on the third (this step for checking is not needed in 2D). I'm going to use equation 1 and equation 2: Use any type of simultaneous equation solving method you want

${\displaystyle 2t=3+u}$ ${\displaystyle 4t=6+2u}$  Doubled both sides of this equation
${\displaystyle 4t=2-2u}$

${\displaystyle 0=4+4u}$  got equation 1, and subtracted from it equation 2
${\displaystyle u=-1}$  solved for u

${\displaystyle 4t=6+2(-1)}$  substituted u back into equation 1
${\displaystyle t=1}$  solved for t

Now to check if it works in the final equation, substitute the values for u and t into the equation that you didn't use.

${\displaystyle 2t=-1-3u}$
${\displaystyle 2(1)=-1-3(-1)}$
${\displaystyle 2=2}$

If you get a value expression (a constant equals itself) then this means that they intersect, else they don't. This is known as a skew.

### The closest point to another point on a line equation and the distance between them

So let's say there's a vector equation ${\displaystyle {\textbf {r}}={\begin{pmatrix}2\\2\\2\end{pmatrix}}+t{\begin{pmatrix}1\\-2\\3\end{pmatrix}}}$  and there's a point P with a position vector: ${\displaystyle {\overrightarrow {OP}}={\begin{pmatrix}1\\1\\2\end{pmatrix}}}$ . We want to find both the coordinate of the point on the line that is closest to P and the distance between that point and P.

We'll refer to this point on the line as X.

First we need to use our dot product rule, because the line from P to X will be perpendicular to the line itself. So to do this we'll need to find the direction of the line and the vector from P to X.

The direction of the line is just the direction vector of the line (which we'll call A): ${\displaystyle {\textbf {A}}={\begin{pmatrix}1\\-2\\3\end{pmatrix}}}$

As for the direction of P to X, we'll need to find X first, even if it's in terms of t.

X is simply the line equation itself written out as one vector
${\displaystyle {\overrightarrow {OX}}={\begin{pmatrix}2+t\\2-2t\\2+3t\end{pmatrix}}}$
From that we can then find ${\displaystyle {\overrightarrow {PX}}}$  in terms of t using "Calculating the vector between two position vectors:"
${\displaystyle {\overrightarrow {PX}}={\overrightarrow {OX}}-{\overrightarrow {OP}}={\begin{pmatrix}2+t\\2-2t\\2+3t\end{pmatrix}}-{\begin{pmatrix}1\\1\\2\end{pmatrix}}={\begin{pmatrix}1+t\\1-2t\\3t\end{pmatrix}}}$

Now that we have the direction of both we can use the dot product:

${\displaystyle {\textbf {A}}\cdot {\overrightarrow {PX}}=0}$
${\displaystyle {\textbf {A}}_{x}{\overrightarrow {PX}}_{x}+{\textbf {A}}_{y}{\overrightarrow {PX}}_{y}+{\textbf {A}}_{z}{\overrightarrow {PX}}_{z}=0}$
${\displaystyle 1(1+t)-2(1-2t)+3(3t)=0}$
${\displaystyle (1+t)(4t-2)+(9t)=0}$
${\displaystyle 14t-1=0}$
${\displaystyle t={\frac {1}{14}}}$

Now that we have the value of t, we can substitute it into the equation for ${\displaystyle {\overrightarrow {OX}}}$ :

${\displaystyle {\overrightarrow {OX}}={\begin{pmatrix}2+t\\2-2t\\2+3t\end{pmatrix}}={\begin{pmatrix}2+{\frac {1}{14}}\\2-2({\frac {1}{14}})\\2+3({\frac {1}{14}})\end{pmatrix}}={\begin{pmatrix}{\frac {29}{14}}\\{\frac {13}{7}}\\{\frac {31}{14}}\end{pmatrix}}}$ , this is the position vector that we were looking for (X).

As for the distance between P and X, we need to substitute t into our ${\displaystyle {\overrightarrow {PX}}}$  equation:

${\displaystyle {\overrightarrow {PX}}={\begin{pmatrix}1+t\\1-2t\\3t\end{pmatrix}}={\begin{pmatrix}1+{\frac {1}{14}}\\1-2({\frac {1}{14}})\\3({\frac {1}{14}})\end{pmatrix}}={\begin{pmatrix}{\frac {15}{14}}\\{\frac {6}{7}}\\{\frac {3}{14}}\end{pmatrix}}}$
This is our vector, now we just need to find the length/magnitude of the vector:
${\displaystyle |{\overrightarrow {PX}}|={\sqrt {{\overrightarrow {PX}}_{x}^{2}+{\overrightarrow {PX}}_{y}^{2}+{\overrightarrow {PX}}_{z}^{2}}}={\sqrt {{\bigg (}{\frac {15}{14}}{\bigg )}^{2}+{\bigg (}{\frac {6}{7}}{\bigg )}^{2}+{\bigg (}{\frac {3}{14}}{\bigg )}^{2}}}={\sqrt {{\frac {225}{196}}+{\frac {36}{49}}+{\frac {9}{196}}}}={\sqrt {\frac {27}{14}}}={\frac {3{\sqrt {42}}}{14}}}$ ${\displaystyle 1.38873}$ , this is the distance between P and the closest point on the line(X).

## Cross Product

Cross Product (Left), Right hand rule (Right)

The cross product is defined as: ${\displaystyle {\overrightarrow {A}}\times {\overrightarrow {B}}=|{\overrightarrow {A}}||{\overrightarrow {B}}|\sin(\theta ){\hat {n}}}$  where ${\displaystyle \theta }$  is the angle between them and ${\displaystyle {\hat {n}}}$  is a unit vector that is perpendicular to both ${\displaystyle {\overrightarrow {A}}}$  and ${\displaystyle {\overrightarrow {B}}}$ . The cross product only works in 3 and in 7 dimensions (you won't need the 7D version).

There are two vectors that will be perpendicular to both A and B, one that's pointing out of the page/screen, and one that's pointing into the screen. One is ${\displaystyle {\overrightarrow {A}}\times {\overrightarrow {B}}}$  and the other is ${\displaystyle {\overrightarrow {B}}\times {\overrightarrow {A}}}$ . As they are both pointing in different directions and the magnitude is the same, this means that: ${\displaystyle {\overrightarrow {A}}\times {\overrightarrow {B}}=-{\overrightarrow {B}}\times {\overrightarrow {A}}}$ . You can find the direction of the cross vector by using the right-hand-rule.

As your computer doesn't have a 'right hand' you can use this formula that does the same thing:

${\displaystyle {\overrightarrow {A}}\times {\overrightarrow {B}}={\overrightarrow {C}}={\begin{pmatrix}A_{y}B_{z}-A_{z}B_{y}\\A_{z}B_{x}-A_{x}B_{z}\\A_{x}B_{y}-A_{y}B_{x}\end{pmatrix}}}$ [2]

## Normal vectors

A normal vector is a unit-length vector that points outwards from a surface of geometry. For every piece of flat geometry, there are two normals, one facing directly up (relative to the surface), the other facing directly down (relative to the surface), such that one is the negative of the other.

### Calculating a normal vector

To calculate a normal vector you'll first need to find a corner of the geometry. There there will be two edges that come from that point. You need to find the vectors for these two edges. From then you'll need to find the cross product of edge 1 (E1) and edge 2 (E2). After that you'll need to normalise the vector to make it unit length.

${\displaystyle {\overrightarrow {E_{1}}}\times {\overrightarrow {E_{2}}}={\overrightarrow {n_{1}}}}$

${\displaystyle {\hat {n_{1}}}={\frac {\overrightarrow {n_{1}}}{|{\overrightarrow {n_{1}}}|}}}$

Rewritten as one equation:

${\displaystyle {\hat {n_{1}}}={\frac {{\overrightarrow {E_{1}}}\times {\overrightarrow {E_{2}}}}{|{\overrightarrow {E_{1}}}\times {\overrightarrow {E_{2}}}|}}}$

Note: This is mathematically better, however it's computationally worse, use the first version with computers to avoid calculation of the cross product twice. This will require the storage of an extra variable, but it's much easier for the processor.

To find the other normal (n2) you can use one of the following equations:

${\displaystyle {\hat {n_{2}}}={\frac {{\overrightarrow {E_{2}}}\times {\overrightarrow {E_{1}}}}{|{\overrightarrow {E_{2}}}\times {\overrightarrow {E_{1}}}|}}}$  or ${\displaystyle {\hat {n_{2}}}=-{\hat {n_{1}}}}$