In part one of this tutorial I explained what a vector is and described some of their basic operations. This tutorial will delve deeper into vectors and explain the more useful operations that vectors can be used for. First, lets see how we can find the length of a vector.

If you are not familiar with the Pythagorean theorem, please read

this article. It is important that you are familiar with this theorem because finding the length of a vector is based on it.

This picture shows a 2-dimensional vector with red lines overlaid to show the x and y displacement. From this picture you should be able to infer how the length is obtained. You square the x and y term of the vector and then take the square root of their sum. In pseudo code it would look like this:

sqrt( x*x + y*y)

Or, in three dimensions:

sqrt (x*x + y*y + z*z)

The length of the vector is sometimes called the norm. If we have a vector A, you will often see the norm of vector A notated as ||A|| or just |A|.

There is a special class of vectors that will be VERY important to us. These are called unit length vectors and any vector that is unit length is said to be normalized. In order for a vector to qualify as unit length it must have a length of exactly 1. We can normalize any vector by first finding its length and dividing each component of the vector by the length (norm). In three dimensions it would look like this:

length = sqrt(x*x + y*y + z*z)

x = x / length

y = y / length

z = z / length

If the vector were only 2-dimensional you would take out the z component.

We will now discuss the most important and widely used operation on vectors in all of game programming - the dot product. Given two vectors A and B, we define the dot product of A and B as ||A||||B||cos theta. Theta is simply the angle between the two vectors. The dot product will return a scalar value between 1 and -1. We obtain the dot product by simply multiplying the two vectors' components. So, if we have vectors A and B that are 2-dimensional vectors, we find the dot product like so:

A.x*B.x + A.y*B.y

If the dot product of A and B is greater than zero, then we know the angle is less than 90 degrees. If the dot product is equal to zero then we know the two vectors are perpendicular or orthogonal to one another. If the scalar value returned is less than zero, we know the angle is greater than 90 degrees. If you need the actual angle between the two vectors, take the inverse cosine of the scalar value returned by the dot product.

We will now move on to the perpendicular product and the cross product; two similarly related concepts. For a vector A in 2-dimensions, there are two vectors that are exactly perpendicular to that vector. This picture will help demonstrate.

The green vector is vector A. We say that the blue vector is the left hand normal of vector A and the red vector is the right hand normal of vector A. If we were to take the dot product of vector A and the left hand normal or the right hand normal, it would be exactly 0. For any 2-dimensional vector, to find the left hand normal switch the x and y components and negate the x component. In pseudo code it would look like this:

left_normal.x = vector.y

left_normal.y = -vector.x

The right hand normal is similar:

right_normal.x = -vector.y

right_normal.y = vector.x

The cross product is similar to the perpendicular product but it is used in 3-dimensions. Given three points: A, B, C, we can construct 2 vectors from these points. We will name the vectors V1 and V2. The following diagram will help in my explanation.

Vector V1 in constructed by taking the point B and subtracting it from A. Vector V2 is constructed the same way except by taking the point C and subtracting it from A. Similar to the perpendicular product, we can get two different normals that will be orthogonal to the plane constructed from the original 3 points. It is common to face the normal out of the plane. We calculate the normal vector like so:

normal.x = (V1.y * V2.z) - (V2.y * V1.z)

normal.y = (V1.z * V2.x) - (V2.z * V1.x)

normal.z = (V1.x * V2.y) - (V2.x * V1.y)

The normal vector is represented in the picture by the blue vector which is facing out. The cross product is usually written as normal = V1 x V2.

We now move onto our last topic which is vector projection. Have a look at this picture:

Lets say the red vector is vector A and the green vector is vector B. We define the projection of A onto B as:

Projection = Dot(A, B) / (||B|| * ||B||) * B

The projection vector will be parallel to vector B. Notice the projection drops a

perpendicular onto B from A. If vector B is normalized we can reduce the projection vector calculation to:

Projection = Dot(A, B) * B

This is the end of this tutorial. If you have read over both tutorials you should have a good understanding of vectors and be able to start applying them to your game projects.

Back to Vectors - part 1
Back to Main Page