avian2d/dynamics/rigid_body/
mod.rs

1//! Common components and bundles for rigid bodies.
2
3pub mod forces;
4pub mod mass_properties;
5pub mod sleeping;
6
7// Components
8mod locked_axes;
9mod physics_material;
10mod world_query;
11
12pub use locked_axes::LockedAxes;
13pub use physics_material::{
14    CoefficientCombine, DefaultFriction, DefaultRestitution, Friction, Restitution,
15};
16pub use world_query::*;
17
18#[cfg(feature = "2d")]
19pub(crate) use forces::FloatZero;
20
21use crate::{
22    physics_transform::init_physics_transform,
23    prelude::{forces::AccumulatedLocalAcceleration, *},
24};
25use bevy::{
26    ecs::{lifecycle::HookContext, world::DeferredWorld},
27    prelude::*,
28};
29use derive_more::From;
30
31/// A non-deformable body used for the simulation of most physics objects.
32///
33/// # Rigid Body Types
34///
35/// A rigid body can be either dynamic, kinematic or static.
36///
37/// - **Dynamic bodies** are similar to real life objects and are affected by forces and contacts.
38/// - **Kinematic bodies** can only be moved programmatically, which is useful for things like character controllers and moving platforms.
39/// - **Static bodies** can not move, so they can be good for objects in the environment like the ground and walls.
40///
41/// # Creation
42///
43/// Creating a rigid body is as simple as adding the [`RigidBody`] component,
44/// and an optional [`Collider`]:
45///
46/// ```
47#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
48#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
49/// use bevy::prelude::*;
50///
51/// fn setup(mut commands: Commands) {
52///     // Spawn a dynamic rigid body and specify its position.
53///     commands.spawn((
54///         RigidBody::Dynamic,
55///         Collider::capsule(0.5, 1.5),
56///         Transform::from_xyz(0.0, 3.0, 0.0),
57///     ));
58/// }
59/// ```
60///
61/// By default, dynamic rigid bodies will have mass properties computed based on the attached colliders
62/// and their [`ColliderDensity`]. See the [Mass properties](#mass-properties) section for more information.
63///
64/// # Movement
65///
66/// A rigid body can be moved in three ways: by modifying its position directly,
67/// by changing its velocity, or by applying forces or impulses.
68///
69/// To change the position of a rigid body, you can simply modify its `Transform`:
70///
71/// ```
72#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
73#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
74/// use bevy::prelude::*;
75///
76/// fn move_bodies(mut query: Query<&mut Transform, With<RigidBody>>) {
77///     for mut transform in &mut query {
78///         transform.translation.x += 0.1;
79///     }
80/// }
81/// ```
82///
83/// However, moving a dynamic body by changing its position directly is similar
84/// to teleporting the body, which can result in unexpected behavior since the body can move
85/// inside walls.
86///
87/// You can instead change the velocity of a dynamic or kinematic body with the [`LinearVelocity`]
88/// and [`AngularVelocity`] components:
89///
90/// ```
91#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
92#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
93/// use bevy::prelude::*;
94///
95/// # #[cfg(feature = "f32")]
96/// fn accelerate_bodies(
97///     mut query: Query<(&mut LinearVelocity, &mut AngularVelocity)>,
98///     time: Res<Time>,
99/// ) {
100///     let delta_secs = time.delta_secs();
101///     for (mut linear_velocity, mut angular_velocity) in &mut query {
102///         linear_velocity.x += 2.0 * delta_secs;
103#[cfg_attr(
104    feature = "2d",
105    doc = "        angular_velocity.0 += 0.5 * delta_secs;"
106)]
107#[cfg_attr(
108    feature = "3d",
109    doc = "        angular_velocity.z += 0.5 * delta_secs;"
110)]
111///     }
112/// }
113/// # #[cfg(feature = "f64")]
114/// # fn main() {}
115/// ```
116///
117/// For applying forces, impulses, and acceleration to dynamic bodies, see the [`forces`] module.
118///
119/// Avian does not have a built-in character controller, so if you need one,
120/// you will need to implement it yourself or use a third party option.
121/// You can take a look at the [`basic_dynamic_character`] and [`basic_kinematic_character`]
122/// examples for a simple implementation.
123///
124/// [`basic_dynamic_character`]: https://github.com/Jondolf/avian/blob/42fb8b21c756a7f4dd91071597dc251245ddaa8f/crates/avian3d/examples/basic_dynamic_character.rs
125/// [`basic_kinematic_character`]: https://github.com/Jondolf/avian/blob/42fb8b21c756a7f4dd91071597dc251245ddaa8f/crates/avian3d/examples/basic_kinematic_character.rs
126///
127/// # Mass Properties
128///
129/// Every dynamic rigid body has [mass], [angular inertia], and a [center of mass].
130/// These mass properties determine how the rigid body responds to forces and torques.
131///
132/// - **Mass**: Represents resistance to linear acceleration. A higher mass requires more force for the same acceleration.
133/// - **Angular Inertia**: Represents resistance to angular acceleration. A higher angular inertia requires more torque for the same angular acceleration.
134/// - **Center of Mass**: The average position of mass in the body. Applying forces at this point produces no torque.
135///
136/// Static and kinematic rigid bodies have infinite mass and angular inertia,
137/// and do not respond to forces or torques. Zero mass for a dynamic body is also
138/// treated as a special case, and corresponds to infinite mass.
139///
140/// If no mass properties are set, they are computed automatically from attached colliders
141/// based on their shape and density.
142///
143/// ```
144#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
145#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
146/// # use bevy::prelude::*;
147/// #
148/// # fn setup(mut commands: Commands) {
149/// // Note: `ColliderDensity` is optional, and defaults to `1.0` if not present.
150/// commands.spawn((
151///     RigidBody::Dynamic,
152///     Collider::capsule(0.5, 1.5),
153///     ColliderDensity(2.0),
154/// ));
155/// # }
156/// ```
157///
158/// If mass properties are set with the [`Mass`], [`AngularInertia`], and [`CenterOfMass`] components,
159/// they override the values computed from colliders.
160///
161/// ```
162#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
163#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
164/// # use bevy::prelude::*;
165/// #
166/// # fn setup(mut commands: Commands) {
167/// // Override mass and the center of mass, but use the collider's angular inertia.
168/// commands.spawn((
169///     RigidBody::Dynamic,
170///     Collider::capsule(0.5, 1.5),
171///     Mass(5.0),
172#[cfg_attr(feature = "2d", doc = "    CenterOfMass::new(0.0, -0.5),")]
173#[cfg_attr(feature = "3d", doc = "    CenterOfMass::new(0.0, -0.5, 0.0),")]
174/// ));
175/// # }
176/// ```
177///
178/// If the rigid body has child colliders, their mass properties will be combined for
179/// the total [`ComputedMass`], [`ComputedAngularInertia`], and [`ComputedCenterOfMass`].
180///
181/// ```
182#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
183#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
184/// # use bevy::prelude::*;
185/// #
186/// # fn setup(mut commands: Commands) {
187/// // Total mass: 10.0 + 5.0 = 15.0
188#[cfg_attr(
189    feature = "2d",
190    doc = "// Total center of mass: (10.0 * [0.0, -0.5] + 5.0 * [0.0, 4.0]) / (10.0 + 5.0) = [0.0, 1.0]"
191)]
192#[cfg_attr(
193    feature = "3d",
194    doc = "// Total center of mass: (10.0 * [0.0, -0.5, 0.0] + 5.0 * [0.0, 4.0, 0.0]) / (10.0 + 5.0) = [0.0, 1.0, 0.0]"
195)]
196/// commands.spawn((
197///     RigidBody::Dynamic,
198///     Collider::capsule(0.5, 1.5),
199///     Mass(10.0),
200#[cfg_attr(feature = "2d", doc = "    CenterOfMass::new(0.0, -0.5),")]
201#[cfg_attr(feature = "3d", doc = "    CenterOfMass::new(0.0, -0.5, 0.0),")]
202///     Transform::default(),
203/// ))
204/// .with_child((
205#[cfg_attr(feature = "2d", doc = "    Collider::circle(1.0),")]
206#[cfg_attr(feature = "3d", doc = "    Collider::sphere(1.0),")]
207///     Mass(5.0),
208///     Transform::from_xyz(0.0, 4.0, 0.0),
209/// ));
210/// # }
211/// ```
212///
213/// To prevent child entities from contributing to the total mass properties, use the [`NoAutoMass`],
214/// [`NoAutoAngularInertia`], and [`NoAutoCenterOfMass`] marker components.
215///
216/// ```
217#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
218#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
219/// # use bevy::prelude::*;
220/// #
221/// # fn setup(mut commands: Commands) {
222/// // Total mass: 10.0
223#[cfg_attr(feature = "2d", doc = "// Total center of mass: [0.0, -0.5]")]
224#[cfg_attr(feature = "3d", doc = "// Total center of mass: [0.0, -0.5, 0.0]")]
225/// commands.spawn((
226///     RigidBody::Dynamic,
227///     Collider::capsule(0.5, 1.5),
228///     Mass(10.0),
229#[cfg_attr(feature = "2d", doc = "    CenterOfMass::new(0.0, -0.5),")]
230#[cfg_attr(feature = "3d", doc = "    CenterOfMass::new(0.0, -0.5, 0.0),")]
231///     NoAutoMass,
232///     NoAutoCenterOfMass,
233///     Transform::default(),
234/// ))
235/// .with_child((
236#[cfg_attr(feature = "2d", doc = "    Collider::circle(1.0),")]
237#[cfg_attr(feature = "3d", doc = "    Collider::sphere(1.0),")]
238///     Mass(5.0),
239///     Transform::from_xyz(0.0, 4.0, 0.0),
240/// ));
241/// # }
242/// ```
243///
244/// See the [`mass_properties`] module for more information.
245///
246/// [mass]: mass_properties::components::Mass
247/// [angular inertia]: mass_properties::components::AngularInertia
248/// [center of mass]: mass_properties::components::CenterOfMass
249/// [mass properties]: mass_properties
250///
251/// # See More
252///
253/// - [Colliders](Collider)
254/// - [Gravity] and [gravity scale](GravityScale)
255/// - [Linear](LinearDamping) and [angular](AngularDamping) velocity damping
256/// - [Friction] and [restitution](Restitution) (bounciness)
257/// - [Lock translational and rotational axes](LockedAxes)
258/// - [Dominance]
259/// - [Continuous Collision Detection](dynamics::ccd)
260///     - [Speculative collision](dynamics::ccd#speculative-collision)
261///     - [Swept CCD](dynamics::ccd#swept-ccd)
262/// - [`Transform` interpolation and extrapolation](PhysicsInterpolationPlugin)
263/// - [Temporarily disabling a rigid body](RigidBodyDisabled)
264/// - [Automatic deactivation with sleeping](Sleeping)
265#[derive(Reflect, Clone, Copy, Component, Debug, Default, PartialEq, Eq)]
266#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
267#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
268#[reflect(Debug, Component, Default, PartialEq)]
269#[require(
270    // TODO: Only dynamic and kinematic bodies need velocity,
271    //       and only dynamic bodies need mass and angular inertia.
272    Position::PLACEHOLDER,
273    Rotation::PLACEHOLDER,
274    LinearVelocity,
275    AngularVelocity,
276    ComputedMass,
277    ComputedAngularInertia,
278    ComputedCenterOfMass,
279    // Required for local forces and acceleration.
280    AccumulatedLocalAcceleration,
281    // TODO: We can remove these pre-solve deltas once joints don't use XPBD.
282    PreSolveDeltaPosition,
283    PreSolveDeltaRotation,
284)]
285#[component(immutable, on_add = RigidBody::on_add)]
286pub enum RigidBody {
287    /// Dynamic bodies are bodies that are affected by forces, velocity and collisions.
288    #[default]
289    Dynamic,
290
291    /// Static bodies are not affected by any forces, collisions or velocity, and they act as if they have an infinite mass and moment of inertia.
292    /// The only way to move a static body is to manually change its position.
293    ///
294    /// Collisions with static bodies will affect dynamic bodies, but not other static bodies or kinematic bodies.
295    ///
296    /// Static bodies are typically used for things like the ground, walls and any other objects that you don't want to move.
297    Static,
298
299    /// Kinematic bodies are bodies that are not affected by any external forces or collisions.
300    /// They will realistically affect colliding dynamic bodies, but not other kinematic bodies.
301    ///
302    /// Unlike static bodies, kinematic bodies can have velocity.
303    /// The engine doesn't modify the values of a kinematic body's components,
304    /// so you have full control of them.
305    Kinematic,
306}
307
308impl RigidBody {
309    /// Checks if the rigid body is dynamic.
310    pub fn is_dynamic(&self) -> bool {
311        *self == Self::Dynamic
312    }
313
314    /// Checks if the rigid body is static.
315    pub fn is_static(&self) -> bool {
316        *self == Self::Static
317    }
318
319    /// Checks if the rigid body is kinematic.
320    pub fn is_kinematic(&self) -> bool {
321        *self == Self::Kinematic
322    }
323
324    fn on_add(mut world: DeferredWorld, ctx: HookContext) {
325        // Initialize the global physics transform for the rigid body.
326        init_physics_transform(&mut world, &ctx);
327    }
328}
329
330/// A query filter that selects rigid bodies that are neither disabled nor sleeping.
331pub(crate) type RigidBodyActiveFilter = (Without<RigidBodyDisabled>, Without<Sleeping>);
332
333/// A marker component that indicates that a [rigid body](RigidBody) is disabled
334/// and should not participate in the simulation. Disables velocity, forces, contact response,
335/// and attached joints.
336///
337/// This is useful for temporarily disabling a body without removing it from the world.
338/// To re-enable the body, simply remove this component.
339///
340/// Note that this component does *not* disable collision detection or spatial queries for colliders
341/// attached to the rigid body.
342///
343/// # Example
344///
345/// ```
346#[cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
347#[cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
348/// # use bevy::prelude::*;
349/// #
350/// #[derive(Component)]
351/// pub struct Character;
352///
353/// /// Disables physics for all rigid body characters, for example during cutscenes.
354/// fn disable_character_physics(
355///     mut commands: Commands,
356///     query: Query<Entity, (With<RigidBody>, With<Character>)>,
357/// ) {
358///     for entity in &query {
359///         commands.entity(entity).insert(RigidBodyDisabled);
360///     }
361/// }
362///
363/// /// Enables physics for all rigid body characters.
364/// fn enable_character_physics(
365///     mut commands: Commands,
366///     query: Query<Entity, (With<RigidBody>, With<Character>)>,
367/// ) {
368///     for entity in &query {
369///         commands.entity(entity).remove::<RigidBodyDisabled>();
370///     }
371/// }
372/// ```
373///
374/// # Related Components
375///
376/// - [`ColliderDisabled`]: Disables a collider.
377/// - [`JointDisabled`]: Disables a joint constraint.
378#[derive(Clone, Copy, Component, Reflect, Debug, Default)]
379#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
380#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
381#[reflect(Debug, Component, Default)]
382pub struct RigidBodyDisabled;
383
384/// The linear velocity of a [rigid body](RigidBody), typically in meters per second.
385///
386/// # Example
387///
388/// ```
389#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
390#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
391/// use bevy::prelude::*;
392///
393/// # #[cfg(feature = "f32")]
394/// fn accelerate_linear(mut query: Query<&mut LinearVelocity>, time: Res<Time>) {
395///     let delta_secs = time.delta_secs();
396///     for mut linear_velocity in &mut query {
397///         // Accelerate the entity towards +X at `2.0` units per second squared.
398///         linear_velocity.x += 2.0 * delta_secs;
399///     }
400/// }
401/// # #[cfg(feature = "f64")]
402/// # fn main() {}
403/// ```
404///
405/// # Related Components
406///
407/// - [`AngularVelocity`]: The angular velocity of a body.
408/// - [`LinearDamping`]: Reduces the linear velocity of a body over time, similar to air resistance.
409/// - [`MaxLinearSpeed`]: Clamps the linear velocity of a body.
410#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq, From)]
411#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
412#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
413#[reflect(Debug, Component, Default, PartialEq)]
414pub struct LinearVelocity(pub Vector);
415
416impl LinearVelocity {
417    /// Zero linear velocity.
418    pub const ZERO: LinearVelocity = LinearVelocity(Vector::ZERO);
419}
420
421/// The maximum linear speed of a [rigid body](RigidBody), clamping the [`LinearVelocity`],
422/// typically in meters per second.
423///
424/// This can be useful for limiting how fast bodies can move, and can help control behavior and prevent instability.
425///
426/// # Example
427///
428/// ```
429#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
430#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
431/// use bevy::prelude::*;
432///
433/// // Spawn a dynamic body with linear velocity clamped to `100.0` units per second.
434/// fn setup(mut commands: Commands) {
435///     commands.spawn((RigidBody::Dynamic, MaxLinearSpeed(100.0)));
436/// }
437/// ```
438#[derive(Reflect, Clone, Copy, Component, Debug, Deref, DerefMut, PartialEq, From)]
439#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
440#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
441#[reflect(Debug, Component, Default, PartialEq)]
442#[doc(alias = "MaxLinearVelocity")]
443pub struct MaxLinearSpeed(pub Scalar);
444
445impl Default for MaxLinearSpeed {
446    fn default() -> Self {
447        Self(Scalar::INFINITY)
448    }
449}
450
451/// The maximum angular speed of a [rigid body](RigidBody), clamping the [`AngularVelocity`],
452/// in radians per second.
453///
454/// This can be useful for limiting how fast bodies can rotate, and can help control behavior and prevent instability.
455///
456/// # Example
457///
458/// ```
459#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
460#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
461/// use bevy::prelude::*;
462///
463/// // Spawn a dynamic body with angular velocity clamped to `20.0` radians per second.
464/// fn setup(mut commands: Commands) {
465///     commands.spawn((RigidBody::Dynamic, MaxAngularSpeed(20.0)));
466/// }
467/// ```
468#[derive(Reflect, Clone, Copy, Component, Debug, Deref, DerefMut, PartialEq, From)]
469#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
470#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
471#[reflect(Debug, Component, Default, PartialEq)]
472#[doc(alias = "MaxAngularVelocity")]
473pub struct MaxAngularSpeed(pub Scalar);
474
475impl Default for MaxAngularSpeed {
476    fn default() -> Self {
477        Self(Scalar::INFINITY)
478    }
479}
480
481/// The angular velocity of a [rigid body](RigidBody) in radians per second.
482/// Positive values will result in counterclockwise rotation.
483///
484/// # Example
485///
486/// ```
487/// use avian2d::prelude::*;
488/// use bevy::prelude::*;
489///
490/// # #[cfg(feature = "f32")]
491/// fn accelerate_angular(mut query: Query<&mut AngularVelocity>, time: Res<Time>) {
492///     let delta_secs = time.delta_secs();
493///     for mut angular_velocity in &mut query {
494///         // Accelerate rotation counterclockwise at `0.5` radians per second squared.
495///         angular_velocity.0 += 0.5 * delta_secs;
496///     }
497/// }
498/// # #[cfg(feature = "f64")]
499/// # fn main() {}
500/// ```
501///
502/// # Related Components
503///
504/// - [`LinearVelocity`]: The linear velocity of a body.
505/// - [`AngularDamping`]: Reduces the angular velocity of a body over time, similar to air resistance.
506/// - [`MaxAngularSpeed`]: Clamps the angular velocity of a body.
507#[cfg(feature = "2d")]
508#[derive(Reflect, Clone, Copy, Deref, DerefMut, Component, Debug, Default, PartialEq, From)]
509#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
510#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
511#[reflect(Debug, Component, Default, PartialEq)]
512pub struct AngularVelocity(pub Scalar);
513
514/// The angular velocity of a [rigid body](RigidBody), represented as a rotation axis
515/// multiplied by the angular speed in radians per second.
516///
517/// # Example
518///
519/// ```
520/// use avian3d::prelude::*;
521/// use bevy::prelude::*;
522///
523/// # #[cfg(feature = "f32")]
524/// fn accelerate_angular(mut query: Query<&mut AngularVelocity>, time: Res<Time>) {
525///     let delta_secs = time.delta_secs();
526///     for mut angular_velocity in &mut query {
527///         // Accelerate rotation about the Z axis at `0.5` radians per second squared.
528///         angular_velocity.z += 0.5 * delta_secs;
529///     }
530/// }
531/// # #[cfg(feature = "f64")]
532/// # fn main() {}
533/// ```
534///
535/// # Related Components
536///
537/// - [`LinearVelocity`]: The linear velocity of a body.
538/// - [`AngularDamping`]: Reduces the angular velocity of a body over time, similar to air resistance.
539/// - [`MaxAngularSpeed`]: Clamps the angular velocity of a body.
540#[cfg(feature = "3d")]
541#[derive(Reflect, Clone, Copy, Component, Debug, Default, Deref, DerefMut, PartialEq, From)]
542#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
543#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
544#[reflect(Debug, Component, Default, PartialEq)]
545pub struct AngularVelocity(pub Vector);
546
547impl AngularVelocity {
548    /// Zero angular velocity.
549    #[cfg(feature = "2d")]
550    pub const ZERO: AngularVelocity = AngularVelocity(0.0);
551    /// Zero angular velocity.
552    #[cfg(feature = "3d")]
553    pub const ZERO: AngularVelocity = AngularVelocity(Vector::ZERO);
554}
555
556/// Controls how [gravity](Gravity) affects a specific [rigid body](RigidBody).
557///
558/// A gravity scale of `0.0` will disable gravity, while `2.0` will double the gravity.
559/// Using a negative value will flip the direction of the gravity.
560///
561/// # Example
562///
563/// ```
564#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
565#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
566/// use bevy::prelude::*;
567///
568/// // Spawn a dynamic body with `1.5` times the normal gravity.
569/// fn setup(mut commands: Commands) {
570///     commands.spawn((RigidBody::Dynamic, GravityScale(1.5)));
571/// }
572/// ```
573#[derive(Component, Reflect, Debug, Clone, Copy, PartialEq, PartialOrd, Deref, DerefMut, From)]
574#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
575#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
576#[reflect(Debug, Component, Default, PartialEq)]
577pub struct GravityScale(pub Scalar);
578
579impl Default for GravityScale {
580    fn default() -> Self {
581        Self(1.0)
582    }
583}
584
585/// Automatically slows down a dynamic [rigid body](RigidBody), decreasing its
586/// [linear velocity](LinearVelocity) each frame. This can be used to simulate air resistance.
587///
588/// The default linear damping coefficient is `0.0`, which corresponds to no damping.
589///
590/// # Example
591///
592/// ```
593#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
594#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
595/// use bevy::prelude::*;
596///
597/// fn setup(mut commands: Commands) {
598///     commands.spawn((RigidBody::Dynamic, LinearDamping(0.8)));
599/// }
600/// ```
601#[derive(
602    Component, Reflect, Debug, Clone, Copy, PartialEq, PartialOrd, Default, Deref, DerefMut, From,
603)]
604#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
605#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
606#[reflect(Debug, Component, Default, PartialEq)]
607pub struct LinearDamping(pub Scalar);
608
609/// Automatically slows down a dynamic [rigid body](RigidBody), decreasing its
610/// [angular velocity](AngularVelocity) each frame. This can be used to simulate air resistance.
611///
612/// The default angular damping coefficient is `0.0`, which corresponds to no damping.
613///
614/// # Example
615///
616/// ```
617#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
618#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
619/// use bevy::prelude::*;
620///
621/// fn setup(mut commands: Commands) {
622///     commands.spawn((RigidBody::Dynamic, AngularDamping(1.6)));
623/// }
624/// ```
625#[derive(
626    Component, Reflect, Debug, Clone, Copy, PartialEq, PartialOrd, Default, Deref, DerefMut, From,
627)]
628#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
629#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
630#[reflect(Debug, Component, Default, PartialEq)]
631pub struct AngularDamping(pub Scalar);
632
633/// **Dominance** allows [dynamic rigid bodies](RigidBody::Dynamic) to dominate
634/// each other during physical interactions.
635/// 
636/// The body with a higher dominance acts as if it had infinite mass, and will be unaffected during
637/// collisions and other interactions, while the other body will be affected normally.
638/// 
639/// The dominance must be between `-127` and `127`, and the default value is `0`.
640/// Note that static and kinematic bodies will always have a higher dominance value
641/// than dynamic bodies regardless of the value of this component.
642/// 
643/// # Example
644/// 
645/// ```
646#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
647#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
648/// use bevy::prelude::*;
649///
650/// // Player dominates all dynamic bodies with a dominance lower than `5`.
651/// fn spawn_player(mut commands: Commands) {
652///     commands.spawn((
653///         RigidBody::Dynamic,
654///         Collider::capsule(0.4, 1.0),
655///         Dominance(5),
656///     ));
657/// }
658/// ```
659#[rustfmt::skip]
660#[derive(Component, Reflect, Debug, Clone, Copy, Default, Deref, DerefMut, From, PartialEq, PartialOrd, Eq, Ord)]
661#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
662#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
663#[reflect(Debug, Component, Default, PartialEq)]
664pub struct Dominance(pub i8);