Skip to content
Saarrthi Rana

Game / November 2026

Collision Lab

An interactive lab about the data structures games use to find collisions, running in your browser.

Role
Solo project
Team
Solo
When
November 2026
TypeScriptCanvasSpatial hashingQuadtreeSweep and pruneVitest
Try it

Collision Lab

Pick an arena and a way of finding collisions, then press Stress test. The counters show the work each one does on the same frame, so you can compare them directly.

Collision Lab: a swirling crowd of circles inside the quadtree's small boxes

Loading Collision Lab

Overview

Every frame, a game has to work out which objects are touching. Checking every pair gets slow fast: 2,000 objects means about two million pairs. Engines use data structures to skip most of them, and which one works best depends on how the objects are spread out.

Collision Lab runs brute force, a uniform grid, a quadtree and sweep and prune on the same moving bodies, and counts the work each one does. There are four arenas with an even spread, a crowd, lanes of traffic and a few giants among tiny bodies, and each one favours a different structure. A stress test keeps adding bodies until the structure you picked goes over a fixed work budget, so you can see how far each one gets.

What's inside
  1. Brute force as the baseline. It compares every pair, n(n-1)/2 of them.
  2. A uniform grid that only compares bodies sharing a cell, and tests each pair in just one cell so nothing is counted twice.
  3. A quadtree that splits a box into four when it gets crowded, so it's fine where bodies pack together and coarse where they don't.
  4. Sweep and prune, which keeps the bodies sorted along one axis and fixes the order with insertion sort each frame. That costs almost nothing when bodies only move a little.
  5. A work counter for each one that adds the pairs it compares to the work it takes to set up, against a budget of 60,000 a frame.
  6. Four arenas that each move differently. There's an even spread, a crowd circling a moving point, lanes of traffic, and a few giants among tiny bodies.
  7. A stress test that adds bodies until the chosen structure goes over budget, and records how many it handled.
  8. Tests that check all four find exactly the same touching pairs in every arena, frame after frame.
How it works

Even spread

The grid does the least work. Every cell holds a few bodies, so each body is only compared with its neighbours. At 2,500 bodies it needs about 16,000 units of work a frame, where brute force needs over three million.

Crowd

Most bodies circle one moving spot. The grid's cells in the crowd fill up, while the quadtree splits finely there and stays coarse everywhere else. At 2,500 bodies it does about 30 percent less work than the grid.

Traffic

Cars in lanes barely change order, so sweep and prune re-sorts them with almost no swaps. Sorting along X instead of Y cuts its work to about a third, because along Y every car overlaps the rest of its lane.

Giants

A few huge bodies land in every cell and leaf they cover. The quadtree suffers most here, with about three times the work it does in the even spread.

What it teaches

Big O tells you how the work grows, but how the objects are spread out decides which structure wins. There isn't one best structure, only the best one for the data you have.

Written in TypeScript and drawn on a canvas. The numbers in the lab are counted live by the same code the tests check.

Next project

Awakening Nightmare

A co-op horror puzzle game for up to four players. We showed it at Level Up Showcase 2026.