@@ -132,6 +132,35 @@ pub fn apply_scale(
132
132
}
133
133
}
134
134
135
+ fn find_child_colliders (
136
+ base_entity : Entity ,
137
+ colliders : & Query < & Collider > ,
138
+ rigid_bodies : & Query < & RigidBody > ,
139
+ childrens : & Query < & Children > ,
140
+
141
+ found : & mut Vec < Entity > ,
142
+ possibilities : & mut Vec < Entity > ,
143
+ ) {
144
+ found. clear ( ) ;
145
+ possibilities. clear ( ) ;
146
+ possibilities. push ( base_entity) ;
147
+ while let Some ( entity) = possibilities. pop ( ) {
148
+ if rigid_bodies. contains ( entity) {
149
+ continue ;
150
+ }
151
+
152
+ if colliders. contains ( entity) {
153
+ found. push ( entity) ;
154
+ }
155
+
156
+ if let Ok ( children) = childrens. get ( entity) {
157
+ possibilities. extend ( children. iter ( ) ) ;
158
+ } else {
159
+ continue ;
160
+ } ;
161
+ }
162
+ }
163
+
135
164
/// System responsible for detecting changes in the hierarchy that would
136
165
/// affect the collider's parent rigid body.
137
166
pub fn collect_collider_hierarchy_changes (
@@ -143,29 +172,10 @@ pub fn collect_collider_hierarchy_changes(
143
172
rigid_bodies : Query < & RigidBody > ,
144
173
colliders : Query < & Collider > ,
145
174
mut collider_parents : Query < & mut ColliderParent > ,
146
- ) {
147
- let child_colliders = |entity : Entity | -> Vec < Entity > {
148
- let mut found = Vec :: new ( ) ;
149
- let mut possibilities = vec ! [ entity] ;
150
- while let Some ( entity) = possibilities. pop ( ) {
151
- if rigid_bodies. contains ( entity) {
152
- continue ;
153
- }
154
-
155
- if colliders. contains ( entity) {
156
- found. push ( entity) ;
157
- }
158
-
159
- if let Ok ( children) = childrens. get ( entity) {
160
- possibilities. extend ( children. iter ( ) ) ;
161
- } else {
162
- continue ;
163
- } ;
164
- }
165
-
166
- found
167
- } ;
168
175
176
+ mut found : Local < Vec < Entity > > ,
177
+ mut possibilities : Local < Vec < Entity > > ,
178
+ ) {
169
179
let parent_rigid_body = |mut entity : Entity | -> Option < Entity > {
170
180
loop {
171
181
if rigid_bodies. contains ( entity) {
@@ -183,25 +193,39 @@ pub fn collect_collider_hierarchy_changes(
183
193
for event in hierarchy_events. iter ( ) {
184
194
match event {
185
195
HierarchyEvent :: ChildAdded { child, .. } | HierarchyEvent :: ChildMoved { child, .. } => {
186
- let colliders = child_colliders ( * child) ;
187
196
let Some ( rigid_body) = parent_rigid_body ( * child) else {
188
197
continue ;
189
198
} ;
199
+ find_child_colliders (
200
+ * child,
201
+ & colliders,
202
+ & rigid_bodies,
203
+ & childrens,
204
+ & mut found,
205
+ & mut possibilities,
206
+ ) ;
190
207
191
- for collider in colliders {
208
+ for collider in & found {
192
209
let new_collider_parent = ColliderParent ( rigid_body) ;
193
- if let Ok ( mut collider_parent) = collider_parents. get_mut ( collider) {
210
+ if let Ok ( mut collider_parent) = collider_parents. get_mut ( * collider) {
194
211
* collider_parent = new_collider_parent;
195
212
} else {
196
- commands. entity ( collider) . insert ( new_collider_parent) ;
213
+ commands. entity ( * collider) . insert ( new_collider_parent) ;
197
214
}
198
215
}
199
216
}
200
217
HierarchyEvent :: ChildRemoved { child, .. } => {
201
- let colliders = child_colliders ( * child) ;
202
- for collider in colliders {
203
- if collider_parents. contains ( collider) {
204
- commands. entity ( collider) . remove :: < ColliderParent > ( ) ;
218
+ find_child_colliders (
219
+ * child,
220
+ & colliders,
221
+ & rigid_bodies,
222
+ & childrens,
223
+ & mut found,
224
+ & mut possibilities,
225
+ ) ;
226
+ for collider in & found {
227
+ if collider_parents. contains ( * collider) {
228
+ commands. entity ( * collider) . remove :: < ColliderParent > ( ) ;
205
229
}
206
230
}
207
231
}
0 commit comments