# Guide to Game Development/Theory/Mathematics/Vectors
## 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:
In 2D it looks like: $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: $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.
In 2D, it looks like: ${\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: ${\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.
In 2D, it looks like: ${\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: ${\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:
${\overrightarrow {V}}$ or just ${\textbf {V}}$
When going from one location to another (from A to B), it can be written like:
${\overrightarrow {AB}}$

### Vector Addition and Subtraction

Adding vectors is as simple as adding like for like terms.
       Addition:
             ${\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:
             ${\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:
             $c{\begin{pmatrix}x_{1}\\y_{1}\\z_{1}\end{pmatrix}}={\begin{pmatrix}cx_{1}\\cy_{1}\\cz_{1}\end{pmatrix}}$
       Division:
             ${\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

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: ${\hat {\textbf {V}}}$
To turn a vector into a unit length vector, use the following equation: ${\hat {\textbf {V}}}={\frac {\overrightarrow {\textbf {V}}}{{\Big |}{\overrightarrow {\textbf {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 ${\overrightarrow {V}}$;
... and it was in 2D, it would look like: ${\overrightarrow {V}}={\begin{pmatrix}0\\0\end{pmatrix}}$
... and it was in 3D, it would look like: ${\overrightarrow {V}}={\begin{pmatrix}0\\0\\0\end{pmatrix}}$
... and it was in $N$D, it would look like: ${\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 $(0,0)$ or $(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):
$|{\overrightarrow {V}}|={\sqrt {V_{x}^{2}+V_{y}^{2}}}$
The magnitude of a 3D vector is given by:
$|{\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:
${\overrightarrow {OA}}$
${\overrightarrow {OA}}={\begin{pmatrix}1\\0\\-3\end{pmatrix}}$ is the same as saying there's a point $A$ which has coordinates: $(1,0,-3)$

### Calculating the vector between two position vectors

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

## Dot Product

There are two formulas for the dot product:
${\overrightarrow {A}}\cdot {\overrightarrow {B}}=|{\overrightarrow {A}}||{\overrightarrow {B}}|\cos(\theta )$ where $\theta$ is the angle between the two vectors.
${\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:
$\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 ${\overrightarrow {A}}\cdot {\overrightarrow {B}}=0$, then this means that the two vectors are perpendicular.
You should note that ${\overrightarrow {A}}\cdot {\overrightarrow {B}}={\overrightarrow {B}}\cdot {\overrightarrow {A}}$.

## Scalar projection

    See also: w:Dot_product#Scalar_projection_and_first_properties
The Scalar projection (or scalar component) finds the length of the projection of one vector (A) onto another (B).
    ${\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.
The vector of this projection (A projected onto B) can also be found using the following formula:
    $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: ${\textbf {r}}={\textbf {a}}+x{\textbf {b}}$ or ${\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: ${\textbf {r}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+\lambda {\begin{pmatrix}1\\-2\\3\end{pmatrix}}$ Line 2: ${\textbf {s}}={\begin{pmatrix}2\\5\\7\end{pmatrix}}+\mu {\begin{pmatrix}-2\\4\\-6\end{pmatrix}}$
They are parallel as $-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 ${\textbf {P}}={\begin{pmatrix}2\\4\\4\end{pmatrix}}$ and the line equation is ${\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:
       ${\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:
       $2=1+2t$
       $4=2+4t$
       $4=3+3t$
Then solve each equation to find t:
       $2=1+2t$ → $t={\frac {1}{2}}$
       $4=2+4t$ → $t={\frac {1}{2}}$
       $4=3+3t$ → $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: ${\textbf {r}}={\begin{pmatrix}1\\2\\3\end{pmatrix}}+t{\begin{pmatrix}2\\4\\2\end{pmatrix}}$ and line 2:${\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.
       ${\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.
       $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:
       $2t=3+u$
       $4t=2-2u$
       $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
       $2t=3+u$ → $4t=6+2u$ Doubled both sides of this equation
       $4t=2-2u$
       $0=4+4u$ got equation 1, and subtracted from it equation 2
       $u=-1$ solved for u
       $4t=6+2(-1)$ substituted u back into equation 1
       $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.
       $2t=-1-3u$
       $2(1)=-1-3(-1)$
       $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 ${\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: ${\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): ${\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
             ${\overrightarrow {OX}}={\begin{pmatrix}2+t\\2-2t\\2+3t\end{pmatrix}}$
       From that we can then find ${\overrightarrow {PX}}$ in terms of t using "Calculating the vector between two position vectors:"
             ${\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:
       ${\textbf {A}}\cdot {\overrightarrow {PX}}=0$
       ${\textbf {A}}_{x}{\overrightarrow {PX}}_{x}+{\textbf {A}}_{y}{\overrightarrow {PX}}_{y}+{\textbf {A}}_{z}{\overrightarrow {PX}}_{z}=0$
       $1(1+t)-2(1-2t)+3(3t)=0$
       $(1+t)(4t-2)+(9t)=0$
       $14t-1=0$
       $t={\frac {1}{14}}$
Now that we have the value of t, we can substitute it into the equation for ${\overrightarrow {OX}}$:
       ${\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 ${\overrightarrow {PX}}$ equation:
       ${\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:
             $|{\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}}$ ≈ $1.38873$, this is the distance between P and the closest point on the line(X).

## Cross Product

The cross product is defined as: ${\overrightarrow {A}}\times {\overrightarrow {B}}=|{\overrightarrow {A}}||{\overrightarrow {B}}|\sin(\theta ){\hat {n}}$ where $\theta$ is the angle between them and ${\hat {n}}$ is a unit vector that is perpendicular to both ${\overrightarrow {A}}$ and ${\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 ${\overrightarrow {A}}\times {\overrightarrow {B}}$ and the other is ${\overrightarrow {B}}\times {\overrightarrow {A}}$. As they are both pointing in different directions and the magnitude is the same, this means that: ${\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:
       ${\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}}$

## 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 (E$_1$) and edge 2 (E$_2$). After that you'll need to normalise the vector to make it unit length.
${\overrightarrow {E_{1}}}\times {\overrightarrow {E_{2}}}={\overrightarrow {n_{1}}}$
${\hat {n_{1}}}={\frac {\overrightarrow {n_{1}}}{|{\overrightarrow {n_{1}}}|}}$
Rewritten as one equation:
${\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 (n$_2$) you can use one of the following equations:
${\hat {n_{2}}}={\frac {{\overrightarrow {E_{2}}}\times {\overrightarrow {E_{1}}}}{|{\overrightarrow {E_{2}}}\times {\overrightarrow {E_{1}}}|}}$ or ${\hat {n_{2}}}=-{\hat {n_{1}}}$

## See also

* Creating a vector3 class in code

## External links

* Cross product videos by Khan Academy who explains the right hand rule: Episode 1 Episode 2
* Cross product video by Jorge Rodriguez with computer version: https://www.youtube.com/watch?v=FT7MShdqK6w

## References

* 1 2 http://onlinemschool.com/math/assistance/vector/projection/
* ↑ https://www.youtube.com/watch?v=FT7MShdqK6w at 5:17