Skip to content

Commit

Permalink
refactor: Don't use static for Distance and Collision (#92)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
spydon committed Aug 6, 2024
1 parent d05b0ed commit 130994e
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 100 deletions.
13 changes: 4 additions & 9 deletions packages/forge2d/example/web/ball_cage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ class BallCage extends Demo {
/// Constructs a new BallCage.
BallCage() : super('Ball cage');

/// Entrypoint.
static void main() {
final cage = BallCage();
cage.initialize();
cage.initializeAnimation();
cage.runAnimation();
}

@override
void initialize() {
// Define the circle shape.
Expand Down Expand Up @@ -91,5 +83,8 @@ class BallCage extends Demo {
}

void main() {
BallCage.main();
final cage = BallCage();
cage.initialize();
cage.initializeAnimation();
cage.runAnimation();
}
13 changes: 4 additions & 9 deletions packages/forge2d/example/web/blob_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ class BlobTest extends Demo {
/// Constructs a new BlobTest.
BlobTest() : super('Blob test');

/// Entrypoint.
static void main() {
final blob = BlobTest();
blob.initialize();
blob.initializeAnimation();
blob.runAnimation();
}

@override
void initialize() {
Body ground;
Expand Down Expand Up @@ -79,5 +71,8 @@ class BlobTest extends Demo {
}

void main() {
BlobTest.main();
final blob = BlobTest();
blob.initialize();
blob.initializeAnimation();
blob.runAnimation();
}
13 changes: 4 additions & 9 deletions packages/forge2d/example/web/box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ class BoxTest extends Demo {
/// Constructs a new BoxTest.
BoxTest() : super('Box test');

/// Entrypoint.
static void main() {
final boxTest = BoxTest();
boxTest.initialize();
boxTest.initializeAnimation();
boxTest.runAnimation();
}

@override
void initialize() {
_createGround();
Expand Down Expand Up @@ -69,5 +61,8 @@ class BoxTest extends Demo {
}

void main() {
BoxTest.main();
final boxTest = BoxTest();
boxTest.initialize();
boxTest.initializeAnimation();
boxTest.runAnimation();
}
13 changes: 4 additions & 9 deletions packages/forge2d/example/web/domino_tower.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ class DominoTower extends Demo {
/// Construct a DominoTower.
DominoTower() : super('Domino tower');

/// Entrypoint.
static void main() {
final tower = DominoTower();
tower.initialize();
tower.initializeAnimation();
tower.runAnimation();
}

void makeDomino(double x, double y, {required bool horizontal}) {
final shape = PolygonShape()
..setAsBoxXY(.5 * dominoWidth, .5 * dominoHeight);
Expand Down Expand Up @@ -142,5 +134,8 @@ class DominoTower extends Demo {
}

void main() {
DominoTower.main();
final tower = DominoTower();
tower.initialize();
tower.initializeAnimation();
tower.runAnimation();
}
15 changes: 5 additions & 10 deletions packages/forge2d/example/web/friction_joint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import 'demo.dart';
class FrictionJointTest extends Demo {
FrictionJointTest() : super('FrictionJoint test');

/// Entrypoint.
static void main() {
final test = FrictionJointTest();
test.initialize();
test.initializeAnimation();
test.debugDraw.appendFlags(DebugDraw.jointBit);
test.runAnimation();
}

late Body _ground;
late FixtureDef _boxFixture;

Expand Down Expand Up @@ -100,5 +91,9 @@ class FrictionJointTest extends Demo {
}

void main() {
FrictionJointTest.main();
final test = FrictionJointTest();
test.initialize();
test.initializeAnimation();
test.debugDraw.appendFlags(DebugDraw.jointBit);
test.runAnimation();
}
13 changes: 4 additions & 9 deletions packages/forge2d/example/web/particles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ class Particles extends Demo {

/// Constructs a new Particles example.
Particles() : super('Particles');

static void main() {
Particles()
..initialize()
..initializeAnimation()
..runAnimation();
}

@override
void initialize() {
// Define the circle shape.
Expand Down Expand Up @@ -103,5 +95,8 @@ class Particles extends Demo {
}

void main() {
Particles.main();
Particles()
..initialize()
..initializeAnimation()
..runAnimation();
}
18 changes: 7 additions & 11 deletions packages/forge2d/example/web/racer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import 'racer/ground_area.dart';
import 'racer/tire.dart';

class Racer extends Demo implements ContactListener {
static void main() {
final racer = Racer();
racer.initialize();
racer.initializeAnimation();
final paragraph = ParagraphElement()
..innerText = 'Use the arrow keys to drive the car';
document.body?.nodes.add(paragraph);
racer.runAnimation();
}

Racer() : super('Racer', Vector2.zero(), 2.5);

late int _controlState;
Expand Down Expand Up @@ -186,5 +176,11 @@ class Racer extends Demo implements ContactListener {
}

void main() {
Racer.main();
final racer = Racer();
racer.initialize();
racer.initializeAnimation();
final paragraph = ParagraphElement()
..innerText = 'Use the arrow keys to drive the car';
document.body?.nodes.add(paragraph);
racer.runAnimation();
}
3 changes: 2 additions & 1 deletion packages/forge2d/lib/src/collision/collision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Collision {
int indexB,
Transform xfA,
Transform xfB,
Distance distance,
) {
_input.proxyA.set(shapeA, indexA);
_input.proxyB.set(shapeB, indexB);
Expand All @@ -107,7 +108,7 @@ class Collision {

_cache.count = 0;

World.distance.compute(_output, _cache, _input);
distance.compute(_output, _cache, _input);
// djm note: anything significant about 10.0f?
return _output.distance < 10.0 * settings.epsilon;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/forge2d/lib/src/collision/time_of_impact.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TimeOfImpact {
/// If you change the time interval, you should call this function again.
/// Note: use Distance to compute the contact point and normal at the time of
/// impact.
void timeOfImpact(TOIOutput output, TOIInput input) {
void timeOfImpact(TOIOutput output, TOIInput input, Distance distance) {
// CCD via the local separating axis method. This seeks progression
// by computing the largest time at which separation is maintained.

Expand Down Expand Up @@ -100,7 +100,7 @@ class TimeOfImpact {
// to get a separating axis
_distanceInput.transformA = _xfA;
_distanceInput.transformB = _xfB;
World.distance.compute(_distanceOutput, _cache, _distanceInput);
distance.compute(_distanceOutput, _cache, _distanceInput);

// If the shapes are overlapped, we give up on continuous collision.
if (_distanceOutput.distance <= 0.0) {
Expand Down
13 changes: 11 additions & 2 deletions packages/forge2d/lib/src/dynamics/contact_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'package:forge2d/forge2d.dart';
/// Delegate of World.
class ContactManager implements PairCallback {
BroadPhase broadPhase;
final Collision collision;
final Distance distance;
final List<Contact> contacts = [];
ContactFilter? contactFilter;
ContactListener? contactListener;

ContactManager(this.broadPhase) {
ContactManager(this.broadPhase, this.collision, this.distance) {
contactFilter = ContactFilter();
}

Expand Down Expand Up @@ -51,7 +53,14 @@ class ContactManager implements PairCallback {
return;
}

final contact = Contact.init(fixtureA, indexA, fixtureB, indexB);
final contact = Contact.fromPair(
fixtureA,
indexA,
fixtureB,
indexB,
collision,
distance,
);

// Insert into the world.
contacts.add(contact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class ChainAndCircleContact extends Contact {
super.indexA,
super.fixtureB,
super.indexB,
super.collision,
super.distance,
) : assert(fixtureA.type == ShapeType.chain),
assert(fixtureB.type == ShapeType.circle);

@override
void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
final chain = fixtureA.shape as ChainShape;
final edge = chain.childEdge(indexA);
World.collision.collideEdgeAndCircle(
collision.collideEdgeAndCircle(
manifold,
edge,
xfA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class ChainAndPolygonContact extends Contact {
super.indexA,
super.fixtureB,
super.indexB,
super.collision,
super.distance,
) : assert(fixtureA.type == ShapeType.chain),
assert(fixtureB.type == ShapeType.polygon);

@override
void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
final chain = fixtureA.shape as ChainShape;
final edge = chain.childEdge(indexA);
World.collision.collideEdgeAndPolygon(
collision.collideEdgeAndPolygon(
manifold,
edge,
xfA,
Expand Down
12 changes: 8 additions & 4 deletions packages/forge2d/lib/src/dynamics/contacts/circle_contact.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import 'package:forge2d/forge2d.dart';

class CircleContact extends Contact {
CircleContact(Fixture fixtureA, Fixture fixtureB)
: assert(fixtureA.type == ShapeType.circle),
CircleContact(
Fixture fixtureA,
Fixture fixtureB,
Collision collision,
Distance distance,
) : assert(fixtureA.type == ShapeType.circle),
assert(fixtureB.type == ShapeType.circle),
super(fixtureA, 0, fixtureB, 0);
super(fixtureA, 0, fixtureB, 0, collision, distance);

@override
void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
World.collision.collideCircles(
collision.collideCircles(
manifold,
fixtureA.shape as CircleShape,
xfA,
Expand Down
Loading

0 comments on commit 130994e

Please sign in to comment.