From ac1fd0269e04f19f6892c62019cbb9ad9c19444e Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Sat, 13 Jul 2024 20:03:23 +0300 Subject: [PATCH] Fix contact constraints being cleared many times with many backends (#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. --- src/collision/narrow_phase.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/collision/narrow_phase.rs b/src/collision/narrow_phase.rs index 4d5200c0..f3117071 100644 --- a/src/collision/narrow_phase.rs +++ b/src/collision/narrow_phase.rs @@ -135,6 +135,18 @@ impl Plugin for NarrowPhasePlugin { ); if self.generate_constraints { + if is_first_instance { + // Clear contact constraints. + app.add_systems( + self.schedule, + (|mut constraints: ResMut| { + constraints.clear(); + }) + .after(NarrowPhaseSet::PostProcess) + .before(NarrowPhaseSet::GenerateConstraints), + ); + } + // Generate contact constraints. app.add_systems( self.schedule, @@ -262,9 +274,6 @@ fn generate_constraints( ) { 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