feat!: Migrate flame_forge2d to the Box2D v3 based forge2d#3952
Open
spydon wants to merge 21 commits into
Open
feat!: Migrate flame_forge2d to the Box2D v3 based forge2d#3952spydon wants to merge 21 commits into
spydon wants to merge 21 commits into
Conversation
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
marked this pull request as ready for review
July 21, 2026 12:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Migrates
flame_forge2d(and everything in the monorepo that uses it) to the new forge2d, thenative 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_forge2dnow depends on the publishedforge2d: ^0.15.0.flame_forge2dstays in thecustomer_testing.dartexclusions, because forge2d compiles Box2D from source through the Dartbuild hooks and so needs a C toolchain on the flutter/flutter presubmit runner; the reasoning is
documented next to the exclusion.
Core package
Forge2DWorldsteps the world withphysicsWorld.step(dt, subStepCount: subStepCount)and thendispatches the polled contact and sensor events through the new overridable
ContactEventsDispatcher(replacingWorldContactListener, since listener interfaces no longerexist). It keeps a Dart-side
bodiesset (upstream no longer exposes one) which the gravitysetter uses to wake bodies, and exposes the new query API (
castRayClosest,castRay,castRayAll,overlapAabb) plus forwarding setters forpreSolveCallbackandcustomFilterCallback.ContactCallbackskeeps its familiarbeginContact(Object other, Contact contact)shape througha new lightweight flame-side
Contactclass that wraps both contact and sensor events.BodyComponentrenders from the newShape.geometryread-back (Circle,Capsule,Segment,Polygon; chain segments arrive asSegments), withrenderShape/renderSegmentand a newrenderCapsule.fixtureDefsis replaced byshapeSpecs(a list ofShapeSpec, pairing aShapeGeometrywith an optionalShapeDef). The defaultcreateBody()auto-enablescontact/sensor events on shapes whose body or shape userData is a
ContactCallbacks, since thenew engine only generates events for shapes that opted in.
>=3.12.0/ Flutter>=3.44.0(root workspace, melos bootstrap,package, and
FLUTTER_MIN_VERSIONin CI).Forge2DGameawaitsinitializeForge2D()in itsonLoad, andForge2DWorldcreates itsphysics world lazily so that this can happen first. Without it every
Forge2DGamethrows on theweb, since that call is what loads the Box2D WebAssembly module. Code that creates a world
outside of a
Forge2DGamehas to await it itself.Fixture/Contact/Manifoldare gone) andall goldens regenerated.
flame'sMultiTapDispatcher.handleTapDownannotation changed from@internalto@visibleForTestingso the tests can use it without ignores.Examples and docs
padracing, and the package example are migrated. The examples for jointsthat no longer exist in Box2D v3 (gear, pulley, rope, friction, constant-volume) and the blob
example are removed.
doc/bridge_packages/flame_forge2d/forge2d.mdandjoints.mdare rewritten for the new API(including the new filter and wheel joints).
Verification
flutter testinpackages/flame_forge2d(45 tests, compiles native Box2D through build hooks),goldens visually inspected.
dart analyzeclean across the whole workspace.flutter build webof the examples app confirms the Box2D wasm module is bundled automaticallyat the package asset path.
Notes for the forge2d review (found during this migration)
Shape.geometryread-back was added upstream during this work and is what makesBodyComponentrendering possible without a Dart-side geometry registry.Forge2DWorld.BodyComponents no longer receive a finalendContactfor contacts that end due to thedestruction (the old engine fired those synchronously inside destroy).
Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Migration instructions
FixtureandFixtureDefare gone: create shapes withbody.createShape(geometry, ShapeDef(...)), where the geometry is aCircle,Capsule,Segment, orPolygon. Friction and restitution now live inShapeDef.material(a
SurfaceMaterial).body.fixturesbecomesbody.shapes.BodyComponent.fixtureDefsbecomesshapeSpecs, a list ofShapeSpec(geometry, [shapeDef]).renderFixturebecomesrenderShape,renderEdgebecomesrenderSegment, andrenderChainis gone (chain segments render as segments).
CircleShape()..radius = rbecomesCircle(radius: r, center: c),EdgeShape()..set(a, b)becomesSegment(point1: a, point2: b),PolygonShape()..setAsBoxXY(w, h)becomesPolygon.box(w, h), andChainShape()..createChain/createLoopbecomesbody.createChain(ChainDef(points: ..., isLoop: ...))(chains now require at least four points; the first and last points of an open chain areghost anchors).
BodyDef.anglebecomesBodyDef(rotation: Rot.fromAngle(angle)).ShapeDef.enableContactEvents(andenableSensorEventsfor sensors and their visitors). The defaultBodyComponent.createBody()enables them automatically when a
ContactCallbacksis present in the body's or shape'suserData.
ContactCallbacks.beginContact/endContactkeep their signatures but receive the new flame-sideContact(withshapeA,shapeB,bodyA,bodyB, begin-onlynormal/points, andisSensorEvent).preSolve/postSolveare removed: useForge2DWorld.preSolveCallbackwithShapeDef.enablePreSolveEvents, and hit events (ShapeDef.enableHitEvents+world.physicsWorld.contactEvents.hit) respectively.WorldContactListeneris replaced byContactEventsDispatcher, and thecontactListenerparameter of
Forge2DGamebycontactEventsDispatcher.world.physicsWorld.createRevoluteJoint(RevoluteJointDef(bodyA: ..., bodyB: ...))andjoint.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
initializehelpers are gone; anchors are passed as local points(
body.localPoint(worldAnchor)).world.raycast(callback, p1, p2)becomescastRayClosest/castRay/castRayAll(taking an origin and a translation),
queryAABBbecomesoverlapAabb, andclearForcesandthe particle system are removed.
worldCenterbecomesworldCenterOfMass,setAwake(x)becomesisAwake = x,getInertia()becomesrotationalInertia, andworldVector(v)becomesrotation.rotate(v).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.