avian3d/dynamics/rigid_body/mass_properties/components/
collider.rs

1use super::super::MassProperties;
2use crate::prelude::*;
3use bevy::prelude::*;
4use derive_more::derive::From;
5
6/// The density of a [`Collider`], used for computing [`ColliderMassProperties`].
7/// Defaults to `1.0`.
8///
9/// If the entity has the [`Mass`] component, it will be used instead of the collider's mass.
10///
11/// # Example
12///
13/// ```
14#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
15#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
16/// use bevy::prelude::*;
17///
18/// // Spawn a body with a collider that has a density of `2.5`.
19/// fn setup(mut commands: Commands) {
20///     commands.spawn((
21///         RigidBody::Dynamic,
22#[cfg_attr(feature = "2d", doc = "        Collider::circle(0.5),")]
23#[cfg_attr(feature = "3d", doc = "        Collider::sphere(0.5),")]
24///         ColliderDensity(2.5),
25///     ));
26/// }
27/// ```
28#[derive(Reflect, Clone, Copy, Component, Debug, Deref, DerefMut, PartialEq, PartialOrd, From)]
29#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
30#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
31#[reflect(Debug, Component, PartialEq)]
32pub struct ColliderDensity(pub f32);
33
34impl Default for ColliderDensity {
35    fn default() -> Self {
36        Self(1.0)
37    }
38}
39
40impl ColliderDensity {
41    /// A density of `0.0`, resulting in a collider with no mass.
42    pub const ZERO: Self = Self(0.0);
43}
44
45/// A read-only component for the mass properties of a [`Collider`].
46/// Computed automatically from the collider's shape and [`ColliderDensity`].
47///
48/// If the entity has the [`Mass`], [`AngularInertia`], or [`CenterOfMass`] components,
49/// they will be used instead when updating the associated rigid body's [`ComputedMass`],
50/// [`ComputedAngularInertia`], and [`ComputedCenterOfMass`] components respectively.
51///
52/// # Example
53///
54/// ```no_run
55#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
56#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
57/// use bevy::prelude::*;
58///
59/// fn setup(mut commands: Commands) {
60#[cfg_attr(
61    feature = "2d",
62    doc = "    commands.spawn((RigidBody::Dynamic, Collider::circle(0.5)));"
63)]
64#[cfg_attr(
65    feature = "3d",
66    doc = "    commands.spawn((RigidBody::Dynamic, Collider::sphere(0.5)));"
67)]
68/// }
69///
70/// fn print_collider_masses(query: Query<&ColliderMassProperties>) {
71///     for mass_properties in &query {
72///         println!("{}", mass_properties.mass);
73///     }
74/// }
75/// ```
76#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, PartialEq, From)]
77#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
78#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
79#[reflect(Debug, Component, PartialEq)]
80pub struct ColliderMassProperties(MassProperties);
81
82impl ColliderMassProperties {
83    /// The collider has no mass.
84    pub const ZERO: Self = Self(MassProperties::ZERO);
85
86    /// Computes mass properties from a given shape and density.
87    ///
88    /// Because [`ColliderMassProperties`] is intended to be read-only, adding this as a component manually
89    /// has no effect. The mass properties will be recomputed using the [`ColliderDensity`].
90    #[inline]
91    pub fn from_shape<T: ComputeMassProperties>(shape: &T, density: f32) -> Self {
92        Self(shape.mass_properties(density))
93    }
94}