Skip to content

feat: add Box2D physics2d plugin with components, systems, and queries#880

Open
AnthonyWadham wants to merge 1 commit intomainfrom
Anthony/physics2d-components
Open

feat: add Box2D physics2d plugin with components, systems, and queries#880
AnthonyWadham wants to merge 1 commit intomainfrom
Anthony/physics2d-components

Conversation

@AnthonyWadham
Copy link
Copy Markdown
Contributor

@AnthonyWadham AnthonyWadham commented Mar 31, 2026

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.

Copy link
Copy Markdown
Contributor Author

AnthonyWadham commented Mar 31, 2026


How to use the Graphite Merge Queue

Add the label graphite/merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@AnthonyWadham AnthonyWadham changed the title feat: physics2d component setup feat: add Box2D physics2d plugin with components, systems, and queries Mar 31, 2026
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch 3 times, most recently from 349a200 to a46fb0f Compare April 1, 2026 19:57
@AnthonyWadham AnthonyWadham marked this pull request as ready for review April 1, 2026 20:02
@AnthonyWadham AnthonyWadham requested a review from rmrt1n April 1, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant