HTML5 game publishing
2D Collision Detection for Browser Games Without a Physics Engine
AABB and circle tests, a spatial grid broad phase, axis-by-axis resolution, tunneling fixes and collision layers.
Collision detection is the part of a 2D game where small mistakes become visible bugs: bullets that pass through enemies, players that stick to walls, frame rates that collapse when the screen fills up. You do not need a physics engine for most browser games. You need a few simple shape tests, a way to avoid checking everything against everything, and a careful way to resolve overlaps. This post covers all three.
Pick simple shapes
Most 2D games get by with two shapes:
- Axis-aligned bounding boxes (AABB): rectangles that never rotate. Great for tiles, platforms, walls and most characters.
- Circles: great for bullets, coins, balls and round enemies. The test is the cheapest of all.
The collision shape does not need to match the sprite exactly. A slightly smaller hitbox than the sprite feels fairer to players: near misses feel like dodges, not cheap deaths. Many classic games make the player's hitbox smaller than its art for exactly that reason.
The basic tests
// rectangle vs rectangle (x, y = top-left)
function aabb(a, b) {
return a.x < b.x + b.w && a.x + a.w > b.x &&
a.y < b.y + b.h && a.y + a.h > b.y;
}
// circle vs circle
function circles(a, b) {
const dx = a.x - b.x, dy = a.y - b.y, r = a.r + b.r;
return dx * dx + dy * dy < r * r; // no square root needed
}
// circle vs rectangle
function circleRect(c, r) {
const nx = Math.max(r.x, Math.min(c.x, r.x + r.w));
const ny = Math.max(r.y, Math.min(c.y, r.y + r.h));
const dx = c.x - nx, dy = c.y - ny;
return dx * dx + dy * dy < c.r * c.r;
}
Comparing squared distances avoids Math.sqrt, which matters when you run thousands of tests per frame.
The broad phase: don't test everything
With 500 objects, testing every pair is about 125,000 tests per frame. With 2,000 objects, about 2 million. That is where frame rates collapse. A broad phase quickly finds pairs that might collide, and only those get the exact test.
For most 2D browser games, a uniform grid (also called a spatial hash) is the simplest and fastest option:
const CELL = 64;
const grid = new Map();
function key(cx, cy) { return cx * 73856093 ^ cy * 19349663; }
function insert(obj) {
const x0 = Math.floor(obj.x / CELL), x1 = Math.floor((obj.x + obj.w) / CELL);
const y0 = Math.floor(obj.y / CELL), y1 = Math.floor((obj.y + obj.h) / CELL);
for (let cx = x0; cx <= x1; cx++)
for (let cy = y0; cy <= y1; cy++) {
const k = key(cx, cy);
let cell = grid.get(k);
if (!cell) grid.set(k, cell = []);
cell.push(obj);
}
}
Each frame, clear the grid (or reuse the arrays), insert moving objects, and for each object check only the others in its cells. Choose a cell size around the size of your typical object, or a bit bigger. Objects that span several cells are inserted into each, so remember to skip pairs you already tested.
Static geometry, like a tile map, does not need to go in the grid every frame. For tiles, compute which tiles an object overlaps directly from its position, since the map is already a grid.
Resolving collisions
Detecting overlap is half the job. For solid things like walls and platforms, you also need to push the object back out.
The most reliable approach for platformers is to move and resolve one axis at a time:
- Move horizontally by
vx * dt. - Check collisions with solid tiles. If overlapping, push the player back out horizontally and set
vx = 0. - Move vertically by
vy * dt. - Check again. If overlapping, push out vertically. If you were moving down, you are now on the ground.
Resolving both axes at once is where "sticking to walls" and "snagging on tile seams" bugs come from: the game cannot tell whether to push the player up or sideways. Separating the axes removes the ambiguity.
Tunneling: when fast objects pass through
A bullet moving 30 pixels per step can skip over a 10-pixel wall entirely: one step it is in front, the next it is behind, and no step catches the overlap. Fixes, from simplest:
- Fixed timestep. Keeps step sizes small and predictable. Large variable steps make tunneling much worse.
- Substeps. Move fast objects in several smaller steps per update, so each step is smaller than the thinnest wall.
- Raycasts or swept tests. Treat the bullet's movement as a line segment from old to new position and test that line against targets. This is the standard approach for very fast projectiles.
- Thicker colliders. Sometimes just making walls thicker in the collision map (not the art) is enough.
Layers and filtering
Not everything needs to collide with everything. Player bullets should hit enemies, not the player or other bullets. A simple bitmask filter avoids useless tests and accidental hits:
const PLAYER = 1, ENEMY = 2, PBULLET = 4, EBULLET = 8, WALL = 16;
player.mask = ENEMY | EBULLET | WALL;
pbullet.mask = ENEMY | WALL;
function shouldTest(a, b) {
return (a.mask & b.layer) || (b.mask & a.layer);
}
When to use a physics library
For stacking, rotating bodies, joints, ropes, ragdolls or realistic bouncing, use a library. Writing a stable rigid-body solver is a project of its own. Matter.js and Planck.js are common choices for 2D in JavaScript. For everything else, such as platformers, top-down shooters and most arcade games, simple shapes and the methods above are faster, easier to control and easier to debug.
Debug drawing
Add a key that draws every hitbox, the grid cells and the collision normals. Half of all collision bugs become obvious the moment you can see the hitboxes. Leave the toggle in debug builds only.
Checklist
- Simple shapes (AABB, circle), hitboxes slightly forgiving for the player.
- A broad phase grid when you have many objects.
- Axis-by-axis resolution for solid tiles.
- Substeps or raycasts for fast projectiles.
- Collision layers to skip pairs that should never interact.
- A debug view for hitboxes.
For the game loop that goes with this, see our post on fixed timestep loops on the developer blog.


