Preface

Preface

Somewhere behind every game you've ever played, a quiet layer of mathematics is doing the real work. When a character slides along a wall instead of sticking to it, that's a dot product. When a fireball arcs, and a distant tower grows as you fly toward it, that's a vector, a matrix and a division. When an enemy finds its way to you around a maze of obstacles, that's a graph search. None of it is on the screen, and all of it is underneath.

This book is about that layer: the mathematics and the algorithms that every game runs on. It teaches them the way I believe they're best learned. Not as theorems on a page, but as small, living programs that you run and pull at with the mouse until the idea clicks.

What This Book Is

This is the Fundamentals book in the Learning C++ by Building Games series. The series' first book teaches the C++ language, and builds complete games with it. This one goes underneath those games. It assumes that you can already write a program, and spends its energy on the machinery that makes them work. That's vectors and trigonometry, motion and geometry, collision and physics, and pathfinding and rendering. By its last part, that includes enough 3D mathematics to draw a scene of your own, and to turn, texture and fly through it.

There are no games to finish here, and no lives, score or win screen. Instead, every idea gets a focused demo of its own, 36 in all. Each is a program you run, drag around and watch behave. Seeing a dot chase your cursor correctly, or a fast bullet stop passing through a wall, teaches the mathematics better than any still picture can.

Who This Book Is For

Three kinds of reader are all welcome here:

  • Readers of Learning C++ by Building Games. You know the language, and you've built some small games. This is the natural next step: the why and the how beneath the code you've been writing.
  • C++ programmers who are new to game mathematics. You're comfortable with classes and the standard library, but a rotation matrix or the separating axis theorem is new ground. Everything here is built from a blank file.
  • Game developers who want the math from the ground up. You've used an engine's Vector3 and its raycasts, and you'd like to know what they're really doing.

You should know C++ up to classes and structs. That means functions and their parameters, references and const, and the standard containers, above all std::vector. The book doesn't teach those again. It does teach the one feature it leans on that a first course often skips: operator overloading. Chapter 2 builds it from scratch, so that two vectors can be added as a + b. Anything else past the basics, such as <random> or a function pointer, is explained where it's first used.

How the Book Works

Four ideas shape every chapter.

SDL draws, and the math is ours. The SDL 3 library opens a window and reads the mouse and the keyboard. It also draws lines, rectangles and textures, and fills triangles with them. That's all it does. Every transform, intersection test and step of physics is the book's own code, written out and explained. In Part 7, so is every pixel of a solid 3D object, filled by a rasterizer we write ourselves. No library does the interesting work off the page.

Every demo is walked through. Each chapter's demo is a complete program, and the chapter walks through its code part by part, printing every line that's new, and explaining it. A helper from an earlier chapter is named rather than printed again. Nothing is left for you to puzzle out alone on GitHub.

Math you can see. Every idea gets a picture, and wherever it can, something to drag. A vector is an arrow you pull, a dot product is a cone that lights up, and a camera is a world that slides the other way when you move. Every number the text quotes is checked against the demo it's about.

One skeleton. Chapter 1 builds a small program called the skeleton. It has a window, a game loop timed by delta time, input and text. Every demo starts from it, and each chapter shows only what's new. Appendix B prints it whole. The math grows the same way, in headers built a few functions at a time. The first is Vec2, and later ones hold matrices, 3D vectors and quaternions. So you always know exactly what's inside the tools you're using, because you put it there.

How the Book Is Organized

The thirty-five chapters come in seven parts, climbing from a single point on the screen to a textured 3D scene:

  • Part 1. Foundations: The Vector (Chapters 1 to 4). Coordinates and the skeleton, the Vec2 type and its arithmetic, and the dot and cross products.
  • Part 2. Angles, Rotation and Motion (Chapters 5 to 10). Trigonometry and rotation, motion by delta time and easing, then steering and the forces of nature.
  • Part 3. Geometry, Shapes and Curves (Chapters 11 to 14). Shapes and lines, distances and closest points, and Bezier curves and splines.
  • Part 4. Collision Detection and Response (Chapters 15 to 18). Overlap tests and the separating axis theorem, raycasting and swept tests, and collision response.
  • Part 5. Scaling Up (Chapters 19 to 23). Spatial partitioning and a small physics engine, randomness and noise, and pathfinding up to A*.
  • Part 6. Rendering in 2D (Chapters 24 to 27). Textures, the scene graph and 2D matrices, and the camera.
  • Part 7. Into the Third Dimension (Chapters 28 to 35). Vectors and matrices in 3D, projection and the camera, and the whole pipeline. Then hidden surfaces, texturing and quaternions.

Six appendices follow. Appendix A is a cheat sheet of the book's formulas, Appendix B prints the skeleton, and Appendix C lists the SDL functions the demos use. Appendix D points you onward, and names where the ideas came from. Appendix E is a glossary of every term the book teaches, and Appendix F is for Mac and Linux users.

How Each Chapter Works

Each chapter opens by saying what it builds, and why it's worth building. Then it builds the mathematics up, rather than handing it down: each formula is worked out from what came before, checked with numbers, and shown in a picture.

Then comes the demo. The chapter walks through its code in the skeleton's order: its helpers, its state, its events, each frame's work and its drawing. From Chapter 2 on, Trying It Out is a short tour of the running demo, a step at a time. It says what each step shows, and the numbers the readout gives.

Each chapter ends the same way. The FAQ answers the questions the chapter tends to raise, and Exercises gives you problems to work, some with a pencil, and some by changing the demo. From Chapter 2 on, an optional AI exercise suggests a small adventure to take with an AI's help. It gives a prompt that describes the code you've built, so that the AI builds on it, and says what to look for in the answer. A one-paragraph Summary closes the chapter.

Getting the Code

Every demo is on GitHub, finished and ready to run, at https://github.com/EliteIntegrity/Learning-C-by-Building-Games-Fundamentals-2nd-Edition. Choose Code > Download ZIP there, and you'll have a folder for each demo, named after it, and the Skeleton folder. SDL 3 and SDL3_ttf are beside them, and so is the font the demos use, so every demo opens and runs with nothing else to download. Chapter 1 shows how.

The code on these pages is the repository's, line for line, so when a chapter says what a demo does, the demo in front of you does it. If you have the first edition, its code is still where it always was, at https://github.com/EliteIntegrity/Learning-C-by-Building-Games-Fundamentals.

What You'll Need

You'll need Visual Studio 2026, whose Community edition is free, with its Desktop development with C++ workload. The book was built with SDL 3.4.4 and SDL3_ttf 3.2.2, which come with the code, and any later 3.x releases will do. You don't need a powerful machine, a graphics card or any paid software.

The demos are standard C++ and SDL, and they run on a Mac or on Linux too. Appendix F shows how to build them there, with CMake and VS Code.

New in This Edition

If you've read the first edition, you'll find a much deeper book, nearly twice as long. Every chapter has been rewritten to teach its topic in full. Its formulas are built up rather than stated, with worked numbers, and every claim is checked against its demo. Each chapter has an FAQ and exercises now, and the pictures have more than doubled, with every screenshot taken again at print quality.

The demos have been rebuilt too, from one skeleton. They wait for the screen's refresh, move the same at any frame rate, and show live readouts of the numbers the text discusses. Every line the book prints is the demo's own. The repository includes SDL, so every demo opens and builds as it comes, and a new appendix covers the Mac and Linux.

A Word Before We Begin

The mathematics in these pages has a reputation. Linear algebra, the separating axis theorem, perspective-correct interpolation and quaternions are heavy words. They've scared off a great many capable people who would have loved what's underneath them. So I want to say this plainly before we start: you can do this.

Nobody is born understanding a rotation matrix. It isn't a talent. It's a thing you build, one small, visible piece at a time, which is exactly how we're going to build it. Every idea here will be something you can see move on the screen, and something you wrote the code for. By the end of Part 7, you'll have built a textured, lit, spinning cube from a blank file. And you'll understand every line that put it there.

Let's begin with a single point on the screen.