Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class DynamicTree implements BroadPhaseStrategy {

/// returns a node to the pool
void _freeNode(DynamicTreeNode node) {
assert(0 < _nodeCount);
assert(_nodeCount > 0);
node.parent = _freeList != nullNode ? _nodes[_freeList] : null;
node.height = -1;
_freeList = node.id;
Expand Down
11 changes: 5 additions & 6 deletions packages/forge2d/lib/src/dynamics/contact_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ class ContactManager implements PairCallback {
final Collision collision;
final Distance distance;
final List<Contact> contacts = [];
ContactFilter? contactFilter;
ContactFilter contactFilter;
ContactListener? contactListener;

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

/// Broad-phase callback.
@override
Expand Down Expand Up @@ -49,7 +48,7 @@ class ContactManager implements PairCallback {
}

// Check user filtering.
if (contactFilter?.shouldCollide(fixtureA, fixtureB) == false) {
if (contactFilter.shouldCollide(fixtureA, fixtureB) == false) {
return;
}

Expand Down Expand Up @@ -122,7 +121,7 @@ class ContactManager implements PairCallback {
}

// Check user filtering.
if (contactFilter?.shouldCollide(fixtureA, fixtureB) == false) {
if (contactFilter.shouldCollide(fixtureA, fixtureB) == false) {
contactRemovals.add(c);
continue;
}
Expand Down
41 changes: 31 additions & 10 deletions packages/forge2d/lib/src/dynamics/world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class World {
late ContactManager contactManager;
final List<Body> bodies = <Body>[];
final List<Joint> joints = <Joint>[];
final List<Body> bodiesToCreate = <Body>[];
final List<Body> bodiesToDestroy = <Body>[];
final List<Joint> jointsToCreate = <Joint>[];
final List<Joint> jointsToDestroy = <Joint>[];

final Vector2 _gravity;

Expand All @@ -37,7 +41,7 @@ class World {
///
/// See also:
///
/// * [Body.gravityScale], to multipy [gravity] for a [Body].
/// * [Body.gravityScale], to multiply [gravity] for a [Body].
/// * [Body.gravityOverride], to change how the world treats the gravity for
/// a [Body].
/// {@endtemplate}
Expand Down Expand Up @@ -133,8 +137,11 @@ class World {
///
/// Warning: This function is locked during callbacks.
Body createBody(BodyDef def) {
assert(!isLocked);
final body = Body(def, this);
if (isLocked) {
bodiesToCreate.add(body);
return body;
}
bodies.add(body);
return body;
}
Expand All @@ -145,8 +152,10 @@ class World {
/// Warning: This automatically deletes all associated shapes and joints.
/// Warning: This function is locked during callbacks.
void destroyBody(Body body) {
assert(bodies.isNotEmpty);
assert(!isLocked);
if (isLocked) {
bodiesToDestroy.add(body);
return;
}

// Delete the attached joints.
while (body.joints.isNotEmpty) {
Expand All @@ -173,10 +182,11 @@ class World {
/// This may cause the connected bodies to cease colliding.
///
/// Adding a joint doesn't wake up the bodies.
///
/// Warning: This function is locked during callbacks.
void createJoint(Joint joint) {
assert(!isLocked);
if (isLocked) {
jointsToCreate.add(joint);
return;
}
joints.add(joint);

final bodyA = joint.bodyA;
Expand All @@ -197,10 +207,11 @@ class World {
}

/// Destroys a joint. This may cause the connected bodies to begin colliding.
///
/// Warning: This function is locked during callbacks.
void destroyJoint(Joint joint) {
assert(!isLocked);
if (isLocked) {
jointsToDestroy.add(joint);
return;
}

final collideConnected = joint.collideConnected;
joints.remove(joint);
Expand Down Expand Up @@ -297,6 +308,16 @@ class World {

flags &= ~locked;

for (final body in bodiesToCreate) {
bodies.add(body);
}
bodiesToCreate.clear();

for (final body in bodiesToDestroy) {
destroyBody(body);
}
bodiesToDestroy.clear();

_profile.step.record(_stepTimer.getMilliseconds());
}

Expand Down
2 changes: 1 addition & 1 deletion packages/forge2d/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
sdk: ">=3.8.0 <4.0.0"

dependencies:
meta: ^1.17.0
meta: ^1.16.0
vector_math: ^2.2.0
web: ^1.1.1

Expand Down