The Craft
Maths for Games
The handful of arithmetic tricks every game leans on — movement, direction, distance and chance — on machines with no fast multiply, no fractions, no square root and no sine. How to get the answer the machine hates to compute, cheaply, by trading a little exactness or memory for a lot of speed. Shown in Sinclair BASIC.
What this is
A game is things that move, and movement is maths: a position, a speed, a direction, a distance between two things. You already have the number sense — Numbers & Bits gave you bits, hex, two’s complement and the fixed-point that carries a fraction. This is where that sense goes to work, and where it runs into a hard wall: the machines these games run on are hostile to exactly the maths a game needs. No fast multiply. No fractions in the hardware. No square root. No sine. The very sums a moving thing depends on are the ones the processor is worst at.
So this module is not really about maths. It’s about the art of the cheap answer — getting the number a game needs without doing the sum the machine can’t afford. A direction becomes a lookup, not a sine. “Which enemy is closer” becomes a comparison with no square root in it. Half a pixel of drift a frame becomes an accumulator, not a decimal. Every unit is the same move: spot the expensive sum a game seems to demand, and find the cheap thing that gives the same answer in time to draw the next frame.
Each idea appears twice, as ever — but with a twist that fits this module. Sinclair
BASIC can multiply, and it does have SIN; so here BASIC plays the part of the
expensive, honest reference — the clear way, the slow way — and the technique is the
cheap version standing next to it. You’ll see the answer you want, and then the answer
you can actually afford, and check they match.
Who it’s for
Anyone building anything that moves — which is every game there is. Come here once Numbers & Bits is behind you: this module leans on fixed-point, on bit logic, and on the fact that shifting is the machine’s free multiply-and-divide-by-two. You don’t need a machine chosen yet — the strategies are true of every 8-bit processor, which is exactly why they’re taught here and not in one machine’s track.
What it is not
This is not a maths course — no proofs, no theory for its own sake, only the small handful of tricks games actually reach for. And it is not the specific routines for a specific chip: the exact sine table for your machine, the exact shift-and-add sequence your assembler wants — those are the machine’s own, and they live in Meet the Machine and the pattern library, told concretely once you’ve picked a side. Here we teach the strategy — a table instead of a calculation; a comparison instead of a root — and each machine supplies the values. The idea travels; the routine is local.
The shape
- The maths a game needs, and the maths a machine hates — position, speed, direction, distance: what a moving thing is made of, and why the processor underneath is bad at every part of it. The one skill: trade exactness or memory for speed.
- Sub-pixel motion — moving half a pixel a frame when the screen only has whole ones. A fixed-point accumulator that hoards fractions until they’re worth a pixel — smooth movement on a grid that has no in-between.
- Multiplying when you can’t multiply — no
MULon these machines. Free doubling by shifting, general multiply built from shift-and-add, and the table you reach for when even that’s too slow. Whyy × width + xis the sum behind every screen position. - Turning without trigonometry — you can’t call
SINsixty times a frame. A small precomputed table turns an angle into an (x, y) step with a lookup, so a thing can head in any direction, fire at any angle, or circle a point — no trig at run time. - Distance without square roots — how far away is the player? The honest sum has a square root in it, and a root is agony. Compare squared distances to find the nearest with no root at all; approximate the real distance cheaply when you need the number.
- Chance without a random number — the machine is perfectly predictable, and games need surprise. Manufacture it: a few cheap bit operations that spit out a stream that looks random — for drops, spawns, wander, twinkle.
- A thing that chases you — the payoff: build a simple pursuer from the pieces. A direction to the player (turning), a distance to decide whether to bother (distance), a fixed-point step to close in smoothly (sub-pixel), a pinch of chance so it isn’t robotic. That’s the arithmetic spine of pathfinding and enemy AI, whole.
By the end you’ll own the small toolkit that every moving, chasing, aiming, spawning thing in a game is built from — and you’ll reach for a table or a comparison where you once reached for a calculator. From here the tricks turn concrete: Meet the Machine, and the pattern library, where each becomes a real routine in your chosen machine’s own language.