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