Skip to content

Commit 072ec52

Browse files
authored
Fix ColliderOf updates when a rigid body is added in the hierarchy (#682)
# Objective When a `RigidBody` is added to a hierarchy that already has existing colliders, the `ColliderOf` relationships aren't updated correctly. ## Solution Fix `ColliderOf` updates to also consider newly added rigid bodies.
1 parent 3023578 commit 072ec52

File tree

1 file changed

+12
-7
lines changed
  • src/collision/collider/collider_hierarchy

1 file changed

+12
-7
lines changed

src/collision/collider/collider_hierarchy/plugin.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ impl Plugin for ColliderHierarchyPlugin {
4949
}
5050
}
5151

52-
/// Updates [`ColliderOf`] components for colliders when their ancestors change.
52+
/// Updates [`ColliderOf`] components for colliders when their ancestors change
53+
/// or when a rigid body is added to the hierarchy.
5354
fn on_collider_rigid_body_changed(
54-
trigger: Trigger<OnInsert, (ChildOf, AncestorMarker<ColliderMarker>)>,
55+
trigger: Trigger<OnInsert, (ChildOf, RigidBody, AncestorMarker<ColliderMarker>)>,
5556
mut commands: Commands,
5657
query: Query<
5758
Has<ColliderMarker>,
5859
Or<(With<ColliderMarker>, With<AncestorMarker<ColliderMarker>>)>,
5960
>,
6061
child_query: Query<&Children>,
6162
parent_query: Query<&ChildOf>,
62-
rb_query: Query<(), With<RigidBody>>,
63+
rb_query: Query<Entity, With<RigidBody>>,
6364
collider_query: Query<(), With<ColliderMarker>>,
6465
) {
6566
let entity = trigger.target();
@@ -70,10 +71,14 @@ fn on_collider_rigid_body_changed(
7071
};
7172

7273
// Find the closest rigid body ancestor.
73-
let Some(rb_entity) = parent_query
74-
.iter_ancestors(trigger.target())
75-
.find(|&entity| rb_query.contains(entity))
76-
else {
74+
let Some(rb_entity) = rb_query.get(entity).ok().map_or_else(
75+
|| {
76+
parent_query
77+
.iter_ancestors(trigger.target())
78+
.find(|&entity| rb_query.contains(entity))
79+
},
80+
Some,
81+
) else {
7782
return;
7883
};
7984

0 commit comments

Comments
 (0)