You are on page 1of 16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

3D Game Engine Programming


Helping you build your dream game engine.

3D Math Primer for Game Programmers (Vector Operations)


Posted on February 4, 2011 by Jeremiah van Oosten

Distance between two points (3 )

In this article I would like to discuss operations on vectors. This article assumes the reader has a basic knowledge of what vectors are and how they are represented. My goal here is simply to refresh your memory about what kind of operations can be performed on vectors specifically operations that are important if you are trying to create a 3D game engine.

Table of Contents Conventions Vector Operations Vector Negation Vector Addition and Subtraction
3dgep.com/?p=359 1/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Vector Multiplication by a Scalar Magnitude of a Vector Normalizing Vectors Distance Between two Points Vector Dot Product Vector Projection Vector Cross Product References

Conventions Throughout this article, I will use a convention when referring to vectors, scalars, and matrices. 1. Scalars are represented by lower-case italic characters ( 2. Vectors are represented by lower-case bold characters ( 3. Matrices are represented by upper-case bold characters ( Vector Operations There are several useful operations that can be performed on vectors. These operations include negating a vector, adding two vectors, subtracting two vectors, calculating the length (or magnitude) of a vector, calculating the distance between two vectors and normalizing a vector. Other operations on vectors that might not be immediately obvious are calculating the dot product between two vectors and calculating the cross-product between two vectors. Vector Negation To negate a vector, we simply negate ervery component of the vector. Negating a vector, results in a vector of the same magnitude, but opposite in direction. A few examples of vector negation are: ) ) ).

Vector Addition and Subtraction We can perform vector addition and vector subtraction on two vectors of the same dimension. To perform vector addition and subtraction, we simply add or subtract each component of the first vector with the matching component of the second vector.

3dgep.com/?p=359

2/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Suppose we have two vectors, vector placing the tail of vector

, and vector

. Visually,

is equivalent to , that is

on the head of vector

without changing the direction of either

vector. Vector subtraction is equivalent to addition of the negative of vector . Visually, we would negate vector way the tail of at the tail of vector placed at the head of

then add the vectors the same

. The resulting vector is the vector that starts .

and ends at the head of vector

Some example of vector addition and subtraction are:

Personally, I have trouble remembering what order I have to subtract vectors in order to get the vector in the correct direction. I use the following mnemonic to help me remember: The vector from a to b is b minus a. The following images show examples of adding and subtracting vectors:

3dgep.com/?p=359

Vector addition and subtraction


3/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Vector Multiplication by a Scalar Vectors can also be multiplied by a scalar. Scalar division is also supported, but this is equivalent to multiplying the vector by the reciprocal of the scalar. Vector multiplication by a scalar is show below:

Multiplying a vector by a positive scalar does not change the direction of the vector, it only changes the magnitude of the vector. Multiplying by a negative number will inverse the direction of the vector and scale the magnitude. Multiplying a vector by negating the vector. is equivalent to

Magnitude of a Vector The magnitude of a vector, also called the length, or the norm of the vector is a scalar and is represented using the double vertical bars on either side of the vector variable ( is calculated using the following general algebraic rule: ), not to be

mistaken as the notation used to denote the absolute value of a scalar. The length of a vector

An example of calculating the length of a vector is shown below:

Normalizing Vectors It is sometimes useful to express vectors only by their direction and not by their length. This is necessary for example when we want to find out if another object, or point is in front of or
3dgep.com/?p=359 4/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

behind our reference frame, or when we need to calculate a reflection vector that would occur from a surface with a upward facing plane. Vectors of unit length are also called normals. The normal of a vector can be calculated using the following equation:

In words, the normalized vector is the vector divided by the magnitude of itself. This results in a vector of unit length. We must take care when calculating the normalized vector because zero length vectors cannot be normalized. Normalizing a zero-length vector will usually result in a divide-by-zero error. Usually we resolve this by performing the normalization in multiple steps: 1. Calculate the length squared of the vector (only calculate the squared length of the vector because we only need the square root if the squared length is not zero). 2. If the squared length is greater than zero, then calculate the square root of that and multiply the vector components by the reciprocal of the length. Distance Between two Points Recall from the Vector Addition and Subtraction section that I stated the mnemonic the vector from a to b is b minus a? Well this is where this comes in handy. When we want to know the vector that goes from one point to another, we visualize the two points and as vectors from the origin and when we subtract them, the result is another vector which doesnt necessarily describe a point in space, but rather a direction from one point to the other. The images below shows an example of using vector subtraction to calculate a vector from one point in space to the other:

3dgep.com/?p=359

5/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

The distance between two points (1 )

The image shows the resulting vector from to .

by subtracting point

from

to get the vector

Distance between two points (2 )

The vector

is negated and the tail of

is placed at the head of vector

3dgep.com/?p=359

6/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Distance between two points (3 )

The resulting vector is a vector from the origin to the head of vector

To get the distance between these two points, we simply calculate the magnitude of the resulting vector .

Notice that if the only thing we need to calculate is the distance between the two points, it doesnt matter if we calculate Vector Dot Product The result of a dot product on two vectors is a sum of the products of the matching components of each vector. Dot products are defined on vectors of all dimensions, but both vectors must have the same dimension (same number of components) to calculate a valid dot product. The result of a dot product is always a scalar. The general form for the dot product rule is: or , the result will be the same.

An example of calculating the dot product of two vectors:


3dgep.com/?p=359 7/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

You may have noticed that if you calculate the dot product of a vector on itself give the squared distance of the vector. The dot product of two vectors is equal to the magnitude of the vectors multiplied by the cosine of the angle between them.

We can solve for

if we rearrange the equation:

We can further simplify the equation if we assume that both

and

are of unit-length (that is

to say that the vectors are normalized). In such a case, the denominator becomes 1:

If we are only interested in the position of an object relative to another, we only need to calculate the dot product. The following table shows the result of the dot product under specific conditions:

and relative to eachother vector and vector are pointing in the same direction. vector and vector are perpendicular to eachother. vector and vector are pointing in opposite directions.
3dgep.com/?p=359 8/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Result of the dot product in specific cases.

Vector Projection It is sometimes useful to know how much of one vector is parallel to another, and how much of a vector is perpendicular to another. The part of the vector denoted and the part that is perpendicular to is denoted that is parallel to vector . is

The image below shows this graphically:

Vector Projection

Using what we know about the dot product show in the previous example, we can solve for

I think you will recognize the first part of the equation,

. This is simply the vector . Youre

normalization formula. The next part of the formula might be a bit confusing,

probably thinking how can we know the length of the vector that we are trying to calculate?. Well, this is easy if we remember back from our trigonometry class the simple trigonometric ratio for right-angle trangles:

3dgep.com/?p=359

9/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

In this case, the hypotenuse is

, and the adjacent is

Re-writing our equation, we get:

So now we can express

in terms of things we know. Substituting the left side of the

previous equation back into our original formula gives:

We can re-write the equation and multiply both the numerator and denominator by

to get:

Now we see that the numerator of our fraction looks like the dot product shown earlier, which we can easily solve. So substituting the numerator for the dot product equation gives:

If we know that the vector becomes

is of unit length, then we know that

is

and the denominator

and can be omitted from the equation:

Be careful to only use this form of the equation when we know that Now that we know how to solve for
3dgep.com/?p=359

is of unit length.

, we can easily solve for

. Using vector addition, we


10/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

know that:

Subtracting

from both sides gives:

And substituting

for the previous equation:

Vector Cross Product Unlike the dot product, the cross product is only defined for three-dimensional vectors. In addition, the result of the cross product is a vector as opposed to the result of a dot product is a scalar. The cross product is written using the multiplication. symbol, but it should not be mistaken with scalar

The result of the cross product between vector to both vectors and

and vector

is a vector that is perpendicular

as show in the image below:

3dgep.com/?p=359

Vector Cross Product


11/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

In this image we see a vector (gray). The cross product

(red) and vector

(blue) which lie in the plane with normal and and parallel to .

is perpendicular to both

The equation for the cross product is given by:

If the dot product and the cross product are used together, then the cross product must be calculated first, as in the formula . This is obvious if you try to cross a vector with the result of a dot product, which is a scalar, it simply wont work. This form of taking the dot product of a cross product is known as the triple product and will be discussed further in the article about matrices. It is useful to note that the cross product is not commutative, in fact, the cross product is anticommutative. That is to say that if we reverse the operands, the resulting vector is negated:

. The cross product is also not associative:

The result of a cross product is a vector which is perpendicular to both vectors, even if the original vectors are not perpendicular to each other. This is a useful property of the cross product and can be exploited when we need to find a vector basis where all three axes are perpendicular to each other, for example when we need to othogonolize the basis vectors for a cameras view matrix. This will be explained in more detail in the article on matrices. Another useful property of the cross product is that the magnitude of the cross product is equal to the product of the magnitudes of the two vectors and the sine of the angle between them. The expression of this is shown below:

It is also true that the magnitude of the cross product is the positive area of the parallelogram ( ) formed by the edges of the two vectors.

3dgep.com/?p=359

12/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

This is shown in the image below:

Area of a Parallelogram

If vector

and vector

are parallel vectors or if either vector is the zero vector.

or vector

is the zero vector,

then the result of

If we say the vectors

are the vectors that form the orthogonal basis for our coordinate

space, then the cross product satisfies the following identities:

And if we swap the order of the vectors for the cross product, the result will be negated:

It is also useful to note, that when switching from a left-handed coordinate system and a righthanded coordinate system, one of the axes is inverted (traditionally the z-axis is inverted). This means that if in a left-handed coordinate system the result of the cross product on the
3dgep.com/?p=359 13/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

unit basis vectors

This means that in a left-handed coordinate system, the result of the cross product will point away from the viewer (into the screen), but in a right-handed coordinate system, the result of the same cross product will point towards the viewer (out of the screen). This is a by-product of the handedness of the coordinate system. References

3 D Math Prim er for Graphics and Gam e Dev elopm ent

Fletcher Dunn and Ian Parberry (2002). 3D Math Primer for Graphics and Game Development. Wordware Publishing.

Coordinate Systems

Matrices

Post to Facebook 4 4
Like

4 4 4 4

3dgep.com/?p=359

14/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

You may also like:

Introduction to OpenCL

Understanding Quaternions

Understanding the View Matrix

Rendering Primitives with OpenGL

3D Game Engines

Environment Mapping with Cg and OpenGL

This entry was posted in Graphics Programming, Math, Programming and tagged cross product, distance , dot product, length, math, normal, normalization, primer, scalar, vector, vector operations, vectors by Jeremiah van Oosten. Bookmark the permalink [http://3dgep.com/?p=359] .
5 THOUGHTS ON 3D MATH PRIMER FOR GAME PROGRAMMERS (VECTOR OPERATIONS)

Ben Retan on February 28, 2013 at 11:13 pm said:

Thank you so much! I was looking for a good simple description of the Look At algorithm and none of the other sites I found provided such a clear and simple explanation. Spent 4 hours trying to debug why mine didnt work and couldnt figure it out until I found this article.

Franky on April 4, 2013 at 8:33 pm said:


3dgep.com/?p=359 15/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

that would occur from a serface with a upward facing plane spelling at surface (feel free to delete this comment)

Jeremiah van Oosten on April 5, 2013 at 10:08 am said:

Thanks for pointing this out! It has been fixed.

Pankaj on April 23, 2013 at 4:58 pm said:

The result of the cross product between vector a and vector b is a vector that is parallel to both vectors as show in the image below: Plz change parallel to perpendicular.

Jeremiah van Oosten on April 28, 2013 at 1:51 pm said:

Thanks for letting me know. This has been fixed.

3dgep.com/?p=359

16/16