Vectors and Scalars in Game Development
Game development involves some math, but it doesn't need to be difficult, and Unreal Engine 4 gives us a great many good tools for working with the concepts of Vectors, Rotators, Transforms, and the math associated with them. Let us then explore the ideas behind Linear Algebra and how they apply to game programming in Unreal Engine 4 (or any game engine for that matter).
Scalars
Most of us understand scalars, but may not know that they are called that. A scalar is just a number, like 3 or 7 or 4.5, even 𝜋, PI. We use scalars for many calculations all the time, and in most cases, we don't really care that they are scalars. When working with vectors and matrices, however, it is necessary to understand that a scalar is a single value, and not a vector or matrix.
Vectors
A vector is an ordered set of numbers and these numbers represent both a magnitude (like a scalar) and a direction. The direction is the direction in which that magnitude extends. The magnitude may represent distance, and in many cases, this is what we use in 3D graphics, but it may represent other things as well, like force.
![]() |
| A Vector |
This is called a row vector and has 2 elements. Vectors can be plotted as arrows on a graph, like this.
![]() |
| Figure 1 |
The red arrow is a vector [1 2] the green arrow is [1 1] and the blue arrow is [2 1]. These vectors aways begin at the point (0,0) as the X/Y coordinate and extend to the point specified by the vector quantities, in this case, the X and Y locations.
When adding vectors we simply add by element so that given the vectors [1 1] + [1 2] = [2 3], graphically this looks like Figure 2.
![]() |
| Figure 2 |
This stacks the vectors end for end and the resulting vector, [2 3] extend from [0 0] to [2 3] in Figure 3.
The result of adding the two vectors creates a vector that extends from 0,0 to the new point at 2,3 that is the same as if you placed the second vector at the tip of the first vector. This is called translation, as it moves a vector from the 0,0 start location to a new location and plots it from there.
Vector subtraction works the same way as addition such as [2 3] - [1 2] = [1 1], produces the resulting vector [1 1] by simply subtracting each element of the first vector from the corresponding element of the second vector to produce a new vector.
Vectors may be multiplied by a scalar, this has the effect of increasing the length of the vector. A scalar can not change the direction of a vector except to reverse the direction with a negative scalar. As an example the vector [1 1] * 3 = [3 3] but the vector [1 1] * (-3) = [-3 -3].
![]() |
| Figure 3 |
Vector subtraction works the same way as addition such as [2 3] - [1 2] = [1 1], produces the resulting vector [1 1] by simply subtracting each element of the first vector from the corresponding element of the second vector to produce a new vector.
Vectors may be multiplied by a scalar, this has the effect of increasing the length of the vector. A scalar can not change the direction of a vector except to reverse the direction with a negative scalar. As an example the vector [1 1] * 3 = [3 3] but the vector [1 1] * (-3) = [-3 -3].
Dot Product
The dot product has a very specific mathematical description that is more general than we need for our purposes, formal mathematics is not necessary to understand what we can use it for in a 3D world game. The dot product is calculated as the sum of the products of each corresponding element of two vectors of the same size. Technically this is a row vector and a column vector which by the rules of matrix multiplication for a matrix of 1 element (or a scalar). In software generally, we do not distinguish between column and row vectors explicitly, instead, we rely upon the context of the code.
I will use . for the dot product symbol.
Two vectors [a b c] . [d e f] = a * d + b * e + c * f
Which yields a single number (a scalar). This function also has a geometric interpretation This number represents a projection of the second vector along the line of the first vector, which is to say how far along, and in what direction, the vectors travel. A positive value means they go in generally the same direction, a negative value means they go in opposite directions. If the dot product is zero (0), then they are perpendicular.
There are more subtleties to this in mathematics but this is all we are really interested in game development. We want to know when two vectors are near each other or far apart and how this can relate to gameplay.
Unreal Engine 4 has a built-in Dot Product function, so you do not need to perform this calculation, but rather understand why you would use the dot product. This is common in materials for determining things how close a particular face is to directly on the camera or at a steep angle. We will discuss this in more detail in articles about materials in Unreal Engine 4.
Cross Product
The cross product takes two vectors in three-dimensional space (the cross product is not defined for 2 dimensions or less), and returns another vector that is perpendicular to both vectors. This can be very useful when making various decisions about direction in game development.
Unreal Engine also supplies a cross product function, so there is no need to go into how it is computed, just keep in mind that it is used to find perpendiculars.




Comments
Post a Comment