📚 Encyclopedia

Games & Graphics · updated 2026-10-13

Tile collision with merged rectangles

collisiontilemapphysicsgreedy-mergejavascript

Colliding a moving circle against a grid of solid tiles one tile at a time gives wrong contact normals on continuous walls. Merging solid tiles into large rectangles first (a greedy merge) fixes it and is cheap. Measured on the Spacebar campaign map.

The problem with per-tile collision

A circle straddling two adjacent solid tiles that form one flat wall touches the shared edge between them. Per-tile resolution sees a corner or side contact and produces a normal along that shared edge, ejecting the vehicle sideways at full radius. On a flat wall this produced 39% wrong normals, worst case 34.8 degrees.

The fix: greedy rectangle merge at bake time

Data structure verdict

A review of alternatives concluded that a flat Uint8Array tile grid is near-optimal at this scale. Upgrade paths if needs change: edge segments or a signed distance field (SDF) for curves and diagonals; a navmesh only if on-foot movement arrives; a quadtree is the wrong scale below about 16k x 16k tiles.

Diagonals warning

Staircase (stair-stepped) tile edges produce normals of 90 or 180 degrees, never 135, so 45-degree walls built from square tiles feel wrong. Doing diagonals properly needs triangle half-tiles plus edge-segment colliders and a clearance check. Spacebar deferred them; curves were approved instead as a stair-step barrier with a smooth routing path (see Road-graph map pipeline (author roads, generate the rest)).

Gotchas

See also

Linked from

Sources: spacebar memory (project_map_pipeline, project_js_rebuild)