Skip to content

Commit 130994e

Browse files
authored
refactor: Don't use static for Distance and Collision (#92)
# Description This removes the use of `static` for `Collision` and `Distance` so that multiple worlds can be used simultaneously and old stuff is GCed properly. ## Checklist <!-- Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (`[x]`). This will ensure a smooth and quick review process. --> - [x] The title of my PR starts with a [Conventional Commit] prefix (`fix:`, `feat:`, `docs:` etc). - [x] I have read the [Contributor Guide] and followed the process outlined for submitting PRs. - [x] I have updated/added tests for ALL new/updated/fixed functionality. - [x] I have updated/added relevant documentation in `docs` and added dartdoc comments with `///`. - [x] I have updated/added relevant examples in `examples`. ## Breaking Change <!-- Does your PR require Flame users to manually update their apps to accommodate your change? If the PR is a breaking change this should be indicated with suffix "!" (for example, `feat!:`, `fix!:`). See [Conventional Commit] for details. --> - [x] No, this is *not* a breaking change. ## Related Issues <!-- Provide a list of issues related to this PR from the [issue database]. Indicate which of these issues are resolved or fixed by this PR, i.e. Fixes #xxxx* !--> <!-- Links --> [issue database]: https://github.com/flame-engine/flame/issues [Contributor Guide]: https://github.com/flame-engine/flame/blob/main/CONTRIBUTING.md [Flame Style Guide]: https://github.com/flame-engine/flame/blob/main/STYLEGUIDE.md [Conventional Commit]: https://conventionalcommits.org
1 parent d05b0ed commit 130994e

20 files changed

Lines changed: 135 additions & 100 deletions

packages/forge2d/example/web/ball_cage.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ class BallCage extends Demo {
1616
/// Constructs a new BallCage.
1717
BallCage() : super('Ball cage');
1818

19-
/// Entrypoint.
20-
static void main() {
21-
final cage = BallCage();
22-
cage.initialize();
23-
cage.initializeAnimation();
24-
cage.runAnimation();
25-
}
26-
2719
@override
2820
void initialize() {
2921
// Define the circle shape.
@@ -91,5 +83,8 @@ class BallCage extends Demo {
9183
}
9284

9385
void main() {
94-
BallCage.main();
86+
final cage = BallCage();
87+
cage.initialize();
88+
cage.initializeAnimation();
89+
cage.runAnimation();
9590
}

packages/forge2d/example/web/blob_test.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ class BlobTest extends Demo {
88
/// Constructs a new BlobTest.
99
BlobTest() : super('Blob test');
1010

11-
/// Entrypoint.
12-
static void main() {
13-
final blob = BlobTest();
14-
blob.initialize();
15-
blob.initializeAnimation();
16-
blob.runAnimation();
17-
}
18-
1911
@override
2012
void initialize() {
2113
Body ground;
@@ -79,5 +71,8 @@ class BlobTest extends Demo {
7971
}
8072

8173
void main() {
82-
BlobTest.main();
74+
final blob = BlobTest();
75+
blob.initialize();
76+
blob.initializeAnimation();
77+
blob.runAnimation();
8378
}

packages/forge2d/example/web/box_test.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ class BoxTest extends Demo {
88
/// Constructs a new BoxTest.
99
BoxTest() : super('Box test');
1010

11-
/// Entrypoint.
12-
static void main() {
13-
final boxTest = BoxTest();
14-
boxTest.initialize();
15-
boxTest.initializeAnimation();
16-
boxTest.runAnimation();
17-
}
18-
1911
@override
2012
void initialize() {
2113
_createGround();
@@ -69,5 +61,8 @@ class BoxTest extends Demo {
6961
}
7062

7163
void main() {
72-
BoxTest.main();
64+
final boxTest = BoxTest();
65+
boxTest.initialize();
66+
boxTest.initializeAnimation();
67+
boxTest.runAnimation();
7368
}

packages/forge2d/example/web/domino_tower.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ class DominoTower extends Demo {
1717
/// Construct a DominoTower.
1818
DominoTower() : super('Domino tower');
1919

20-
/// Entrypoint.
21-
static void main() {
22-
final tower = DominoTower();
23-
tower.initialize();
24-
tower.initializeAnimation();
25-
tower.runAnimation();
26-
}
27-
2820
void makeDomino(double x, double y, {required bool horizontal}) {
2921
final shape = PolygonShape()
3022
..setAsBoxXY(.5 * dominoWidth, .5 * dominoHeight);
@@ -142,5 +134,8 @@ class DominoTower extends Demo {
142134
}
143135

144136
void main() {
145-
DominoTower.main();
137+
final tower = DominoTower();
138+
tower.initialize();
139+
tower.initializeAnimation();
140+
tower.runAnimation();
146141
}

packages/forge2d/example/web/friction_joint_test.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ import 'demo.dart';
77
class FrictionJointTest extends Demo {
88
FrictionJointTest() : super('FrictionJoint test');
99

10-
/// Entrypoint.
11-
static void main() {
12-
final test = FrictionJointTest();
13-
test.initialize();
14-
test.initializeAnimation();
15-
test.debugDraw.appendFlags(DebugDraw.jointBit);
16-
test.runAnimation();
17-
}
18-
1910
late Body _ground;
2011
late FixtureDef _boxFixture;
2112

@@ -100,5 +91,9 @@ class FrictionJointTest extends Demo {
10091
}
10192

10293
void main() {
103-
FrictionJointTest.main();
94+
final test = FrictionJointTest();
95+
test.initialize();
96+
test.initializeAnimation();
97+
test.debugDraw.appendFlags(DebugDraw.jointBit);
98+
test.runAnimation();
10499
}

packages/forge2d/example/web/particles.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ class Particles extends Demo {
1515

1616
/// Constructs a new Particles example.
1717
Particles() : super('Particles');
18-
19-
static void main() {
20-
Particles()
21-
..initialize()
22-
..initializeAnimation()
23-
..runAnimation();
24-
}
25-
2618
@override
2719
void initialize() {
2820
// Define the circle shape.
@@ -103,5 +95,8 @@ class Particles extends Demo {
10395
}
10496

10597
void main() {
106-
Particles.main();
98+
Particles()
99+
..initialize()
100+
..initializeAnimation()
101+
..runAnimation();
107102
}

packages/forge2d/example/web/racer.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import 'racer/ground_area.dart';
1010
import 'racer/tire.dart';
1111

1212
class Racer extends Demo implements ContactListener {
13-
static void main() {
14-
final racer = Racer();
15-
racer.initialize();
16-
racer.initializeAnimation();
17-
final paragraph = ParagraphElement()
18-
..innerText = 'Use the arrow keys to drive the car';
19-
document.body?.nodes.add(paragraph);
20-
racer.runAnimation();
21-
}
22-
2313
Racer() : super('Racer', Vector2.zero(), 2.5);
2414

2515
late int _controlState;
@@ -186,5 +176,11 @@ class Racer extends Demo implements ContactListener {
186176
}
187177

188178
void main() {
189-
Racer.main();
179+
final racer = Racer();
180+
racer.initialize();
181+
racer.initializeAnimation();
182+
final paragraph = ParagraphElement()
183+
..innerText = 'Use the arrow keys to drive the car';
184+
document.body?.nodes.add(paragraph);
185+
racer.runAnimation();
190186
}

packages/forge2d/lib/src/collision/collision.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class Collision {
9898
int indexB,
9999
Transform xfA,
100100
Transform xfB,
101+
Distance distance,
101102
) {
102103
_input.proxyA.set(shapeA, indexA);
103104
_input.proxyB.set(shapeB, indexB);
@@ -107,7 +108,7 @@ class Collision {
107108

108109
_cache.count = 0;
109110

110-
World.distance.compute(_output, _cache, _input);
111+
distance.compute(_output, _cache, _input);
111112
// djm note: anything significant about 10.0f?
112113
return _output.distance < 10.0 * settings.epsilon;
113114
}

packages/forge2d/lib/src/collision/time_of_impact.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TimeOfImpact {
5353
/// If you change the time interval, you should call this function again.
5454
/// Note: use Distance to compute the contact point and normal at the time of
5555
/// impact.
56-
void timeOfImpact(TOIOutput output, TOIInput input) {
56+
void timeOfImpact(TOIOutput output, TOIInput input, Distance distance) {
5757
// CCD via the local separating axis method. This seeks progression
5858
// by computing the largest time at which separation is maintained.
5959

@@ -100,7 +100,7 @@ class TimeOfImpact {
100100
// to get a separating axis
101101
_distanceInput.transformA = _xfA;
102102
_distanceInput.transformB = _xfB;
103-
World.distance.compute(_distanceOutput, _cache, _distanceInput);
103+
distance.compute(_distanceOutput, _cache, _distanceInput);
104104

105105
// If the shapes are overlapped, we give up on continuous collision.
106106
if (_distanceOutput.distance <= 0.0) {

packages/forge2d/lib/src/dynamics/contact_manager.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import 'package:forge2d/forge2d.dart';
33
/// Delegate of World.
44
class ContactManager implements PairCallback {
55
BroadPhase broadPhase;
6+
final Collision collision;
7+
final Distance distance;
68
final List<Contact> contacts = [];
79
ContactFilter? contactFilter;
810
ContactListener? contactListener;
911

10-
ContactManager(this.broadPhase) {
12+
ContactManager(this.broadPhase, this.collision, this.distance) {
1113
contactFilter = ContactFilter();
1214
}
1315

@@ -51,7 +53,14 @@ class ContactManager implements PairCallback {
5153
return;
5254
}
5355

54-
final contact = Contact.init(fixtureA, indexA, fixtureB, indexB);
56+
final contact = Contact.fromPair(
57+
fixtureA,
58+
indexA,
59+
fixtureB,
60+
indexB,
61+
collision,
62+
distance,
63+
);
5564

5665
// Insert into the world.
5766
contacts.add(contact);

0 commit comments

Comments
 (0)