Skip to content

Commit

Permalink
Fix contact constraints being cleared many times with many backends (#…
Browse files Browse the repository at this point in the history
…424)

# Objective

The `custom_collider` example is currently broken on some runs, because contact constraints are cleared by every instance of `generate_constraints`. If there are multiple collision backends at the same time, they can overwrite each other's results.

## Solution

Clear contact constraints in a separate system that always only has one instance.
  • Loading branch information
Jondolf authored Jul 13, 2024
1 parent 17ea394 commit ac1fd02
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/collision/narrow_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ impl<C: AnyCollider> Plugin for NarrowPhasePlugin<C> {
);

if self.generate_constraints {
if is_first_instance {
// Clear contact constraints.
app.add_systems(
self.schedule,
(|mut constraints: ResMut<ContactConstraints>| {
constraints.clear();
})
.after(NarrowPhaseSet::PostProcess)
.before(NarrowPhaseSet::GenerateConstraints),
);
}

// Generate contact constraints.
app.add_systems(
self.schedule,
Expand Down Expand Up @@ -262,9 +274,6 @@ fn generate_constraints<C: AnyCollider>(
) {
let delta_secs = time.delta_seconds_adjusted();

// Clear contact constraints.
constraints.clear();

// TODO: Parallelize.
for contacts in narrow_phase.collisions.get_internal().values() {
let Ok([collider1, collider2]) = narrow_phase
Expand Down

0 comments on commit ac1fd02

Please sign in to comment.