Implement the following plan: Fix Collision Detection Bugs (Movement Snapping & Bounding…
- User
Implement the following plan:
Fix Collision Detection Bugs (Movement Snapping & Bounding Box Misalignment)
Context
When colliding with obstacles in Level 2, the player experiences movement "snapping" (getting locked to one axis while touching certain edges) and visual misalignment between debug bounding boxes and actual collision behavior. Three interrelated bugs cause these issues.
Root Cause Analysis
Bug 1: Integer truncation in collision rectangle positioning
RectangleExtensions.AtPosition()usesposition.ToPoint()which truncates floats to ints, causing up to 1px error between the collision rect and the entity's actual position. The debug rendering uses float positions (viaSpritePrepSystem), so the red boxes are more accurate than the actual collision math. This explains:- "Don't actually touch" cases: collision box shifted away from debug box
- "Overlap" cases: collision box shifted into obstacle
Bug 2: Negative contactTime accepted as valid collision
DynamicRectVsRect(line 117) only checkscontactTime < 1.0fwith no lower bound. When Bug 1 causes sub-pixel overlap, the ray inRayVsRectstarts inside the expanded target, producingcontactTime < 0. The resulting contact normal is perpendicular to the player's movement, so resolution snaps the wrong axis and zeros the wrong velocity component -- causing the "locked to one axis" behavior.The legacy
CollisionDetectionSystem.cs:130even has the correct fix commented out:// return potentialCollision && contactTime is >= 0.0f and < 1.0f;Bug 3: ParallelSystem for sequential collision pipeline
LoadLevelExampleGameScreen.cs:109wraps Movement, Velocity, Detection, Resolution, and Commit inParallelSystem. These must run sequentially. Race conditions cause non-deterministic collision failures. (DreamGameScreen.cs:66already usesSequentialSystemfor the same pipeline -- the correct pattern.)Implementation Plan
Step 1: Create
CollisionRectstructNew file:
MonoDreams/Extensions/Monogame/CollisionRect.csA float-precision AABB struct that replaces
Rectanglein collision math, eliminating the integer truncation at the source.csharp Assistant
I'll implement this plan step by step. Let me start by reading the relevant files to understand the current code.
Assistant
Now let me read all the relevant files in parallel.
Assistant
Now let me read the
RectangleExtensionsto see theAtPositionmethod and check the existing extension directory structure.Assistant
Now let me also check the
BoxCollidercomponent to understandBoundstype.Assistant
Now let me build before making any changes, as the CLAUDE.md instructs.
Assistant
Build succeeds. Now let me implement the changes.
Step 1: Create CollisionRect struct
Assistant
Step 2: Update TransformCollisionDetectionSystem
Assistant
Now I'll rewrite the TransformCollisionDetectionSystem to use
CollisionRect:Assistant
Step 3: Update TransformCollisionResolutionSystem
Assistant
Step 4: Fix system execution order
Assistant
Step 5: Apply same fixes to legacy core systems
Assistant
Now the legacy CollisionResolutionSystem:
Assistant
Step 6: Build and verify
Assistant
Let me also check if there are any other files that reference
DynamicRectVsRectorAtPositionin the collision context that might need updating.Assistant
All callers are already updated or don't need changes. Let me also check
TransformPhysicalCollisionResolutionSystemsince it's a subclass of the resolution system:Assistant
Good, that's just a thin filter subclass - no changes needed. Now let me build:
Assistant
Build succeeds with 0 errors (same pre-existing warnings). Let me also build the full solution to make sure the YarnSpinner and Tests projects aren't broken:
Assistant
Full solution builds clean - 0 errors across all projects.
Assistant
All done. Here's a summary of the changes:
Changes Made
-
New file:
MonoDreams/Extensions/Monogame/CollisionRect.cs— Float-precision AABB struct that replacesRectanglein collision math. HasFromBounds(Rectangle, Vector2)factory that adds float entity position to integer bounds offset without truncation. -
MonoDreams.Examples/System/Collision/TransformCollisionDetectionSystem.cs— Replaced allRectangleusage withCollisionRect(bounds.AtPosition()→CollisionRect.FromBounds()), updatedRayVsRectand signatures, and added check (line 117) to reject negative contact times from sub-pixel overlaps.
-
- User
Well, snapping is gone but so it collision resolution. Detection seems to work but it identifies ghost collisions (at 0;0) as well: Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:32,74848 Y:0,10790014} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0} Collision detected: Entity 2:15.0 vs Entity 2:11.0 at {X:0 Y:0} Collision detected: Entity 2:11.0 vs Entity 2:15.0 at {X:0 Y:0}
Assistant
Let me read the existing plan file and investigate the issues.
Assistant
Now let me explore the current state of the code and the two issues: broken resolution and ghost collisions.
Assistant
Good, the investigation confirms two clear issues. Let me design the fix with a Plan agent.
Assistant
The analysis confirms the approach. Now let me update the plan file.
Assistant
The fix is minimal — 4 line changes across 2 files. The key insight:
contactTime >= 0fneeds to be a detection-layer filter (before publishing), not baked into the geometric test that both detection and resolution share.- User
[Request interrupted by user for tool use]