Hello! Welcome to the fourth lesson in our "3D Math Fundamentals" module.
In our last few lessons, we've built a solid understanding of vectors. We've learned how to perform basic arithmetic with them and how to use the dot and cross products to understand their geometric relationships. Most recently, we saw how to calculate a vector's magnitude and normalize it to get a "pure direction" unit vector.
Today, we transition to a new and powerful mathematical tool: the matrix. In the context of game development, matrices are primarily used to perform transformations—that is, to move, rotate, and scale our game objects. This lesson will focus on the fundamental mechanics of how matrices operate. You will learn how to perform the two most important operations: multiplying a matrix by a vector to transform it, and multiplying a matrix by another matrix to combine transformations.
Introduction to Matrices
From your chemical engineering studies, you have likely encountered matrices as a way to represent and solve systems of linear equations. In 3D graphics, we use them for a different but related purpose: to represent geometric transformations.
A matrix is simply a rectangular grid of numbers, arranged in rows and columns. A matrix with m rows and n columns is called an m x n matrix.
Introduction to Matrices - 3D Math Primer for Graphics and Game ...
Let's start with a formal definition from our core reference text, '3D Math Primer for Graphics and Game Development'. This will establish the basic terminology.
Please read the introduction and Section 4.1.1, 'Matrix Dimensions and Notation'. This will only take a few minutes and will define what a matrix is and how we describe its size.
Matrix-Vector Multiplication: Transforming a Point
The first essential operation is multiplying a matrix by a vector. This is the mechanism we use to apply a transformation to a point or a direction in space. For example, if we have a matrix that represents a 45-degree rotation, multiplying a vector by this matrix will produce a new vector that is rotated by 45 degrees.
Let's start with the intuition behind this.
Matrices and Transformations - Math for Gamedev
The video 'Matrices and Transformations' provides a great geometric intuition for what happens when you multiply a matrix by a vector.
Watch the clip from 02:08 to 02:55. Notice how the matrix is constructed by seeing where the basis vectors (the axes) end up after a transformation. Multiplying a vector by this matrix then calculates where that vector ends up in the new system.
Now for the mechanics. A vector can be thought of as a matrix with only one column. For a multiplication to be valid, the number of columns in the matrix must equal the number of rows in the vector (which is its number of components).
The calculation itself is a series of dot products. To find the first component of the resulting vector, you take the dot product of the first row of the matrix and the vector. For the second component, you use the second row, and so on.

Let's formalize this with another reading.
The LearnOpenGL tutorial series explains this from a practical standpoint, framing a vector as an N-by-1 matrix.
Please read the short section titled 'Matrix-Vector multiplication'. It clearly states the idea that a vector is just a matrix with one column, which is why the rules of matrix multiplication apply.
Matrix-Matrix Multiplication: Combining Transformations
The real power of matrices comes from the ability to multiply them together. If you have a matrix for rotation (R) and a matrix for scaling (S), you can multiply them to get a single matrix (M = R * S) that performs both rotation and scaling in one operation. This is incredibly efficient for a game engine.
The Golden Rule of Matrix Multiplication
You can only multiply two matrices, A and B, if the number of columns in matrix A is equal to the number of rows in matrix B.
If A is an m x n matrix and B is an n x p matrix, their product C = A * B will be an m x p matrix.
The calculation is an extension of what we just learned. To find the element in row i and column j of the result matrix C, you compute the dot product of row i from matrix A and column j from matrix B.
The LearnOpenGL resource has one of the clearest visual explanations of this process.
Read the section 'Matrix-matrix multiplication'. Focus on the color-coded diagram that walks you through how the rows of the first matrix combine with the columns of the second matrix. Try to follow the larger example calculation at the end of the section.
Key Properties of Matrix Multiplication
There are two crucial properties you must remember:
-
It is NOT commutative:
A * B ≠ B * A. The order in which you multiply matrices matters immensely. Applying a rotation and then a translation is not the same as applying a translation and then a rotation. You should read transformations from right to left: in the expressionM = T * R * v, the vectorvis first rotated (R), and the result is then translated (T). -
It IS associative:
(A * B) * C = A * (B * C). This is what allows us to pre-calculate a combined transformation matrix (M = A * B) and then apply it to many vectors, which is a fundamental optimization in graphics.
Matrices and Transformations - Math for Gamedev
Let's return to the 'Matrices and Transformations' video, which demonstrates these properties very clearly.
Watch from 09:34 to 13:53. The first part (until 12:17) explains how multiplying matrices combines transformations. The second part (from 12:17 onwards) covers the critical rules, especially the importance of multiplication order.
Row vs. Column Vectors: An Important Convention
You will find that some math books and graphics APIs (like DirectX) write matrix-vector multiplication as v' = v * M, where v is a row vector. Others (like OpenGL, which we will use) write v' = M * v, where v is a column vector.
This choice affects how the matrices themselves are written (one is the transpose of the other) and the order in which you multiply them to combine transformations.
- Row Vector Convention (v * M): Transformations are applied left-to-right.
v' = v * S * R * Tmeans scale, then rotate, then translate. - Column Vector Convention (M * v): Transformations are applied right-to-left.
v' = T * R * S * vmeans scale, then rotate, then translate.
This is a frequent source of bugs for developers. Since our goal is to build an engine that will eventually interface with OpenGL, we will primarily use the column vector convention.
Introduction to Matrices - 3D Math Primer for Graphics and Game ...
The '3D Math Primer' text has an excellent discussion on this topic. Understanding this now will save you a lot of headaches later.
Read Sections 4.1.7 ('Multiplying a Vector and a Matrix') and 4.1.8 ('Row versus Column Vectors'). This will solidify the mechanical differences and explain the rationale behind each convention.
From Math to Code
Understanding the algorithm is key to implementing it yourself. Let's look at how the row-by-column multiplication logic translates into code.
Matrix Multiplication for 3D Rendering
This video from 'The Coding Train' implements matrix multiplication from scratch. While the language is Java/Processing, the syntax is very similar to C# and the core logic is identical to what you would write in C.
Watch from 01:39 to 05:12 to see the row-column multiplication explained, then skip to 11:20 and watch until 13:53. Focus on the triple nested loop structure used for the calculation. This is the classic algorithm for matrix multiplication: for i in rows(A) for j in columns(B) sum = 0 for k in columns(A) sum += A[i][k] * B[k][j] result[i][j] = sum
Practice
Let's try a simple 2x2 matrix multiplication by hand. Given:
Calculate C = A * B.
Click to see the solution
We need to calculate the four elements of the 2x2 result matrix C.
-
C_11 (row 1, col 1): Dot product of A's 1st row and B's 1st column.
(3 * 5) + (-1 * -2) = 15 + 2 = 17 -
C_12 (row 1, col 2): Dot product of A's 1st row and B's 2nd column.
(3 * 0) + (-1 * 7) = 0 - 7 = -7 -
C_21 (row 2, col 1): Dot product of A's 2nd row and B's 1st column.
(2 * 5) + (4 * -2) = 10 - 8 = 2 -
C_22 (row 2, col 2): Dot product of A's 2nd row and B's 2nd column.
(2 * 0) + (4 * 7) = 0 + 28 = 28
So, the resulting matrix is:
For more practice, you can attempt the problems in Exercise 4 of the "3D Math Primer" reading.
Conclusion
You've now learned the essential mechanics of matrix multiplication, a cornerstone of 3D graphics. While you'll eventually use a library like GLM to handle these calculations in C++, understanding the underlying algorithm is crucial for debugging and for building your engine from the ground up.
Key Takeaways:
- A matrix is a grid of numbers used to represent transformations.
- Matrix-vector multiplication (
M * v) applies a transformation to a point. The result is a new, transformed vector. - Matrix-matrix multiplication (
A * B) combines two transformations into a single new one. - For multiplication to be valid, the number of columns in the first matrix must equal the number of rows in the second.
- The order of multiplication is critical:
A * Bis not the same asB * A.
In our next lesson, we will put this knowledge to use. We will stop focusing on the how of multiplication and start exploring the what—we will construct the specific matrices used for identity, translation, scaling, and rotation.