Skip to content

feat: add Box2D physics2d plugin with CGO#880

Merged
AnthonyWadham merged 1 commit into
mainfrom
Anthony/physics2d-components
Apr 23, 2026
Merged

feat: add Box2D physics2d plugin with CGO#880
AnthonyWadham merged 1 commit into
mainfrom
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
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch from a46fb0f to e015af6 Compare April 1, 2026 20:06
Comment thread pkg/plugin/physics2d/system/init.go Outdated
Comment thread pkg/plugin/physics2d/system/singleton.go Outdated
Comment thread pkg/plugin/physics2d/system/init.go
Comment thread pkg/plugin/physics2d/system/init.go Outdated
Comment thread pkg/plugin/physics2d/system/reconcile.go Outdated
Comment thread pkg/plugin/physics2d/system/pipeline.go
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch from e015af6 to f44dd62 Compare April 7, 2026 16:03
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch 3 times, most recently from ec0e666 to cec88d5 Compare April 13, 2026 17:04
@AnthonyWadham AnthonyWadham changed the title feat: add Box2D physics2d plugin with components, systems, and queries feat: add Box2D physics2d plugin with CGO Apr 13, 2026
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch 3 times, most recently from f33d00c to fa76ad0 Compare April 15, 2026 16:57
@AnthonyWadham AnthonyWadham force-pushed the Anthony/physics2d-components branch from fa76ad0 to cc30361 Compare April 15, 2026 19:07
Copy link
Copy Markdown

@darkmavis darkmavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As was just disccussed, I'm fine with accepting that for a change with this level of complexity, we simply need to merge and test in the real-world. I'd be shocked if we didn't encounter issues, but that's ok. We'll find out what they are and fix them.

In some ways, this carries less risk than the initial decision to use an unofficial port of Box2D from C to Go, and for that, we had to real-world test and find out if it was stable and deterministic, and what to avoid if anything. We did and it worked. We can do the same here, but with more confidence that we're picking up the latest changes and nothing in the core library was lost in translation.

fwiw - I reviewed code with help from Claude. The gameplay test footage is enough of a sanity check for me. I'm not expecting perfection, and if we get it, great.

Also stoked for the performance and stability improvements provided by v3. Thanks for thinking to do this and making it happen.

I'm fine with having this merged in once Ryan's requested changes are resolved.

Comment thread pkg/plugin/physics2d/internal/writeback.go
@AnthonyWadham AnthonyWadham merged commit ae574a1 into main Apr 23, 2026
9 checks passed
@AnthonyWadham AnthonyWadham deleted the Anthony/physics2d-components branch April 23, 2026 20:14
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.

4 participants