Skip to content

feat!: Migrate flame_forge2d to the Box2D v3 based forge2d#3952

Open
spydon wants to merge 21 commits into
mainfrom
forge2d-box2d-v3
Open

feat!: Migrate flame_forge2d to the Box2D v3 based forge2d#3952
spydon wants to merge 21 commits into
mainfrom
forge2d-box2d-v3

Conversation

@spydon

@spydon spydon commented Jul 19, 2026

Copy link
Copy Markdown
Member

Description

Migrates flame_forge2d (and everything in the monorepo that uses it) to the new forge2d, the
native Box2D v3.1.1 bindings that are currently in review in the stacked chain
flame-engine/forge2d#115 (the native rewrite) and flame-engine/forge2d#116 (web support through a
WebAssembly build). This both prepares the migration and serves as real-world validation of the new
forge2d API while that chain is in review.

flame_forge2d now depends on the published forge2d: ^0.15.0. flame_forge2d stays in the
customer_testing.dart exclusions, because forge2d compiles Box2D from source through the Dart
build hooks and so needs a C toolchain on the flutter/flutter presubmit runner; the reasoning is
documented next to the exclusion.

Core package

  • Forge2DWorld steps the world with physicsWorld.step(dt, subStepCount: subStepCount) and then
    dispatches the polled contact and sensor events through the new overridable
    ContactEventsDispatcher (replacing WorldContactListener, since listener interfaces no longer
    exist). It keeps a Dart-side bodies set (upstream no longer exposes one) which the gravity
    setter uses to wake bodies, and exposes the new query API (castRayClosest, castRay,
    castRayAll, overlapAabb) plus forwarding setters for preSolveCallback and
    customFilterCallback.
  • ContactCallbacks keeps its familiar beginContact(Object other, Contact contact) shape through
    a new lightweight flame-side Contact class that wraps both contact and sensor events.
  • BodyComponent renders from the new Shape.geometry read-back (Circle, Capsule, Segment,
    Polygon; chain segments arrive as Segments), with renderShape/renderSegment and a new
    renderCapsule. fixtureDefs is replaced by shapeSpecs (a list of ShapeSpec, pairing a
    ShapeGeometry with an optional ShapeDef). The default createBody() auto-enables
    contact/sensor events on shapes whose body or shape userData is a ContactCallbacks, since the
    new engine only generates events for shapes that opted in.
  • SDK floors raised to Dart >=3.12.0 / Flutter >=3.44.0 (root workspace, melos bootstrap,
    package, and FLUTTER_MIN_VERSION in CI).
  • Forge2DGame awaits initializeForge2D() in its onLoad, and Forge2DWorld creates its
    physics world lazily so that this can happen first. Without it every Forge2DGame throws on the
    web, since that call is what loads the Box2D WebAssembly module. Code that creates a world
    outside of a Forge2DGame has to await it itself.
  • Tests rewritten against real physics worlds (mocked Fixture/Contact/Manifold are gone) and
    all goldens regenerated. flame's MultiTapDispatcher.handleTapDown annotation changed from
    @internal to @visibleForTesting so the tests can use it without ignores.

Examples and docs

  • The examples stories, padracing, and the package example are migrated. The examples for joints
    that no longer exist in Box2D v3 (gear, pulley, rope, friction, constant-volume) and the blob
    example are removed.
  • doc/bridge_packages/flame_forge2d/forge2d.md and joints.md are rewritten for the new API
    (including the new filter and wheel joints).

Verification

  • flutter test in packages/flame_forge2d (45 tests, compiles native Box2D through build hooks),
    goldens visually inspected.
  • dart analyze clean across the whole workspace.
  • flutter build web of the examples app confirms the Box2D wasm module is bundled automatically
    at the package asset path.

Notes for the forge2d review (found during this migration)

  • Shape.geometry read-back was added upstream during this work and is what makes
    BodyComponent rendering possible without a Dart-side geometry registry.
  • There is no upstream way to enumerate a world's bodies, hence the Dart-side set in
    Forge2DWorld.
  • Behavior change to be aware of: destroying a body clears its userData registries, so removed
    BodyComponents no longer receive a final endContact for contacts that end due to the
    destruction (the old engine fired those synchronously inside destroy).

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Migration instructions

  • Fixture and FixtureDef are gone: create shapes with
    body.createShape(geometry, ShapeDef(...)), where the geometry is a Circle, Capsule,
    Segment, or Polygon. Friction and restitution now live in ShapeDef.material
    (a SurfaceMaterial). body.fixtures becomes body.shapes.
  • BodyComponent.fixtureDefs becomes shapeSpecs, a list of ShapeSpec(geometry, [shapeDef]).
    renderFixture becomes renderShape, renderEdge becomes renderSegment, and renderChain
    is gone (chain segments render as segments).
  • Shape construction: CircleShape()..radius = r becomes Circle(radius: r, center: c),
    EdgeShape()..set(a, b) becomes Segment(point1: a, point2: b),
    PolygonShape()..setAsBoxXY(w, h) becomes Polygon.box(w, h), and
    ChainShape()..createChain/createLoop becomes body.createChain(ChainDef(points: ..., isLoop: ...)) (chains now require at least four points; the first and last points of an open chain are
    ghost anchors).
  • BodyDef.angle becomes BodyDef(rotation: Rot.fromAngle(angle)).
  • Contact events are now opt-in per shape: set ShapeDef.enableContactEvents (and
    enableSensorEvents for sensors and their visitors). The default BodyComponent.createBody()
    enables them automatically when a ContactCallbacks is present in the body's or shape's
    userData.
  • ContactCallbacks.beginContact/endContact keep their signatures but receive the new flame-side
    Contact (with shapeA, shapeB, bodyA, bodyB, begin-only normal/points, and
    isSensorEvent). preSolve/postSolve are removed: use Forge2DWorld.preSolveCallback with
    ShapeDef.enablePreSolveEvents, and hit events (ShapeDef.enableHitEvents +
    world.physicsWorld.contactEvents.hit) respectively.
  • WorldContactListener is replaced by ContactEventsDispatcher, and the contactListener
    parameter of Forge2DGame by contactEventsDispatcher.
  • Joints are now created with typed methods and destroyed on the joint:
    world.physicsWorld.createRevoluteJoint(RevoluteJointDef(bodyA: ..., bodyB: ...)) and
    joint.destroy(). The available joints are distance, filter, motor, mouse, prismatic, revolute,
    weld, and wheel; gear, pulley, rope, friction, and constant-volume joints no longer exist. The
    def initialize helpers are gone; anchors are passed as local points
    (body.localPoint(worldAnchor)).
  • Queries: world.raycast(callback, p1, p2) becomes castRayClosest/castRay/castRayAll
    (taking an origin and a translation), queryAABB becomes overlapAabb, and clearForces and
    the particle system are removed.
  • Body API renames: worldCenter becomes worldCenterOfMass, setAwake(x) becomes isAwake = x,
    getInertia() becomes rotationalInertia, and worldVector(v) becomes rotation.rotate(v).
  • Platform requirements: Dart 3.12+ / Flutter 3.44+, a C toolchain when building for native
    platforms, and nothing extra on the web (the Box2D wasm module is bundled automatically).

Related Issues

Depends on flame-engine/forge2d#115 and flame-engine/forge2d#116.

@spydon
spydon force-pushed the forge2d-box2d-v3 branch from 3778fcb to af1b435 Compare July 19, 2026 22:49
spydon and others added 16 commits July 20, 2026 21:24
Every widget of the default flutter create counter app rests on a
Forge2D body and drops to the floor, keeping its normal function.
Pressing the button counts and launches it, and pressing any widget
sends it flying. The bodies follow the shape Material draws: capsules
for the text, a rounded box for the button. Every widget is given the
same mass so the wide app bar does not crush the small counter, and the
walls are solid boxes so nothing squeezes out through a corner.
Add a heroTag to the counter button, drop a redundant trailing zero in
hue_decorator, teach cspell the word subclassing and use the en_US
spelling of neighboring.
@spydon
spydon marked this pull request as ready for review July 21, 2026 12:03
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