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
Throughout this article, I will use a convention when referring to vectors, scalars, and matrices.
- Scalars are represented by lower-case italic characters ().
- Vectors are represented by lower-case bold characters ()
- Matrices are represented by upper-case bold characters ()
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.
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.
Suppose we have two vectors, vector , and vector . Visually, is equivalent to placing the tail of vector on the head of vector without changing the direction of either vector. Vector subtraction is equivalent to addition of the negative of vector , that is . Visually, we would negate vector then add the vectors the same way – the tail of placed at the head of . The resulting vector is the vector that starts at the tail of vector 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:
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 is equivalent to negating the vector.
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 (), not to be mistaken as the notation used to denote the absolute value of a scalar. The length of a vector is calculated using the following general algebraic rule:
An example of calculating the length of a vector is shown below:
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 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:
- 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).
- 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 doesn’t 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:
The image shows the resulting vector by subtracting point from to get the vector from to .
The vector is negated and the tail of is placed at the head of vector .
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 doesn’t matter if we calculate or , the result will be the same.
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:
An example of calculating the dot product of two vectors:
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:
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 that is parallel to vector is denoted and the part that is perpendicular to is denoted .
The image below shows this graphically:
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 normalization formula. The next part of the formula might be a bit confusing, . You’re 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:
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 is of unit length, then we know that is and the denominator becomes and can be omitted from the equation:
Be careful to only use this form of the equation when we know that is of unit length.
Now that we know how to solve for , we can easily solve for . Using vector addition, we 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 symbol, but it should not be mistaken with scalar multiplication.
The result of the cross product between vector and vector is a vector that is perpendicular to both vectors and as show in the image below:
In this image we see a vector (red) and vector (blue) which lie in the plane with normal (gray). The cross product is perpendicular to both and and parallel to .
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 won’t 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 anti-commutative. 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 camera’s 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.
This is shown in the image below:
If vector and vector are parallel vectors or if either vector or vector is the zero vector, then the result of is the zero vector.
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 right-handed 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 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.
Fletcher Dunn and Ian Parberry (2002). 3D Math Primer for Graphics and Game Development. Wordware Publishing.