Skip to main content

Command Palette

Search for a command to run...

A Friendly Introduction to Linear Algebra For Game Devs

Published
9 min read
A Friendly Introduction to Linear Algebra For Game Devs

What Is Linear Algebra Even?

Linear algebra is the math of vectors and matrices. That is it. The whole field.

Vectors describe things like position and direction and movement. Matrices describe transformations. Like moving things. Rotating things. Scaling things.

Games are made of 3D points that need to be positioned and moved and rotated and projected onto your screen. All of that is vectors and matrices. That is why this matters.

Part 1. Vectors.

A vector is a list of numbers. That is the simplest definition.

A 2D vector has two numbers. Like (3, 4). A 3D vector has three numbers. Like (2, 5, 7). You can have 4D vectors and beyond but games mostly use 2D and 3D and sometimes 4D.

But what do those numbers mean? It depends on what you are using the vector for.

Position Vectors

A position vector tells you where something is in space. If your player is at (10, 0, 5) that means they are 10 units along the X axis. 0 units on the Y axis. And 5 units on the Z axis.

Direction Vectors

A direction vector tells you which way something is pointing. Not where it is. Just which way.

If your character is facing forward. Their direction vector might be (0, 0, 1). That means zero on X. Zero on Y. One on Z. So they are pointing down the Z axis.

The same numbers can be a position or a direction depending on how you use them. That is important to remember.

Part 2. Basic Vector Operations.

Vectors are useful because you can do math with them. Here are the operations you will use constantly.

Adding Vectors

You add vectors by adding each of their numbers together.

(1, 2, 3) + (4, 5, 6) = (5, 7, 9)

Why is this useful? Because adding a direction vector to a position vector moves the position in that direction.

If your player is at (10, 0, 5) and they move forward by (0, 0, 1) their new position is (10, 0, 6). That is literally how movement works in games.

Scaling Vectors

You can multiply a vector by a single number. This is called scalar multiplication. A scalar is just a regular number. Not a vector.

2 × (1, 2, 3) = (2, 4, 6)

This makes the vector bigger or smaller while keeping its direction the same. Multiply by 2 and it is twice as long. Multiply by 0.5 and it is half as long. Multiply by -1 and it flips around pointing the opposite way.

This is useful for things like speed. A direction vector tells you where you are going. Multiply it by how fast you want to go. Now you have velocity.

Part 3. Magnitude and Normalization.

Magnitude

The magnitude of a vector is its length. How long is the arrow.

For a 2D vector (3, 4) the magnitude is 5. For a 3D vector you use the same idea. Square each number. Add them up. Take the square root. You do not need to memorize the formula. Just know that magnitude means length.

If a vector represents velocity. Its magnitude is the speed. If it represents a distance from A to B. Its magnitude is how far apart they are.

Normalization

A normalized vector is one whose length is exactly 1. Also called a unit vector.

You normalize a vector by dividing it by its own magnitude. The direction stays the same. Only the length changes to 1.

Why do this? Because when you only care about direction not distance. You want length 1. It makes math cleaner and faster.

Every time a game needs "which way is this thing facing" or "which direction is the light coming from" it uses normalized vectors.

Part 4. The Dot Product.

This one is huge. The dot product shows up everywhere in games.

The dot product takes two vectors and gives you back a single number. Not a vector. Just a number.

Here is what matters. That number tells you how aligned the two vectors are.

  • If they point in the exact same direction. The dot product is 1.

  • If they are perpendicular. Ninety degrees apart. The dot product is 0.

  • If they point in opposite directions. The dot product is -1.

  • Anything in between gives you a value between -1 and 1.

This only works cleanly when both vectors are normalized. Remember normalization from before? This is one of the reasons it matters.

Why You Care About The Dot Product

Lighting. To figure out how bright a surface is. You take the dot product of the surface normal and the light direction. A surface normal is a vector that points straight out from a surface. If it faces the light the dot is close to 1. Bright. If it faces away the dot is close to 0 or negative. Dark. That is literally how games calculate basic lighting.

Is something in front of you? Take the dot product of your forward direction and the direction to the other object. If it is positive. They are in front of you. If negative. They are behind you. Super useful for AI and camera logic.

What angle are two things at? The dot product tells you how aligned two vectors are. You can use it to check if two objects are roughly facing the same way. Or for a guard that should only see you if you are in their cone of vision.


Part 5. The Cross Product.

The cross product takes two vectors and gives you back a new vector. Not a number like the dot product. An actual vector.

The new vector is perpendicular to both of the input vectors. Ninety degrees from both.

Why You Care About The Cross Product

Calculating normal vectors. A normal vector points straight out from a surface. Games need them for lighting and physics. If you have a triangle with three corners. You can take two of its edges as vectors. Cross product them. The result is the normal vector for that triangle. Every single 3D model uses this.

Defining a 3D coordinate system. If you know which way is forward and which way is up for a character. You can cross product them to find which way is right. This is how character controllers and cameras work.


Part 6. Matrices.

A matrix is a grid of numbers. Rows and columns.

A 4x4 matrix has 16 numbers arranged in 4 rows and 4 columns. That is the most common one in games.

Matrices look scary but they do one main thing. They transform vectors.

Transform means change. Move. Rotate. Scale. Any of that.

When you multiply a matrix by a vector you get a new vector. The matrix is the transformation. The original vector is the input. The result is the transformed vector.

The Three Transformations You Need

There are three main kinds of transformations in games. Each one is just a specific matrix.

Translation. Moves a point from one place to another. Adds some offset to the position. If you want to move something 5 units to the right. There is a translation matrix for that.

Rotation. Spins a point around an axis. Rotating around the Y axis spins things like they are on a turntable. Rotating around the X axis tilts things up and down. Each rotation is its own matrix.

Scale. Makes things bigger or smaller. A scale of 2 doubles the size. A scale of 0.5 halves it. You can even scale differently on each axis. Make something twice as tall without changing its width.

Part 7. Combining Transformations.

Here is the really cool part. You can multiply matrices together to combine transformations.

If you have a rotation matrix and a translation matrix. Multiply them together. Now you have a single matrix that rotates AND translates in one step.

This is how every 3D object in a game gets placed into the world. The game has a matrix for each object that contains all the position and rotation and scale info combined. One matrix holds everything.

Order Matters

One thing to watch out for. The order you multiply matrices in changes the result. Rotating then moving is different from moving then rotating.

Think of it like directions. Walk 10 steps forward then turn right. You end up in a different place than if you turn right first and then walk 10 steps forward. Same operations. Different order. Different result.

Part 8. The Big Picture. How It All Fits In A 3D Engine.

Every 3D point you see on screen goes through a chain of matrix transformations before it gets drawn. This chain is called the MVP transformation. Model. View. Projection.

Model Matrix. Takes a 3D object and places it in the world. Position. Rotation. Scale. All in one matrix per object.

View Matrix. Takes the world and positions it relative to the camera. Like moving the whole world so the camera is at the origin looking forward.

Projection Matrix. Takes the 3D world and squishes it down onto your 2D screen. This is where perspective happens. Things farther away look smaller.

Every vertex of every 3D model gets multiplied by these three matrices. Millions of times per frame. The GPU does this incredibly fast. That is why modern games can render huge detailed worlds.