Vector Math

From WikiCoder

Vectors are essentially a line (or otherwise a direction) in space, originating from a point 0,0,0. They are described as a point in space, usually in two (2D) or three coordinates (3D), such as 3,2,4.

A 3D vector is usually described in a format of x,y,z. Similarly, a 2D vector may be described in a format of x,y.


To calculate the direction from two vector points, subtract the first point A from the second point B. The remaining vector will be a direction from A to B.


Vector3 Direction = PointB - PointA;


Magnitude or Distance is a calculation of the length of a vector, in essence it's distance from 0,0,0. The formula is the square root of Pythagorean theorem.


float Magnitude = Mathf.Sqrt( (inputVector3.x * inputVector3.x) + (inputVector3.y * inputVector3.y) + (inputVector3.z * inputVector3.z) );

Normalized Vectors

Normalizing means taking the length (or Magnitude) of a vector and dividing each component by it.

Continue reading about Sphere Projection.

Dot Product

Dot Product calculates the difference between two vectors. With a normalized vector as an input, it will have values in the range -1 to 1, with 1 meaning that the two vectors are pointing in the exact same direction and -1 means that they are pointing in opposite directions, while a value near 0 means that they are perpendicular at a 90° angle.

float DotProduct = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);

Calculating Angle Between Two Vectors

Use the acosine function on the dot product to calculate the angular difference (in Radians) between these two vectors.

float angularDifference_inRadians = Mathf.Acos( DotProduct );

Cross Product

Cross Product is used to get a vector that is perpendicular to two other vectors (A and B). Vectors A and B form a plane, and the Vector that is perpendicular to both A and B is the normal to that plane..

Hand Rule

Finding the direction of the cross product by the right-hand rule.

The 1st vector in the cross product = your index finger
The 2nd vector in the cross product = your middle finger
Then first vector * second vector = your thumb


Vector3 CrossProduct = new Vector3( (a.y * b.z - a.z * b.y), (a.z * b.x - a.x * b.z), (a.x * b.y - a.y * b.x) );


Sphere Projection

To project a vector onto a sphere (also known as Normalization), we divide each component of an input vector by the vector's length (or Magnitude). This amounts to a sphere around 0,0,0, where the length of the vector is always equal to 1.

float magnitude = Magnitude(inputVector);
if(magnitude > 0)
     Vector3 normalizedVector = inputVector / magnitude;

Box Projection

To project a vector onto a box, we first find the maximum value of all components in the vector. Then we must get the absolute value of that maximum, and then divide each component by that maximum value. If we do not obtain an absolute value of the maximum, the division would reverse the vector if the maximum were a negative number.

This effectively projects the vector onto a box between -1,-1,-1 and 1,1,1.

float max = Mathf.Max(inputVector);
max = Mathf.Abs(max);
return inputVector / max;

Cone Projection

Plane Projection

To project a vector onto a plane, we can use the plane's normal to determine the plane's direction.

float sqrNormal = DotProduct(planeNormal, planeNormal);
if (sqrNormal == 0)
     return vector;
     var dot = DotProduct(vector, planeNormal);
     return new Vector3(vector.x - planeNormal.x * dot / sqrNormal,
                        vector.y - planeNormal.y * dot / sqrNormal,
                        vector.z - planeNormal.z * dot / sqrNormal);