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

Optimize wake_on_collision_ended when large number of collisions are occurring #508

Merged
Merged
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
28 changes: 21 additions & 7 deletions src/dynamics/sleeping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,18 @@ fn wake_on_collision_ended(
moved_bodies: Query<Ref<Position>, (Changed<Position>, Without<Sleeping>)>,
colliders: Query<(&ColliderParent, Ref<ColliderTransform>)>,
collisions: Res<Collisions>,
mut sleeping: Query<(Entity, &mut TimeSleeping)>,
mut sleeping: Query<(Entity, &mut TimeSleeping, Has<Sleeping>)>,
) {
// Wake up bodies when a body they're colliding with moves.
for (entity, mut time_sleeping) in &mut sleeping {
for (entity, mut time_sleeping, is_sleeping) in &mut sleeping {
// Skip anything that isn't currently sleeping and already has a time_sleeping of zero.
// We can't gate the sleeping query using With<Sleeping> here because must also reset
// non-zero time_sleeping to 0 when a colliding body moves.
let must_check = is_sleeping || time_sleeping.0 > 0.0;
if !must_check {
continue;
}

// Here we could use CollidingEntities, but it'd be empty if the ContactReportingPlugin was disabled.
let mut colliding_entities = collisions.collisions_with_entity(entity).map(|c| {
if entity == c.entity1 {
Expand All @@ -283,7 +291,9 @@ fn wake_on_collision_ended(
|| moved_bodies.get(p.get()).is_ok_and(|pos| pos.is_changed())
})
}) {
commands.entity(entity).remove::<Sleeping>();
if is_sleeping {
commands.entity(entity).remove::<Sleeping>();
}
time_sleeping.0 = 0.0;
}
}
Expand All @@ -293,12 +303,16 @@ fn wake_on_collision_ended(
if contacts.during_current_frame || !contacts.during_previous_frame {
continue;
}
if let Ok((_, mut time_sleeping)) = sleeping.get_mut(contacts.entity1) {
commands.entity(contacts.entity1).remove::<Sleeping>();
if let Ok((_, mut time_sleeping, is_sleeping)) = sleeping.get_mut(contacts.entity1) {
if is_sleeping {
commands.entity(contacts.entity1).remove::<Sleeping>();
}
time_sleeping.0 = 0.0;
}
if let Ok((_, mut time_sleeping)) = sleeping.get_mut(contacts.entity2) {
commands.entity(contacts.entity2).remove::<Sleeping>();
if let Ok((_, mut time_sleeping, is_sleeping)) = sleeping.get_mut(contacts.entity2) {
if is_sleeping {
commands.entity(contacts.entity2).remove::<Sleeping>();
}
time_sleeping.0 = 0.0;
}
}
Expand Down