TExcelTAD
Home › Math for Games › Matrices and Multiplication

Matrices and Multiplication

A matrix is a grid of numbers that describes a transformation: rotate, scale, shear, mirror, and (with one trick, in two lessons) move. The reason games use them is not that they are clever. It is that many transformations combine into one matrix, and one matrix is cheap to apply to thousands of vertices.

A matrix is a set of axes

The cleanest way to read a matrix: its columns are where the axes go. A 2×2 matrix

text
| a  b |
| c  d |

sends the x axis (1, 0) to its first column (a, c) and the y axis (0, 1) to its second column (b, d). Any vector (x, y) is "x of the first column plus y of the second":

text
M * (x, y) = x * (a, c) + y * (b, d) = (a*x + b*y,  c*x + d*y)

Each output component is the dot product of a row with the vector. Both views are the same arithmetic.

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec2 { double x, y; };

// m[row][col]
struct Mat2 {
    double m[2][2];
};

Vec2 operator*(const Mat2& M, Vec2 v) {
    return {M.m[0][0] * v.x + M.m[0][1] * v.y,
            M.m[1][0] * v.x + M.m[1][1] * v.y};
}

int main() {
    std::cout << std::fixed << std::setprecision(1);
    Mat2 stretch{{{2, 0},
                  {0, 0.5}}};     // x axis -> (2, 0), y axis -> (0, 0.5)
    Mat2 shear{{{1, 1},
                {0, 1}}};         // y axis leans over to (1, 1)
    Mat2 mirror{{{-1, 0},
                 {0, 1}}};        // flip x

    Vec2 p{3, 2};
    Vec2 a = stretch * p, b = shear * p, c = mirror * p;
    std::cout << "stretch: (" << a.x << ", " << a.y << ")\n";
    std::cout << "shear:   (" << b.x << ", " << b.y << ")\n";
    std::cout << "mirror:  (" << c.x << ", " << c.y << ")\n";
}
stretch: (6.0, 1.0)
shear:   (5.0, 2.0)
mirror:  (-3.0, 2.0)

Identity

The identity matrix has 1s on the diagonal and 0s elsewhere. It leaves every vector unchanged, like multiplying a number by 1. Every transform in an engine starts as identity.

Multiplying matrices: combining transforms

A * B is a single matrix that does B first, then A (with column vectors, the matrix nearest the vector acts first). Entry (r, c) of the product is the dot product of row r of A with column c of B.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Vec2 { double x, y; };
struct Mat2 { double m[2][2]; };

Vec2 operator*(const Mat2& M, Vec2 v) {
    return {M.m[0][0] * v.x + M.m[0][1] * v.y, M.m[1][0] * v.x + M.m[1][1] * v.y};
}

Mat2 operator*(const Mat2& A, const Mat2& B) {
    Mat2 R{};
    for (int r = 0; r < 2; r++)
        for (int c = 0; c < 2; c++)
            for (int k = 0; k < 2; k++)
                R.m[r][c] += A.m[r][k] * B.m[k][c];
    return R;
}

Mat2 rotation(double a) {
    double c = std::cos(a), s = std::sin(a);
    return {{{c, -s}, {s, c}}};
}
Mat2 scale(double sx, double sy) { return {{{sx, 0}, {0, sy}}}; }

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
void print(const char* name, Vec2 v) { std::cout << name << "(" << z0(v.x) << ", " << z0(v.y) << ")\n"; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Mat2 R = rotation(std::numbers::pi / 2);     // 90 degrees
    Mat2 S = scale(3, 1);                        // stretch x only
    Vec2 p{1, 0};

    print("R * (S * p) = ", R * (S * p));
    print("(R * S) * p = ", (R * S) * p);        // same thing, one matrix
    print("(S * R) * p = ", (S * R) * p);        // different order, different result
}
R * (S * p) = (0.000, 3.000)
(R * S) * p = (0.000, 3.000)
(S * R) * p = (0.000, 1.000)

R * S stretches first, then rotates: the stretched point ends up at (0, 3). S * R rotates first (so the point is now on the y axis), then stretches x, which does nothing to it. Matrix multiplication is not commutative, and "which transform happens first?" is the question to ask whenever an object ends up in the wrong place.

Multiplying is associative

Although order matters, grouping doesn't: (A * B) * C == A * (B * C). That is what makes combining worthwhile. An engine computes projection * view * model once per object and then applies that single matrix to every vertex, instead of three matrices per vertex.

cpp · try it yourself
#include <iostream>

struct Mat2 { double m[2][2]; };
Mat2 operator*(const Mat2& A, const Mat2& B) {
    Mat2 R{};
    for (int r = 0; r < 2; r++)
        for (int c = 0; c < 2; c++)
            for (int k = 0; k < 2; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
    return R;
}
void print(const char* name, const Mat2& M) {
    std::cout << name << "[" << M.m[0][0] << " " << M.m[0][1] << "; " << M.m[1][0] << " " << M.m[1][1] << "]\n";
}

int main() {
    Mat2 A{{{1, 2}, {3, 4}}}, B{{{0, 1}, {1, 0}}}, C{{{2, 0}, {0, 3}}};
    print("(A*B)*C = ", (A * B) * C);
    print("A*(B*C) = ", A * (B * C));
    print("A*B     = ", A * B);
    print("B*A     = ", B * A);
}
(A*B)*C = [4 3; 8 9]
A*(B*C) = [4 3; 8 9]
A*B     = [2 1; 4 3]
B*A     = [3 4; 1 2]

Row vectors vs column vectors

This course (like GLM, OpenGL, Unity's Matrix4x4 and Godot) writes vectors as columns and multiplies M * v. The DirectX tradition (DirectXMath, HLSL's mul(v, M)) and Unreal's FMatrix use row vectors, v * M, where every matrix is the transpose and the order of a product reverses: v * S * R does S first. Both describe the same transformations. When you copy a formula from a book, check which convention it uses.

There is a separate question of memory layout (row-major vs column-major storage). GLM and OpenGL store matrices column by column; DirectXMath stores them row by row. The Toolkit lesson comes back to this.

Test yourself

With column vectors, what do the columns of a matrix tell you?

  1. The inverse of the transform
  2. Where the x and y (and z) axes end up
  3. The translation only
  4. Nothing; only rows matter

In M = A * B applied as M * v, which transform happens first?

  1. A
  2. B
  3. Both at once, order does not matter
  4. Whichever is a rotation

Why do engines multiply projection * view * model once per object?

  1. Because matrix multiplication is commutative
  2. Because it is associative, so one combined matrix can be applied to every vertex
  3. Because GPUs cannot multiply matrices
  4. To normalize the vertices