@@ -188,7 +188,7 @@ impl<C: ScalableCollider> Plugin for ColliderBackendPlugin<C> {
188
188
let entity_ref = world. entity_mut ( ctx. entity ) ;
189
189
190
190
// Get the rigid body entity that the collider is attached to.
191
- let Some ( parent ) = entity_ref. get :: < ColliderParent > ( ) . copied ( ) else {
191
+ let Some ( collider_of ) = entity_ref. get :: < ColliderOf > ( ) . copied ( ) else {
192
192
return ;
193
193
} ;
194
194
@@ -197,22 +197,24 @@ impl<C: ScalableCollider> Plugin for ColliderBackendPlugin<C> {
197
197
* world. resource :: < ColliderRemovalSystem > ( ) . to_owned ( ) ;
198
198
199
199
// Handle collider removal.
200
- world. commands ( ) . run_system_with ( system_id, parent ) ;
200
+ world. commands ( ) . run_system_with ( system_id, collider_of ) ;
201
201
} ) ;
202
202
203
203
// When the `Sensor` component is added to a collider, queue its rigid body for a mass property update.
204
204
app. add_observer (
205
205
|trigger : Trigger < OnAdd , Sensor > ,
206
206
mut commands : Commands ,
207
- query : Query < ( & ColliderMassProperties , & ColliderParent ) > | {
208
- if let Ok ( ( collider_mass_properties, parent) ) = query. get ( trigger. target ( ) ) {
207
+ query : Query < ( & ColliderMassProperties , & ColliderOf ) > | {
208
+ if let Ok ( ( collider_mass_properties, & ColliderOf { rigid_body } ) ) =
209
+ query. get ( trigger. target ( ) )
210
+ {
209
211
// If the collider mass properties are zero, there is nothing to subtract.
210
212
if * collider_mass_properties == ColliderMassProperties :: ZERO {
211
213
return ;
212
214
}
213
215
214
- // Queue the parent rigid body for a mass property update.
215
- if let Ok ( mut entity_commands) = commands. get_entity ( parent . get ( ) ) {
216
+ // Queue the rigid body for a mass property update.
217
+ if let Ok ( mut entity_commands) = commands. get_entity ( rigid_body ) {
216
218
entity_commands. insert ( RecomputeMassProperties ) ;
217
219
}
218
220
}
@@ -252,12 +254,6 @@ impl<C: ScalableCollider> Plugin for ColliderBackendPlugin<C> {
252
254
) ,
253
255
) ;
254
256
255
- // Update collider parents for colliders that are on the same entity as the rigid body.
256
- app. add_systems (
257
- self . schedule ,
258
- update_root_collider_parents :: < C > . before ( PrepareSet :: Finalize ) ,
259
- ) ;
260
-
261
257
let physics_schedule = app
262
258
. get_schedule_mut ( PhysicsSchedule )
263
259
. expect ( "add PhysicsSchedule first" ) ;
@@ -290,29 +286,6 @@ impl<C: ScalableCollider> Plugin for ColliderBackendPlugin<C> {
290
286
#[ reflect( Component , Debug , Default ) ]
291
287
pub struct ColliderMarker ;
292
288
293
- /// Updates [`ColliderParent`] for colliders that are on the same entity as the [`RigidBody`].
294
- ///
295
- /// The [`ColliderHierarchyPlugin`] should be used to handle hierarchies.
296
- fn update_root_collider_parents < C : AnyCollider > (
297
- mut commands : Commands ,
298
- mut bodies : Query <
299
- ( Entity , Option < & mut ColliderParent > ) ,
300
- ( With < RigidBody > , With < C > , Or < ( Added < RigidBody > , Added < C > ) > ) ,
301
- > ,
302
- ) {
303
- for ( entity, collider_parent) in & mut bodies {
304
- if let Some ( mut collider_parent) = collider_parent {
305
- collider_parent. 0 = entity;
306
- } else {
307
- commands. entity ( entity) . try_insert ( (
308
- ColliderParent ( entity) ,
309
- // TODO: This probably causes a one frame delay. Compute real value?
310
- ColliderTransform :: default ( ) ,
311
- ) ) ;
312
- }
313
- }
314
- }
315
-
316
289
/// Generates [`Collider`]s based on [`ColliderConstructor`]s.
317
290
///
318
291
/// If a [`ColliderConstructor`] requires a mesh, the system keeps running
@@ -505,7 +478,7 @@ fn update_aabb<C: AnyCollider>(
505
478
& mut ColliderAabb ,
506
479
& Position ,
507
480
& Rotation ,
508
- Option < & ColliderParent > ,
481
+ Option < & ColliderOf > ,
509
482
Option < & CollisionMargin > ,
510
483
Option < & SpeculativeMargin > ,
511
484
Has < SweptCcd > ,
@@ -520,7 +493,7 @@ fn update_aabb<C: AnyCollider>(
520
493
Changed < C > ,
521
494
) > ,
522
495
> ,
523
- parent_velocity : Query <
496
+ rb_velocities : Query <
524
497
(
525
498
& Position ,
526
499
& ComputedCenterOfMass ,
@@ -544,7 +517,7 @@ fn update_aabb<C: AnyCollider>(
544
517
mut aabb,
545
518
pos,
546
519
rot,
547
- collider_parent ,
520
+ collider_of ,
548
521
collision_margin,
549
522
speculative_margin,
550
523
has_swept_ccd,
@@ -571,16 +544,16 @@ fn update_aabb<C: AnyCollider>(
571
544
// Expand the AABB based on the body's velocity and CCD speculative margin.
572
545
let ( lin_vel, ang_vel) = if let ( Some ( lin_vel) , Some ( ang_vel) ) = ( lin_vel, ang_vel) {
573
546
( * lin_vel, * ang_vel)
574
- } else if let Some ( Ok ( ( parent_pos , center_of_mass, Some ( lin_vel) , Some ( ang_vel) ) ) ) =
575
- collider_parent . map ( |p| parent_velocity . get ( p . get ( ) ) )
547
+ } else if let Some ( Ok ( ( rb_pos , center_of_mass, Some ( lin_vel) , Some ( ang_vel) ) ) ) =
548
+ collider_of . map ( |& ColliderOf { rigid_body } | rb_velocities . get ( rigid_body ) )
576
549
{
577
550
// If the rigid body is rotating, off-center colliders will orbit around it,
578
551
// which affects their linear velocities. We need to compute the linear velocity
579
552
// at the offset position.
580
553
// TODO: This assumes that the colliders would continue moving in the same direction,
581
554
// but because they are orbiting, the direction will change. We should take
582
555
// into account the uniform circular motion.
583
- let offset = pos. 0 - parent_pos . 0 - center_of_mass. 0 ;
556
+ let offset = pos. 0 - rb_pos . 0 - center_of_mass. 0 ;
584
557
#[ cfg( feature = "2d" ) ]
585
558
let vel_at_offset =
586
559
lin_vel. 0 + Vector :: new ( -ang_vel. 0 * offset. y , ang_vel. 0 * offset. x ) * 1.0 ;
@@ -661,26 +634,24 @@ pub fn update_collider_scale<C: ScalableCollider>(
661
634
662
635
/// A resource that stores the system ID for the system that reacts to collider removals.
663
636
#[ derive( Resource ) ]
664
- struct ColliderRemovalSystem ( SystemId < In < ColliderParent > > ) ;
637
+ struct ColliderRemovalSystem ( SystemId < In < ColliderOf > > ) ;
665
638
666
639
/// Updates the mass properties of bodies and wakes bodies up when an attached collider is removed.
667
640
///
668
- /// Takes the removed collider's entity, parent , mass properties, and transform as input.
641
+ /// Takes the removed collider's entity, rigid body entity , mass properties, and transform as input.
669
642
fn collider_removed (
670
- In ( parent ) : In < ColliderParent > ,
643
+ In ( ColliderOf { rigid_body } ) : In < ColliderOf > ,
671
644
mut commands : Commands ,
672
645
mut sleep_query : Query < & mut TimeSleeping > ,
673
646
) {
674
- let parent = parent. get ( ) ;
675
-
676
- let Ok ( mut entity_commands) = commands. get_entity ( parent) else {
647
+ let Ok ( mut entity_commands) = commands. get_entity ( rigid_body) else {
677
648
return ;
678
649
} ;
679
650
680
- // Queue the parent entity for mass property recomputation.
651
+ // Queue the rigid body for mass property recomputation.
681
652
entity_commands. insert ( RecomputeMassProperties ) ;
682
653
683
- if let Ok ( mut time_sleeping) = sleep_query. get_mut ( parent ) {
654
+ if let Ok ( mut time_sleeping) = sleep_query. get_mut ( rigid_body ) {
684
655
// Wake up the rigid body since removing the collider could also remove active contacts.
685
656
entity_commands. remove :: < Sleeping > ( ) ;
686
657
time_sleeping. 0 = 0.0 ;
@@ -720,8 +691,9 @@ mod tests {
720
691
app. add_plugins ( (
721
692
PreparePlugin :: new ( FixedPostUpdate ) ,
722
693
MassPropertyPlugin :: new ( FixedPostUpdate ) ,
694
+ ColliderHierarchyPlugin ,
695
+ ColliderTransformPlugin :: default ( ) ,
723
696
ColliderBackendPlugin :: < Collider > :: new ( FixedPostUpdate ) ,
724
- ColliderHierarchyPlugin :: new ( FixedPostUpdate ) ,
725
697
) ) ;
726
698
727
699
let collider = Collider :: capsule ( 0.5 , 2.0 ) ;
0 commit comments