CollisionManager
CollisionManager is a singleton that runs AABB, sphere, and circle overlap detection each frame. It uses a spatial hash for broad-phase culling and supports layer/mask filtering.
It is mounted automatically by Game via the internal CollisionFlush system (priority -25). You do not need to set it up manually.
import { getCollisionManager } from "@carverjs/core/systems";How It Works #
Each component calls
useCollisionto register a collider with a shape, layer, and callbacks.Every frame at priority -25 (after
fixedUpdate, beforeupdate),CollisionFlushcallstick().tick()runs:Broad phase — inserts all colliders into a spatial hash grid and extracts candidate pairs.
Layer filtering — skips pairs where
(layerA & maskB) === 0 || (layerB & maskA) === 0.Narrow phase — tests AABB-AABB, sphere-sphere, and AABB-sphere overlaps.
Event dispatch — fires
onCollisionEnter,onCollisionStay, andonCollisionExitcallbacks.
Supported Shape Pairs #
| Shape A | Shape B | Supported |
|---|---|---|
| AABB | AABB | Yes |
| Sphere | Sphere | Yes |
| Circle | Circle | Yes |
| AABB | Sphere/Circle | Yes |
| Sphere | Circle | Yes |
All tests are 2D-aware — in "2d" mode, the Z axis is ignored.
API #
getCollisionManager() #
Returns the singleton instance. Creates one if it doesn't exist.
destroyCollisionManager() #
Destroys the singleton. Called automatically when Game unmounts.
Usage #
Most game code should use useCollision instead of accessing CollisionManager directly. Direct access is useful for systems-level code:
import { getCollisionManager } from "@carverjs/core/systems";
// Check if two named colliders overlap
const mgr = getCollisionManager();
const overlaps = mgr.getOverlaps(colliderId);Type Definitions #
See Types for ColliderDef, ColliderShape, CollisionEvent, and CollisionCallback.