Guide to Game Development/Theory/Mathematics/Vectors

Example of a 3D vector

NotationEdit

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

  or just  

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

 

Vector Addition and SubtractionEdit

Adding vectors is as simple as adding like for like terms.

Addition:
 
Subtraction:
 

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

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

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

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 vectorEdit

 
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:  

To turn a vector into a unit length vector, use the following equation:  , this is known as normalising the vector.

Null vectorEdit

A null vector (sometimes called a zero vector) is a vector where the contents are all 0s. If the null vector was denoted by  ;

... and it was in 2D, it would look like:  

... and it was in 3D, it would look like:  

... and it was in  D, it would look like:  

An example of a null position vector would be the origin; this is because it has an origin of point   or  .

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

MagnitudeEdit

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

 

The magnitude of a 3D vector is given by:

 

Position vectorsEdit

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:

 

  is the same as saying there's a point   which has coordinates:  

Calculating the vector between two position vectorsEdit

 
 

 

 

Exercise: Find the vector between these position vectors

Given that   and  , find  

Answer:

 

 

Dot ProductEdit

There are two formulas for the dot product:

  where   is the angle between the two vectors.

 

Angle between two vectorsEdit

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

 

If  , then this means that the two vectors are perpendicular.

You should note that  .

Scalar projectionEdit

See also: w:Dot_product#Scalar_projection_and_first_properties
 
Scalar projection

The Scalar projection (or scalar component) finds the length of the projection of one vector (A) onto another (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]:

 

Vector EquationsEdit

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

Checking if lines are parallelEdit

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

Example:

Line 1:   Line 2:  

They are parallel as  .

Checking if a point lies on a vector line equationEdit

So if the point has the position vector   and the line equation is   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:

 

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

 
 
 

Then solve each equation to find t:

  
  
  

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 intersectEdit

So for this example, you'll need two lines, line 1:   and line 2: 

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

 

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

 

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

 
 
 

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

   Doubled both sides of this equation
 


  got equation 1, and subtracted from it equation 2
  solved for u


  substituted u back into equation 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.

 
 
 

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 themEdit

So let's say there's a vector equation   and there's a point P with a position vector:  . 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):  

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
 
From that we can then find   in terms of t using "Calculating the vector between two position vectors:"
 

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

 
 
 
 
 
 

Now that we have the value of t, we can substitute it into the equation for  :

 , 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   equation:

 
This is our vector, now we just need to find the length/magnitude of the vector:
  , this is the distance between P and the closest point on the line(X).

Cross ProductEdit

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

The cross product is defined as:   where   is the angle between them and   is a unit vector that is perpendicular to both   and  . 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   and the other is  . As they are both pointing in different directions and the magnitude is the same, this means that:  . 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:

 [2]

Normal vectorsEdit

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 vectorEdit

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.

 

 

Rewritten as one equation:

 

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:

  or  

See alsoEdit

External linksEdit

ReferencesEdit