Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Don't use static for Distance and Collision #92

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Comment on lines +65 to +67
Copy link
Member

Choose a reason for hiding this comment

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

since we always call the same 3 methods, shouldn't Demo expose a helper for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, there are looots of fixes to do here, but this was just a minor side fix in the PR (to remove static main)

}
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();
Copy link
Member

Choose a reason for hiding this comment

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

different style, same purpose?

}
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
Loading