Game Development · Guide

Physics & Animation

Collision detection in two phases, why the timestep must be fixed, and how skeletal animation blends into it.

— min read Game Development

Convincing, Not Correct

Game physics is not simulation. It is a set of approximations chosen so that the result looks right, runs in a couple of milliseconds and never explodes. Accuracy is negotiable; stability is not.

Two problems make up most of it. Detection — what is touching what — is a search problem that must not degrade to comparing every object with every other. Response — what happens next — is an integration problem that has to stay stable when the frame rate moves.

Collision Detection

Comparing every pair of objects is O(n²): a thousand objects means half a million tests per frame. So detection splits in two.

PhaseJobUses
BroadReject pairs that cannot possibly touchBounding boxes, spatial grid, BVH
NarrowExact test on the few surviving pairsReal shapes, SAT, GJK

The broad phase is where the algorithmic win is, and it is a spatial data-structure problem — a uniform grid, a quadtree, or a bounding volume hierarchy that lets you discard whole branches at once. The narrow phase can then afford to be precise, because it only ever sees a handful of pairs.

Keep collision shapes simple. A character is a capsule, not the rendered mesh. Colliding against detailed geometry is expensive, unstable and almost never what the design needed.

Detecting an overlap is not enough — you also need penetration depth and a contact normal, or the resolver has no idea which way to push things apart.

Integration & the Fixed Timestep

Integration advances velocity and position by one step of time. The naive form works and is stable enough for most games, as long as the step never changes size:

velocity += acceleration * dt;
position += velocity * dt;

A variable dt makes results depend on frame rate. Worse, one long frame produces one enormous step, and a fast object moves from one side of a wall to the other without ever being inside it. That is tunnelling, and it is why a bullet passes through a door.

ProblemFix
Frame-rate-dependent resultsFixed timestep with an accumulator
Fast objects tunnellingContinuous detection — sweep the shape along its path
Jitter at restSleep bodies below a velocity threshold
Spiral of death after a hitchCap the number of catch-up steps per frame
The spiral of death: a slow frame queues extra physics steps, which makes the next frame slower, which queues more. Always clamp how many steps one frame may run — dropping simulation time is better than freezing.

Skeletal Animation

A character mesh is bound to a skeleton — a hierarchy of bones. Each vertex is weighted to a few of them, so moving a bone moves the surrounding surface. An animation is a set of keyframed bone transforms; playback interpolates between keys, using slerp for the rotations.

Blending is what makes it look like movement rather than a slideshow. Walk to run is a weighted mix of two clips driven by speed; turning is another blend on top; a wave can play on the upper-body bones while the legs keep walking. A state machine decides which clips are active and how fast the weights move.

Blend times are gameplay, not polish. A long blend looks smooth and feels unresponsive — if the character keeps sliding for 300 ms after the stick centres, the controls feel broken however good the animation is.

Root motion decides who moves the character: the animation itself, or the gameplay code. Animation-driven motion has no foot sliding and reads beautifully; code-driven motion is precise and predictable, which multiplayer and platforming usually need more.

Interview Questions

Why split collision into broad and narrow phases?

Testing every pair is O(n²). The broad phase uses cheap bounds and spatial structures to reject almost everything, so the exact tests only run on a handful of surviving pairs.

What is tunnelling and how do you fix it?

A fast object moves further than its own thickness in one step and is never overlapping at any sampled moment. Continuous collision detection sweeps the shape along its path instead of testing endpoints.

Why must physics use a fixed timestep?

Integration results depend on step size, so a variable step makes the simulation behave differently at different frame rates and destabilises it after a hitch.

What is the spiral of death?

A slow frame queues extra catch-up physics steps, which makes the next frame slower still. Capping the steps per frame breaks the feedback loop.

Why use simple collision shapes?

Capsules and boxes are cheap and numerically stable. Colliding against a full render mesh is expensive, jittery, and rarely matches what the gameplay actually needs.

Root motion or code-driven movement?

Root motion removes foot sliding and looks better; code-driven movement is precise and predictable, which multiplayer prediction and tight platforming depend on.

Quick Quiz

1. The broad phase exists to…
2. A bullet passing through a wall is…
3. Physics uses a fixed timestep for…
4. Capping catch-up steps per frame prevents…
5. Blending between animation clips is driven by…