Collision tunneling (fast objects passing through walls)
collisionphysicsccdraycastswept-aabbflame
Tunneling happens when an object moves farther in one frame than the thickness of what it should hit, so a discrete overlap test never sees the contact. It exists in every 2D engine with discrete collision; it is not a framework bug (Justin once asked about rewriting Run in vanilla JS over it; switching engines would not have helped).
Approaches, in order of preference
- Fixed-timestep substepping: split the frame into fixed steps (1/120 s) and run integrate + detect + resolve in each. Erin Catto (Box2D) moved Box2D v3 to substeps as the primary method, calling it simpler and more robust than continuous collision. Requires owning the collision pass (Fixed-timestep game loop for browser games).
- Swept AABB: Minkowski-expand each static box by the mover's half-extents and do a ray-vs-box test along the velocity to get the exact time of impact (Metanet's N tutorial covers this for tile platformers).
- Raycast from previous to current position: cheap safety net, ideal for projectiles and lasers.
What happened in Run (Flame), chronologically
- Manual swept test iterating every platform each frame: worked, but O(n) per frame caused frame spikes and audio stutter.
- dt clamping (20 ms cap): physics felt floaty at low fps. Reverted.
- Substepping inside
update(): does not work in Flame, because Flame's collision detection runs once after all updates, not between substeps; gravity also compounded. Reverted. - Flame raycast (kept): after the move,
game.collisionDetection.raycast()(QuadTree backed, roughly O(log n)) from the previous to the current position, filtered to solid platforms and turret bodies; on hit, snap position, zero velocity, register the contact. Only runs when displacement exceeds 14 px on an axis.
Later fixes to the raycast rescue (2026-07 improvement cycles): - Use one-frame "pending" flags rather than adding to the contact set directly; a bare contact removal burned the buffered ground jump and the double jump. - Make the one-way-platform filter direction-aware and admit one-way landings based on the previous position (pure predicates, unit-tested).
Don'ts
- Don't iterate
children.whereType<Platform>()every frame; use a cached list or the broadphase. - Don't clamp max velocity to hide tunneling; it limits gameplay.
- Don't bring in a full physics engine for a platformer.
References
- Erin Catto, GDC 2013, "Continuous Collision" (box2d.org)
- Gaffer on Games, "Fix Your Timestep"
- Flame docs acknowledge tunneling and recommend raycasts.
See also
- Flame engine architecture and gotchas
- Platformer movement feel (coyote time, jump buffering, wall jumps)