Spec a spatial hash for a 60 fps game

Vibe codingMedium25 min

Your task

Your 2D game checks every pair of objects for collisions each frame. With 3,000 objects, that check alone takes 9 ms of a 16.6 ms frame. You want a spatial hash instead: a grid that buckets objects by cell, so only objects in the same cell get compared. A coding agent will write it in C++.

Write the spec you would hand the agent.

What you know:

  • objects are axis-aligned boxes: struct Box { uint32_t id; float minX, minY, maxX, maxY; }
  • most boxes are 8 to 32 units wide, and a few bosses are 400 units wide
  • the world runs from -10,000 to 10,000 on both axes
  • replays need the same pairs in the same order on every run

Then, in a few sentences: what does "write a collision detection function" get wrong?