1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use crate::prelude::*;
use bevy::prelude::*;
use bevy::utils::HashMap;

/// A component that will automatically generate [`Collider`]s on its descendants at runtime.
/// The type of the generated collider can be specified using [`ColliderConstructor`].
/// This supports computing the shape dynamically from the mesh, in which case only the descendants
/// with a [`Mesh`] will have colliders generated.
///
/// In contrast to [`ColliderConstructor`], this component will *not* generate a collider on its own entity.
///
/// If this component is used on a scene, such as one spawned by a [`SceneBundle`], it will
/// wait until the scene is loaded before generating colliders.
///
/// The exact configuration for each descendant can be specified using the helper methods
/// such as [`with_constructor_for_name`](Self::with_constructor_for_name).
///
/// This component will only override a pre-existing [`Collider`] component on a descendant entity
/// when it has been explicitly mentioned in the `config`.
///
/// ## See also
///
/// For inserting colliders on the same entity, use [`ColliderConstructor`].
///
/// ## Caveats
///
/// When a component has multiple ancestors with [`ColliderConstructorHierarchy`], the insertion order is undefined.
///
/// ## Example
///
/// ```
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>) {
///     let scene = assets.load("my_model.gltf#Scene0");
///
#[cfg_attr(
    feature = "2d",
    doc = "    // Spawn the scene and automatically generate circle colliders"
)]
#[cfg_attr(
    feature = "3d",
    doc = "    // Spawn the scene and automatically generate triangle mesh colliders"
)]
///     commands.spawn((
///         SceneBundle { scene: scene.clone(), ..default() },
#[cfg_attr(
    feature = "2d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::Circle { radius: 2.0 }),"
)]
#[cfg_attr(
    feature = "3d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::TrimeshFromMesh),"
)]
///     ));
///
///     // Specify configuration for specific meshes by name
///     commands.spawn((
///         SceneBundle { scene: scene.clone(), ..default() },
#[cfg_attr(
    feature = "2d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::Circle { radius: 2.0 })
            .with_constructor_for_name(\"Tree\", ColliderConstructor::Rectangle { x_length: 1.0, y_length: 2.0 })"
)]
#[cfg_attr(
    feature = "3d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::TrimeshFromMesh)
            .with_constructor_for_name(\"Tree\", ColliderConstructor::ConvexHullFromMesh)"
)]
///             .with_layers_for_name("Tree", CollisionLayers::from_bits(0b0010, 0b1111))
///             .with_density_for_name("Tree", 2.5),
///     ));
///
///     // Only generate colliders for specific meshes by name
///     commands.spawn((
///         SceneBundle { scene: scene.clone(), ..default() },
///         ColliderConstructorHierarchy::new(None)
#[cfg_attr(
    feature = "2d",
    doc = "            .with_constructor_for_name(\"Tree\", ColliderConstructor::Circle { radius: 2.0 }),"
)]
#[cfg_attr(
    feature = "3d",
    doc = "            .with_constructor_for_name(\"Tree\", ColliderConstructor::ConvexHullFromMesh),"
)]
///     ));
///
///     // Generate colliders for everything except specific meshes by name
///     commands.spawn((
///         SceneBundle { scene, ..default() },
#[cfg_attr(
    feature = "2d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::Circle { radius: 2.0 })
            .without_constructor_for_name(\"Tree\"),"
)]
#[cfg_attr(
    feature = "3d",
    doc = "        ColliderConstructorHierarchy::new(ColliderConstructor::TrimeshFromMeshWithConfig(
             TrimeshFlags::MERGE_DUPLICATE_VERTICES
        ))
        .without_constructor_for_name(\"Tree\"),"
)]
///     ));
/// }
/// ```
#[derive(Component, Clone, Debug, Default, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Component, Debug, PartialEq, Default)]
pub struct ColliderConstructorHierarchy {
    /// The default collider type used for each entity that isn't included in [`config`](Self::config).
    /// If `None`, all entities except the ones in [`config`](Self::config) will be skipped.
    pub default_constructor: Option<ColliderConstructor>,
    /// The default [`CollisionLayers`] used for colliders in the hierarchy.
    ///
    /// [`CollisionLayers::ALL`] by default.
    pub default_layers: CollisionLayers,
    /// The default [`ColliderDensity`] used for colliders in the hierarchy.
    ///
    /// `1.0` by default.
    pub default_density: ColliderDensity,
    /// Specifies data like the [`ColliderConstructor`] and [`CollisionLayers`] for entities
    /// in the hierarchy by `Name`. Entries with a `None` value will be skipped.
    ///
    /// For the entities not found in this `HashMap`, [`default_constructor`](Self::default_constructor),
    /// [`default_layers`](Self::default_layers), and [`default_density`](Self::default_density) will be used instead.
    pub config: HashMap<String, Option<ColliderConstructorHierarchyConfig>>,
}

impl ColliderConstructorHierarchy {
    /// Creates a new [`ColliderConstructorHierarchy`] with the default [`ColliderConstructor`] used for
    /// generating colliders set to the given `default_constructor`.
    ///
    /// If the given constructor type is `None`, collider generation is skipped
    /// for all entities in the hierarchy except the ones in [`config`](Self::config).
    ///
    /// Collider constructors can be specified for individual entities using
    /// [`with_constructor_for_name`](Self::with_constructor_for_name).
    pub fn new(default_constructor: impl Into<Option<ColliderConstructor>>) -> Self {
        Self {
            default_constructor: default_constructor.into(),
            default_layers: CollisionLayers::ALL,
            default_density: ColliderDensity(1.0),
            config: default(),
        }
    }

    /// Specifies the default [`CollisionLayers`] used for colliders not included in [`ColliderConstructorHierarchy::config`].
    pub fn with_default_layers(mut self, layers: CollisionLayers) -> Self {
        self.default_layers = layers;
        self
    }

    /// Specifies the default [`ColliderDensity`] used for colliders not included in [`ColliderConstructorHierarchy::config`].
    pub fn with_default_density(mut self, density: impl Into<ColliderDensity>) -> Self {
        self.default_density = density.into();
        self
    }

    /// Specifies the [`ColliderConstructor`] used for an entity with the given `name`.
    pub fn with_constructor_for_name(
        mut self,
        name: &str,
        constructor: ColliderConstructor,
    ) -> Self {
        if let Some(Some(data)) = self.config.get_mut(name) {
            data.constructor = Some(constructor);
        } else {
            self.config.insert(
                name.to_string(),
                Some(ColliderConstructorHierarchyConfig {
                    constructor: Some(constructor),
                    ..default()
                }),
            );
        }
        self
    }

    /// Specifies the [`CollisionLayers`] used for an entity with the given `name`.
    pub fn with_layers_for_name(self, name: &str, layers: CollisionLayers) -> Self {
        self.with_config_for_name(name, |config| config.layers = Some(layers))
    }

    /// Specifies the [`ColliderDensity`] used for an entity with the given `name`.
    pub fn with_density_for_name(self, name: &str, density: impl Into<ColliderDensity>) -> Self {
        let density = density.into();
        self.with_config_for_name(name, |config| config.density = Some(density))
    }

    /// Sets the [`ColliderConstructor`] for the entity associated with the given `name` to `None`,
    /// skipping collider generation for it.
    pub fn without_constructor_for_name(mut self, name: &str) -> Self {
        self.config.insert(name.to_string(), None);
        self
    }

    fn with_config_for_name(
        mut self,
        name: &str,
        mut mutate_config: impl FnMut(&mut ColliderConstructorHierarchyConfig),
    ) -> Self {
        if let Some(Some(config)) = self.config.get_mut(name) {
            mutate_config(config);
        } else {
            let mut config = ColliderConstructorHierarchyConfig::default();
            mutate_config(&mut config);
            self.config.insert(name.to_string(), Some(config));
        }
        self
    }
}

/// Configuration for a specific collider generated from a scene using [`ColliderConstructorHierarchy`].
#[derive(Clone, Debug, Default, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Default, PartialEq)]
pub struct ColliderConstructorHierarchyConfig {
    /// The type of collider generated for the mesh.
    ///
    /// If `None`, [`ColliderConstructorHierarchy::default_constructor`] is used instead.
    pub constructor: Option<ColliderConstructor>,
    /// The [`CollisionLayers`] used for this collider.
    ///
    /// If `None`, [`ColliderConstructorHierarchy::default_layers`] is used instead.
    pub layers: Option<CollisionLayers>,
    /// The [`ColliderDensity`] used for this collider.
    ///
    /// If `None`, [`ColliderConstructorHierarchy::default_density`] is used instead.
    pub density: Option<ColliderDensity>,
}

/// A component that will automatically generate a [`Collider`] at runtime using [`Collider::try_from_constructor`].
/// Enabling the `collider-from-mesh` feature activates support for computing the shape dynamically from the mesh attached to the same entity.
///
/// Since [`Collider`] is not [`Reflect`], you can use this type to statically specify a collider's shape instead.
///
/// This component will never override a pre-existing [`Collider`] component on the same entity.
///
/// ## See also
///
/// For inserting colliders on an entity's descendants, use [`ColliderConstructorHierarchy`].
///
/// ## Panics
///
/// The system handling the generation of colliders will panic if the specified [`ColliderConstructor`]
/// requires a mesh, but the entity does not have a `Handle<Mesh>` component.
///
/// ## Example
///
/// ```
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>, mut meshes: Assets<Mesh>) {
#[cfg_attr(feature = "2d", doc = "     // Spawn a circle with radius 2")]
#[cfg_attr(
    feature = "3d",
    doc = "    // Spawn a cube with a convex hull collider generated from the mesh"
)]
///     commands.spawn((
#[cfg_attr(
    feature = "2d",
    doc = "        ColliderConstructor::Circle { radius: 2.0 },"
)]
#[cfg_attr(
    feature = "3d",
    doc = "        ColliderConstructor::ConvexHullFromMesh,"
)]
///         PbrBundle {
///             mesh: meshes.add(Mesh::from(Cuboid::default())),
///             ..default()
///         },
///     ));
/// }
/// ```
#[derive(Clone, Debug, PartialEq, Reflect, Component)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[cfg_attr(feature = "collider-from-mesh", derive(Default))]
#[cfg_attr(feature = "collider-from-mesh", reflect(Default))]
#[reflect(Debug, Component, PartialEq)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum ColliderConstructor {
    /// Constructs a collider with [`Collider::circle`].
    #[cfg(feature = "2d")]
    Circle { radius: Scalar },
    /// Constructs a collider with [`Collider::sphere`].
    #[cfg(feature = "3d")]
    Sphere { radius: Scalar },
    /// Constructs a collider with [`Collider::ellipse`].
    #[cfg(feature = "2d")]
    Ellipse {
        half_width: Scalar,
        half_height: Scalar,
    },
    /// Constructs a collider with [`Collider::rectangle`].
    #[cfg(feature = "2d")]
    Rectangle { x_length: Scalar, y_length: Scalar },
    /// Constructs a collider with [`Collider::cuboid`].
    #[cfg(feature = "3d")]
    Cuboid {
        x_length: Scalar,
        y_length: Scalar,
        z_length: Scalar,
    },
    /// Constructs a collider with [`Collider::round_rectangle`].
    #[cfg(feature = "2d")]
    RoundRectangle {
        x_length: Scalar,
        y_length: Scalar,
        border_radius: Scalar,
    },
    /// Constructs a collider with [`Collider::round_cuboid`].
    #[cfg(feature = "3d")]
    RoundCuboid {
        x_length: Scalar,
        y_length: Scalar,
        z_length: Scalar,
        border_radius: Scalar,
    },
    /// Constructs a collider with [`Collider::cylinder`].
    #[cfg(feature = "3d")]
    Cylinder { radius: Scalar, height: Scalar },
    /// Constructs a collider with [`Collider::cone`].
    #[cfg(feature = "3d")]
    Cone { radius: Scalar, height: Scalar },
    /// Constructs a collider with [`Collider::capsule`].
    Capsule { radius: Scalar, height: Scalar },
    /// Constructs a collider with [`Collider::capsule_endpoints`].
    CapsuleEndpoints {
        radius: Scalar,
        a: Vector,
        b: Vector,
    },
    /// Constructs a collider with [`Collider::half_space`].
    HalfSpace { outward_normal: Vector },
    /// Constructs a collider with [`Collider::segment`].
    Segment { a: Vector, b: Vector },
    /// Constructs a collider with [`Collider::triangle`].
    Triangle { a: Vector, b: Vector, c: Vector },
    /// Constructs a collider with [`Collider::regular_polygon`].
    #[cfg(feature = "2d")]
    RegularPolygon { circumradius: f32, sides: usize },
    /// Constructs a collider with [`Collider::polyline`].
    Polyline {
        vertices: Vec<Vector>,
        indices: Option<Vec<[u32; 2]>>,
    },
    /// Constructs a collider with [`Collider::trimesh`].
    Trimesh {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 3]>,
    },
    /// Constructs a collider with [`Collider::trimesh_with_config`].
    TrimeshWithConfig {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 3]>,
        flags: TrimeshFlags,
    },
    /// Constructs a collider with [`Collider::convex_decomposition`].
    #[cfg(feature = "2d")]
    ConvexDecomposition {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 2]>,
    },
    /// Constructs a collider with [`Collider::convex_decomposition`].
    #[cfg(feature = "3d")]
    ConvexDecomposition {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 3]>,
    },
    /// Constructs a collider with [`Collider::convex_decomposition_with_config`].
    #[cfg(feature = "2d")]
    ConvexDecompositionWithConfig {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 2]>,
        params: VhacdParameters,
    },
    /// Constructs a collider with [`Collider::convex_decomposition_with_config`].
    #[cfg(feature = "3d")]
    ConvexDecompositionWithConfig {
        vertices: Vec<Vector>,
        indices: Vec<[u32; 3]>,
        params: VhacdParameters,
    },
    /// Constructs a collider with [`Collider::convex_hull`].
    #[cfg(feature = "2d")]
    ConvexHull { points: Vec<Vector> },
    /// Constructs a collider with [`Collider::convex_hull`].
    #[cfg(feature = "3d")]
    ConvexHull { points: Vec<Vector> },
    /// Constructs a collider with [`Collider::heightfield`].
    #[cfg(feature = "2d")]
    Heightfield { heights: Vec<Scalar>, scale: Vector },
    /// Constructs a collider with [`Collider::heightfield`].
    #[cfg(feature = "3d")]
    Heightfield {
        heights: Vec<Vec<Scalar>>,
        scale: Vector,
    },
    /// Constructs a collider with [`Collider::trimesh_from_mesh`].
    #[cfg(feature = "collider-from-mesh")]
    #[default]
    TrimeshFromMesh,
    /// Constructs a collider with [`Collider::trimesh_from_mesh_with_config`].
    #[cfg(all(
        feature = "3d",
        feature = "collider-from-mesh",
        feature = "default-collider"
    ))]
    TrimeshFromMeshWithConfig(TrimeshFlags),
    /// Constructs a collider with [`Collider::convex_decomposition_from_mesh`].
    #[cfg(feature = "collider-from-mesh")]
    ConvexDecompositionFromMesh,
    /// Constructs a collider with [`Collider::convex_decomposition_from_mesh_with_config`].
    #[cfg(all(
        feature = "3d",
        feature = "collider-from-mesh",
        feature = "default-collider"
    ))]
    ConvexDecompositionFromMeshWithConfig(VhacdParameters),
    /// Constructs a collider with [`Collider::convex_hull_from_mesh`].
    #[cfg(feature = "collider-from-mesh")]
    ConvexHullFromMesh,
}

impl ColliderConstructor {
    /// Returns `true` if the collider type requires a mesh to be generated.
    #[cfg(feature = "collider-from-mesh")]
    pub fn requires_mesh(&self) -> bool {
        matches!(
            self,
            Self::TrimeshFromMesh
                | Self::TrimeshFromMeshWithConfig(_)
                | Self::ConvexDecompositionFromMesh
                | Self::ConvexDecompositionFromMeshWithConfig(_)
                | Self::ConvexHullFromMesh
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bevy::ecs::query::QueryData;
    #[cfg(feature = "bevy_scene")]
    use bevy::scene::ScenePlugin;

    #[test]
    fn collider_constructor_requires_no_mesh_on_primitive() {
        let mut app = create_test_app();

        let entity = app.world_mut().spawn(PRIMITIVE_COLLIDER.clone()).id();

        app.update();

        assert!(app.query_ok::<&Collider>(entity));
        assert!(app.query_err::<&ColliderConstructor>(entity));
    }

    #[cfg(feature = "collider-from-mesh")]
    #[test]
    #[should_panic]
    fn collider_constructor_requires_mesh_on_computed() {
        let mut app = create_test_app();

        app.world_mut().spawn(COMPUTED_COLLIDER.clone());

        app.update();
    }

    #[cfg(feature = "collider-from-mesh")]
    #[test]
    fn collider_constructor_converts_mesh_on_computed() {
        let mut app = create_test_app();

        let mesh_handle = app.add_mesh();
        let entity = app
            .world_mut()
            .spawn((COMPUTED_COLLIDER.clone(), mesh_handle))
            .id();

        app.update();

        assert!(app.query_ok::<&Collider>(entity));
        assert!(app.query_ok::<&Handle<Mesh>>(entity));
        assert!(app.query_err::<&ColliderConstructor>(entity));
    }

    #[test]
    fn collider_constructor_hierarchy_does_nothing_on_self_with_primitive() {
        let mut app = create_test_app();

        let entity = app
            .world_mut()
            .spawn(ColliderConstructorHierarchy::new(
                PRIMITIVE_COLLIDER.clone(),
            ))
            .id();

        app.update();

        assert!(app.query_err::<&ColliderConstructorHierarchy>(entity));
        assert!(app.query_err::<&Collider>(entity));
    }

    #[cfg(feature = "collider-from-mesh")]
    #[test]
    fn collider_constructor_hierarchy_does_nothing_on_self_with_computed() {
        let mut app = create_test_app();

        let mesh_handle = app.add_mesh();
        let entity = app
            .world_mut()
            .spawn((
                ColliderConstructorHierarchy::new(COMPUTED_COLLIDER.clone()),
                mesh_handle,
            ))
            .id();

        app.update();

        assert!(app.query_ok::<&Handle<Mesh>>(entity));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(entity));
        assert!(app.query_err::<&Collider>(entity));
    }

    #[cfg(feature = "collider-from-mesh")]
    #[test]
    fn collider_constructor_hierarchy_does_not_require_mesh_on_self_with_computed() {
        let mut app = create_test_app();

        let entity = app
            .world_mut()
            .spawn(ColliderConstructorHierarchy::new(COMPUTED_COLLIDER.clone()))
            .id();

        app.update();

        assert!(app.query_err::<&Collider>(entity));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(entity));
    }

    #[test]
    fn collider_constructor_hierarchy_inserts_primitive_colliders_on_all_descendants() {
        let mut app = create_test_app();

        // Hierarchy:
        // - parent
        //   - child1
        //   - child2
        //     - child3

        let parent = app
            .world_mut()
            .spawn(ColliderConstructorHierarchy::new(
                PRIMITIVE_COLLIDER.clone(),
            ))
            .id();
        let child1 = app.world_mut().spawn(()).id();
        let child2 = app.world_mut().spawn(()).id();
        let child3 = app.world_mut().spawn(()).id();

        app.world_mut()
            .entity_mut(parent)
            .push_children(&[child1, child2]);
        app.world_mut().entity_mut(child2).push_children(&[child3]);

        app.update();

        // No entities should have ColliderConstructorHierarchy
        assert!(app.query_err::<&ColliderConstructorHierarchy>(parent));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child1));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child2));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child3));

        assert!(app.query_err::<&Collider>(parent));
        assert!(app.query_ok::<&Collider>(child1));
        assert!(app.query_ok::<&Collider>(child2));
        assert!(app.query_ok::<&Collider>(child3));
    }

    #[cfg(feature = "collider-from-mesh")]
    #[test]
    fn collider_constructor_hierarchy_inserts_computed_colliders_only_on_descendants_with_mesh() {
        let mut app = create_test_app();
        let mesh_handle = app.add_mesh();

        // Hierarchy:
        // - parent
        //   - child1 (no mesh)
        //   - child2 (no mesh)
        //     - child3 (mesh)
        //   - child4 (mesh)
        //     - child5 (no mesh)
        //   - child6 (mesh)
        //   - child7 (mesh)
        //     - child8 (mesh)

        let parent = app
            .world_mut()
            .spawn(ColliderConstructorHierarchy::new(COMPUTED_COLLIDER.clone()))
            .id();
        let child1 = app.world_mut().spawn(()).id();
        let child2 = app.world_mut().spawn(()).id();
        let child3 = app.world_mut().spawn(mesh_handle.clone()).id();
        let child4 = app.world_mut().spawn(mesh_handle.clone()).id();
        let child5 = app.world_mut().spawn(()).id();
        let child6 = app.world_mut().spawn(mesh_handle.clone()).id();
        let child7 = app.world_mut().spawn(mesh_handle.clone()).id();
        let child8 = app.world_mut().spawn(mesh_handle.clone()).id();

        app.world_mut()
            .entity_mut(parent)
            .push_children(&[child1, child2, child4, child6, child7]);
        app.world_mut().entity_mut(child2).push_children(&[child3]);
        app.world_mut().entity_mut(child4).push_children(&[child5]);
        app.world_mut().entity_mut(child7).push_children(&[child8]);

        app.update();

        // No entities should have ColliderConstructorHierarchy
        assert!(app.query_err::<&ColliderConstructorHierarchy>(parent));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child1));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child2));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child3));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child4));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child5));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child6));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child7));
        assert!(app.query_err::<&ColliderConstructorHierarchy>(child8));

        assert!(app.query_err::<&Collider>(parent));
        assert!(app.query_err::<&Collider>(child1));
        assert!(app.query_err::<&Collider>(child2));
        assert!(app.query_ok::<&Collider>(child3));
        assert!(app.query_ok::<&Collider>(child4));
        assert!(app.query_err::<&Collider>(child5));
        assert!(app.query_ok::<&Collider>(child6));
        assert!(app.query_ok::<&Collider>(child7));
        assert!(app.query_ok::<&Collider>(child8));
    }

    #[cfg(all(feature = "collider-from-mesh", feature = "bevy_scene"))]
    #[test]
    fn collider_constructor_hierarchy_inserts_correct_configs_on_scene() {
        use parry::shape::ShapeType;

        let mut app = create_gltf_test_app();

        let scene_handle = app
            .world_mut()
            .resource_mut::<AssetServer>()
            .load("ferris.glb#Scene0");

        let hierarchy = app
            .world_mut()
            .spawn((
                SceneBundle {
                    scene: scene_handle,
                    ..default()
                },
                ColliderConstructorHierarchy::new(ColliderConstructor::ConvexDecompositionFromMesh)
                    // Use a primitive collider for the left arm.
                    .with_constructor_for_name("armL_mesh", PRIMITIVE_COLLIDER)
                    .with_density_for_name("armL_mesh", 2.0)
                    // Remove the right arm. Don't worry, crabs can regrow lost limbs!
                    .without_constructor_for_name("armR_mesh"),
                RigidBody::Dynamic,
            ))
            .id();

        while app
            .world()
            .resource::<Events<bevy::scene::SceneInstanceReady>>()
            .is_empty()
        {
            app.update();
        }
        app.update();

        assert!(app.query_err::<&ColliderConstructorHierarchy>(hierarchy));
        assert!(app.query_err::<&Collider>(hierarchy));

        // Check densities
        let densities: HashMap<_, _> = app
            .world_mut()
            .query::<(&Name, &ColliderDensity)>()
            .iter(app.world())
            .map(|(name, density)| (name.to_string(), density.0))
            .collect();

        assert_eq!(densities["eyes_mesh"], 1.0);
        assert_eq!(densities["armL_mesh"], 2.0);
        assert!(densities.get("armR_mesh").is_none());

        // Check collider shape types
        let colliders: HashMap<_, _> = app
            .world_mut()
            .query::<(&Name, &Collider)>()
            .iter(app.world())
            .map(|(name, collider)| (name.to_string(), collider))
            .collect();

        assert_eq!(
            colliders["eyes_mesh"].shape().shape_type(),
            ShapeType::Compound
        );
        assert_eq!(
            colliders["armL_mesh"].shape().shape_type(),
            ShapeType::Capsule
        );
        assert!(colliders.get("armR_mesh").is_none());
    }

    const PRIMITIVE_COLLIDER: ColliderConstructor = ColliderConstructor::Capsule {
        height: 1.0,
        radius: 0.5,
    };

    #[cfg(feature = "collider-from-mesh")]
    const COMPUTED_COLLIDER: ColliderConstructor = ColliderConstructor::TrimeshFromMesh;

    fn create_test_app() -> App {
        let mut app = App::new();
        app.add_plugins((
            MinimalPlugins,
            AssetPlugin::default(),
            #[cfg(feature = "bevy_scene")]
            ScenePlugin,
            HierarchyPlugin,
            PhysicsPlugins::default(),
        ))
        .init_resource::<Assets<Mesh>>();

        app
    }

    #[cfg(all(feature = "collider-from-mesh", feature = "bevy_scene"))]
    fn create_gltf_test_app() -> App {
        use bevy::{diagnostic::DiagnosticsPlugin, winit::WinitPlugin};

        let mut app = App::new();
        app.add_plugins((
            DefaultPlugins
                .build()
                .disable::<WinitPlugin>()
                .disable::<DiagnosticsPlugin>(),
            PhysicsPlugins::default(),
        ));
        app.finish();
        app.cleanup();
        app
    }

    trait AppExt {
        fn query_ok<D: QueryData>(&mut self, entity: Entity) -> bool;
        fn query_err<D: QueryData>(&mut self, entity: Entity) -> bool {
            !self.query_ok::<D>(entity)
        }

        #[cfg(feature = "collider-from-mesh")]
        fn add_mesh(&mut self) -> Handle<Mesh>;
    }

    impl AppExt for App {
        fn query_ok<D: QueryData>(&mut self, entity: Entity) -> bool {
            let mut query = self.world_mut().query::<D>();
            let component = query.get(self.world(), entity);
            component.is_ok()
        }

        #[cfg(feature = "collider-from-mesh")]
        fn add_mesh(&mut self) -> Handle<Mesh> {
            self.world_mut()
                .get_resource_mut::<Assets<Mesh>>()
                .unwrap()
                .add(Mesh::from(Cuboid::default()))
        }
    }
}