feat: add Box2D physics2d plugin with CGO
Merged
AN
7mo ago+46650+46650+01234567890123456789012345678901234567890123456789-0-0-0123456789
Add 2D Physics Plugin with Box2D Integration
This pull request introduces a comprehensive 2D physics plugin for Cardinal using Box2D as the simulation backend.
Components
- Transform2D: World-space position and rotation
- Velocity2D: Linear and angular velocity
- Rigidbody2D: Body type (static/dynamic/kinematic) and simulation parameters
- Collider2D: Compound collider with multiple shapes (circle, box, polygon, chain)
- ActiveContacts: Singleton component tracking active contact pairs for event deduplication
Core Systems
- InitPhysicsSystem: Creates Box2D world and performs initial ECS→Box2D rebuild
- ReconcilePhysicsSystem: Incrementally syncs ECS changes to Box2D using shadow-copy diffing
- PhysicsStepSystem: Steps the simulation and emits contact/trigger events
Contact Events
- ContactBeginEvent/ContactEndEvent: Non-sensor collision events
- TriggerBeginEvent/TriggerEndEvent: Sensor overlap events
- Proper event deduplication after world rebuilds using persisted contact state
Query API
- Raycast: Cast rays through the physics world
- OverlapAABB: Find fixtures overlapping an axis-aligned bounding box
- CircleSweep: Sweep a circle along a path for continuous collision detection
Runtime Management
- Shadow-copy reconciliation for efficient incremental updates
- Structural vs mutable change detection (fixture recreation vs in-place updates)
- Deterministic rebuild ordering and contact event handling
- Proper cleanup of orphaned bodies and contact pairs
The plugin integrates seamlessly with Cardinal's ECS architecture while providing a complete 2D physics simulation with collision detection, rigid body dynamics, and comprehensive query capabilities.