Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 14

Matrix Multiplication for Combined Transformations

Recommended resources

Hello! Welcome to your next lesson in the "3D Math Fundamentals" module.

In our last lesson, we built our foundational toolkit of transformation matrices. You learned how to construct individual 4x4 matrices for identity, scaling, rotation, and translation, and you explored the crucial concept of homogeneous coordinates that makes translation via matrix multiplication possible.

Today, we'll learn how to combine these individual transformations. In a game, an object doesn't just get scaled, or just rotated, or just translated; it gets all three to place it correctly in the world. Instead of applying three separate matrices to every vertex of a model, we can combine them into a single, efficient matrix. This lesson is all about that process and, most importantly, the critical rule that governs it: the order of multiplication matters.

The Composition of Transformations

Applying one transformation after another is called composition. For example, rotating an object and then moving it results in a new, single transformation that captures the combined effect. In linear algebra, the way we compose transformations is through matrix multiplication.

If we have two transformation matrices, M1 (e.g., a rotation) and M2 (e.g., a translation), the combined transformation matrix M_combined is their product:

M_combined = M2 * M1

When you apply this combined matrix to a vector v, you are effectively applying M1 first, and then M2 to the result.

M_combined * v = (M2 * M1) * v = M2 * (M1 * v)

This is just like function composition in mathematics, which you might have seen as f(g(x)), where g(x) is applied first. Because of this, we read the sequence of transformations in a matrix multiplication from right to left.

Matrix multiplication as composition | Chapter 4, Essence of linear algebra

The concept of matrix multiplication as function composition is one of the most elegant ideas in linear algebra. The video 'Matrix multiplication as composition' from the 3Blue1Brown series 'Essence of linear algebra' provides an excellent visual intuition for this.

Watch from 02:04 to 04:31. Focus on how applying two transformations in sequence results in a new, single transformation, and how this directly relates to the right-to-left order of matrix multiplication.


Why Order Is Everything

Since matrix multiplication is not commutative (in general, A * B ≠ B * A), the order in which we multiply our transformation matrices has a dramatic impact on the final result.

Let's consider a practical example:

  1. Scenario A: First, rotate a cube around the Y-axis. Second, translate it along the X-axis.
  2. Scenario B: First, translate a cube along the X-axis. Second, rotate it around the Y-axis.

These sound similar, but the outcomes are completely different.

  • In Scenario A, the cube rotates around its own center, and then the already-rotated cube moves to its final position. This is usually the behavior we want for placing an object in a game world.
  • In Scenario B, the cube first moves away from the origin, and then it rotates. Because the rotation happens around the world's origin (0,0,0), the cube will appear to orbit the center of the world, not spin in place.

Matrices and Transformations - Math for Gamedev

To see this in action, the 'Matrices and Transformations' video by FloatyMonkey has a great segment that visually demonstrates why order matters.

Watch the segment from 09:34 to 13:53. The video uses a shear and a rotation, but the principle is identical. Notice how Rotation * Shear produces a completely different result from Shear * Rotation.

To solidify this, let's look at a clear written explanation with a more direct game development example.

Camera & Transformations: Mathematical Foundations

This document from the Vulkan tutorial series, 'Camera & Transformations: Mathematical Foundations', provides a concise and practical explanation of this exact concept.

Read the short section titled 'Matrix Order Matters'. It provides a perfect illustration of the 'rotate-then-translate' versus 'translate-then-rotate' problem.


The Standard Order: Scale, Rotate, Translate (SRT)

Given that order is so important, there is a standard convention for transforming a model from its own local space (centered at the origin) into the game world. The sequence is almost always:

  1. Scale: Adjust the size of the model.
  2. Rotate: Orient the model correctly.
  3. Translate: Move the model to its final position in the world.

Why this order?

  • You scale first so that the rotation and translation apply to the correctly-sized object.
  • You rotate before you translate to ensure the object spins around its own center, not the world origin.

Following the right-to-left rule of matrix multiplication, the combined matrix, often called the Model Matrix or World Matrix, is built like this:

Model Matrix = Translation Matrix * Rotation Matrix * Scale Matrix

When you apply this to a vertex v, the operations happen in the correct SRT order:

Transformed Vertex = (T * R * S) * v = T * (R * (S * v))

Transformations - LearnOpenGL

The LearnOpenGL article on transformations discusses how to combine matrices and provides the reasoning for the conventional order.

Read the section 'Combining matrices'. The text provides an example of multiplying a translation and a scale matrix. Pay close attention to its note: 'when multiplying matrices the right-most matrix is first multiplied with the vector so you should read the multiplications from right to left.' This confirms the principle we've been discussing.


From Theory to Code

Now, let's see how this looks in practice. Although we're focusing on C for this course, most graphics math libraries (like the popular C++ library GLM) follow this pattern.

You would typically start with an identity matrix and then multiply it by each transformation matrix in the reverse order of application.

// This is a C++ example using the GLM library, but the concept is universal.

// 1. Define the individual transformations
glm::mat4 trans_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(10.0f, 0.0f, 0.0f)); // Translate 10 units on X
glm::mat4 rot_matrix = glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // Rotate 45 deg on Y
glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(2.0f, 2.0f, 2.0f)); // Scale by 2x

// 2. Combine them into a single model matrix (T * R * S)
glm::mat4 model_matrix = trans_matrix * rot_matrix * scale_matrix;

// 3. Apply the combined matrix to a vertex in the shader
// transformed_vertex = model_matrix * original_vertex;

Another common way libraries handle this is by chaining the operations. Notice how each function takes the previous matrix as an argument, effectively building the product step-by-step.

// Chained approach, building the T*R*S matrix
glm::mat4 model_matrix = glm::mat4(1.0f); // Start with identity
model_matrix = glm::translate(model_matrix, glm::vec3(10.0f, 0.0f, 0.0f)); // model = T * I
model_matrix = glm::rotate(model_matrix, glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // model = R * (T * I)
model_matrix = glm::scale(model_matrix, glm::vec3(2.0f, 2.0f, 2.0f)); // model = S * (R * T * I)

Wait, look closely at that second example. The final matrix is S * R * T. This is the wrong order! This would translate, then rotate, then scale. This is a common pitfall. The correct way to chain it would be to apply the operations in reverse:

// Correct chained approach (produces T*R*S)
glm::mat4 model_matrix = glm::mat4(1.0f); // Start with identity
model_matrix = glm::scale(model_matrix, glm::vec3(2.0f, 2.0f, 2.0f));       // model = S * I
model_matrix = glm::rotate(model_matrix, glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // model = R * (S * I)
model_matrix = glm::translate(model_matrix, glm::vec3(10.0f, 0.0f, 0.0f)); // model = T * (R * S * I)

This final model_matrix now contains all the information needed to scale, rotate, and translate a vertex in one single, efficient multiplication.


Conclusion

This lesson connected the individual matrices from our last session into a powerful, practical tool. You now understand not just how to combine them, but why the order of combination is one of the most important rules in 3D graphics programming.

Key Takeaways:

  • Applying multiple transformations in sequence is called composition.
  • Composition is achieved by multiplying the corresponding transformation matrices.
  • Matrix multiplication is not commutative, so order is critical.
  • Transformations are applied from right to left. The expression T * R * S means "first apply Scale, then Rotate, then Translate."
  • The standard order for a model matrix to position an object in the world is Scale, then Rotate, then Translate.

In the next lesson, we will revisit homogeneous coordinates. We've been using them all along (that 4th w component!), but we'll explore why they are so fundamental, not just for translation, but for creating the illusion of perspective in a 3D world.