### The mathematics involved in 3D game programming - Vector Algebra

This is the second blog in a series on the mathematics involved in 3D game programming.

**Vector Algebra - A Review**

As was mentioned in the previous blog:

- Vectors have both magnitude and direction.
- Vectors are usually represented as a line segment with a starting point, the tail end, and an ending point, an arrowhead.
- Vectors play an important role in computer graphics, collision detection, and physical simulation in 3D programming.
- Vectors are used to represent positions, displacements, directions, velocities, and forces in 3D game programming.

And then there are operations that can be performed on vectors, such as the addition and subtraction of vectors, or the dot product and cross product of vectors.

In this blog article, I will be discussing some of these vector mathematical operations. I will discuss further mathematical vector operations in subsequent blog articles.

Before discussing the mathematical operations, keep in mind that vectors are usually displayed on coordinate systems, whether 2D (with just x and y axes) or 3D (with x, y, and z axes), as can be seen below:

**Vector Location on a Coordinate System**

Something important to understand about vectors - though vectors give magnitude and direction, they don't exactly give location. Let me explain this in more detail. Vectors can be used to represent forces and velocity. Say, for example, a few vectors are used to represent wind velocities of about 5.4 mph in the north-west direction. Such a configuration of vectors could be represented on a 2D coordinate system (x, y axes) as shown below:

Each of the above vectors starts at a specific point and then moves 2 units to the left and 5 units up. The representation for this particular vector, vector v, is **v** = <** **-2, 5 >** **as opposed to** **(-2, 5) which only represents a point itself on the x, y axes coordinate system. To give the representation for a particular vector, say for instance the vector in the above image farthest to the right, then simply subtract the corresponding end points from the starting points. So, for this vector, with starting point (5, -1) and ending point (3, 4), this would be

<3 - 5, 4 - -1>

to give a final vector of <-2, 5>, which matches the original vector **v. **

Before I continue with explaining more about vector algebra, I want to mention an excellent mathematics site that I used quite often while in college. This site, Paul's Online Math Notes, gives the easy-to-understand class notes from professor Paul Dawkins at Lamar University in Texas. Please visit this excellent site if you want to learn more about advanced mathematics typically used for engineering degrees. Some of the vector images and information used in this blog article were obtained from this site.

**Basic Vector Operations - Equality, Addition, Subtraction and Scalar Multiplication**

For explanations of basic vector operations, these vectors from the 3D coordinate system will be used: **a** = < a1, a2, a3 > and **b** = < b1, b2, b3 >

1. Vector equality. Two vectors are equal, **a** = **b** only if their corresponding components are equal, that is a1 = b1, a2 = b2, and a3 = b3.

2. Vector addition. Vectors are added component-wise, and they have to be of the same dimension (both 2D or both 3D), so **a **+ **b** = < a1+b1, a2+b2, a3+b3 >

3. Vector subtraction. Similar to vector addition. **a** -** b**= < a1-b1, a2-b2, a3-b3 >

4. Scalar multiplication. A scalar (of a quantity) is only magnitude, not direction. So, for example, a temperature of 72 degrees F is a scalar quantity, since temperature only has magnitude, not direction. Scalar multiplication of a vector by c, where c is any number,is c**a **= < ca1, ca2, ca3 >

To try out some of the examples of vector operations, visit this page from Paul's site.

**Vector Length, Unit Vectors and the Normalization of a Vector**

To find the length of a vector, the Pythagorean theorem is used (with hypotenuese a), and with a triangle in the xz-plane with sides x, z, and hypotenuse a:

Then, consider a triangle with sides a, y, and hypotenuse ||**u**|| (the double bars is not absolute value, but signifies magnitude or length), and then the formula is obtained thus:

This can be visualized in the image below:

As is mentioned in the book Introduction to 3D Game Programming with DirectX 12 (which was also used as a reference source for this blog article), for many game applications or other applications, the length of the vector is not important; this is because sometimes only the direction is important, in other words a vector that represents pure direction. In this case, the vector length should only be exactly 1. This is referred to as normalizing a vector. The way to normalize a vector is to divide each of its components by its magnitude:

Here is an example taken from Introduction to 3D Game Programming with DirectX 12:

Vector **v **= <-1, 3, 4>. Then, the vector's length is

To normalize this vector:

And then finally, to verify that **v **is now a unit vector, use the length formula to determine its length:

As can be seen, vector **v **is now a length of only 1, i.e. normalized, and therefore is a unit vector, sufficient when direction is only needed in a game program, but not magnitude.

**Vector Dot Product**

The vector dot product is very important. The dot product is primarily used for two main purposes:

1. To determine whether the angle between two vectors is acute (less than 90 degrees) or obtuse (greater than 90 degrees).

2. To determine if the angle between two vectors is 90 degrees (the two vectors are perpendicular each other, or, in other words, orthogonal to each other).

If vector **u **= < u1, u2, u3 > and vector **v **= < v1, v2, v3 >, then the dot product for these two vectors is:

**u** * **v = **u1v1 + u2v2 + u3v3

Notice that this gives a scalar answer (only magnitude, no direction).

But to have the dot product perform the useful purposes mentioned above, a different formula is used:

In other words, find the magnitude (length) of both u and v, then multiply by the cosine of the angle between u and v. But, since the angle is usually unknown, and needed, this form of the formula works much better:

Here is an example of this from Paul's site:

For the next blog article in the series of the mathematics involved in 3D game programming, I will be discussing vector projections and vector cross products. And I will also mention some of the 'hows' and 'whys' vectors and vector operations are greatly needed in 3D programming.