Skip to content

Spatial Grid

Vicious Squid edited this page May 13, 2026 · 3 revisions

The spatial grid is a performance optimization tool used by the physics engine (defined in engine/physics.py).
It divides the 3D game world into a series of uniform mathematical buckets or "cells."

The grid can be viewed (during Play Mode) using the console command: sg

Instead of performing a "narrow-phase" collision check between every single object in the scene—which would be computationally expensive (O(N^2))—the engine uses the grid for a "broad-phase" check. It only calculates collisions for objects that occupy the same or adjacent grid cells.

Implementation Details

  • Grid Size: The dimensions of each cell are generally defined in engine/constants.py

  • Dynamic Updates: As entities move, they are removed from their old cell and registered into a new one. Toggling sg allows you to verify that these updates are happening correctly and that objects aren't "leaking" out of the partitioning system.

  • Command Registration: In editor/console_commands.py, the command is mapped to the toggle_spatial_grid method, which interacts directly with the Physics class.

Clone this wiki locally