avian2d/lib.rs
1//! 
2//!
3//! **Avian** is an ECS-driven 2D and 3D physics engine for the [Bevy game engine](https://bevyengine.org/).
4//!
5//! Check out the [GitHub repository](https://github.com/Jondolf/avian)
6//! for more information about the design, read the [Getting Started](#getting-started)
7//! guide below to get up to speed, and take a look at the [Table of Contents](#table-of-contents)
8//! for an overview of the engine's features and their documentation.
9//!
10//! You can also check out the [FAQ](#frequently-asked-questions), and if you encounter
11//! any further problems, consider saying hello on the [Bevy Discord](https://discord.gg/bevy)!
12//!
13//! # Getting Started
14//!
15//! This short guide should help you get started with Avian.
16//!
17//! ## Add the Dependency
18//!
19//! First, add `avian2d` or `avian3d` to the dependencies in your `Cargo.toml`:
20//!
21//! ```toml
22//! # For 2D applications:
23//! [dependencies]
24//! avian2d = "0.4"
25//!
26//! # For 3D applications:
27//! [dependencies]
28//! avian3d = "0.4"
29//!
30//! # If you want to use the most up-to-date version, you can follow the main branch:
31//! [dependencies]
32//! avian3d = { git = "https://github.com/Jondolf/avian", branch = "main" }
33//! ```
34//!
35//! You can specify features by disabling the default features and manually adding
36//! the feature flags you want:
37//!
38//! ```toml
39//! [dependencies]
40//! # Add 3D Avian with double-precision floating point numbers.
41//! # `parry-f64` enables collision detection using Parry.
42//! avian3d = { version = "0.4", default-features = false, features = ["3d", "f64", "parry-f64", "xpbd_joints"] }
43//! ```
44//!
45//! ## Feature Flags
46//!
47//! | Feature | Description | Default feature |
48//! | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
49//! | `2d` | Enables 2D physics. Incompatible with `3d`. | Yes (`avian2d`) |
50//! | `3d` | Enables 3D physics. Incompatible with `2d`. | Yes (`avian3d`) |
51//! | `f32` | Enables `f32` precision for physics. Incompatible with `f64`. | Yes |
52//! | `f64` | Enables `f64` precision for physics. Incompatible with `f32`. | No |
53//! | `default-collider` | Enables the default [`Collider`]. Required for [spatial queries](spatial_query). Requires either the `parry-f32` or `parry-f64` feature. | Yes |
54//! | `parry-f32` | Enables the `f32` version of the Parry collision detection library. Also enables the `default-collider` feature. | Yes |
55//! | `parry-f64` | Enables the `f64` version of the Parry collision detection library. Also enables the `default-collider` feature. | No |
56//! | `xpbd_joints` | Enables support for [XPBD joints](dynamics::solver::xpbd). . | Yes |
57#![cfg_attr(
58 feature = "3d",
59 doc = "| `collider-from-mesh` | Allows you to create [`Collider`]s from `Mesh`es. | Yes |"
60)]
61//! | `bevy_scene` | Enables [`ColliderConstructorHierarchy`] to wait until a [`Scene`] has loaded before processing it. | Yes |
62//! | `bevy_picking` | Enables physics picking support for [`bevy_picking`] using the [`PhysicsPickingPlugin`]. The plugin must be added separately. | Yes |
63//! | `bevy_diagnostic` | Enables writing [physics diagnostics] to the [`DiagnosticsStore`] with the [`PhysicsDiagnosticsPlugin`]. The plugin must be added separately. | No |
64//! | `diagnostic_ui` | Enables [physics diagnostics] UI for performance timers and counters using the [`PhysicsDiagnosticsUiPlugin`]. The plugin must be added separately. | No |
65//! | `debug-plugin` | Enables physics debug rendering using the [`PhysicsDebugPlugin`]. The plugin must be added separately. | Yes |
66//! | `enhanced-determinism` | Enables cross-platform deterministic math, improving determinism across architectures at a small performance cost. | No |
67//! | `parallel` | Enables some extra multithreading, which improves performance for larger simulations but can add some overhead for smaller ones. | Yes |
68//! | `simd` | Enables [SIMD] optimizations. | No |
69//! | `serialize` | Enables support for serialization and deserialization using Serde. | No |
70//! | `validate` | Enables additional correctness checks and validation at the cost of worse performance. | No |
71//!
72//! [`bevy_picking`]: bevy::picking
73//! [physics diagnostics]: diagnostics
74//! [`DiagnosticsStore`]: bevy::diagnostic::DiagnosticsStore
75//! [SIMD]: https://en.wikipedia.org/wiki/Single_instruction,_multiple_data
76//!
77//! ## Add the Plugins
78//!
79//! Avian is designed to be very modular. It is built from several [plugins](PhysicsPlugins) that
80//! manage different parts of the engine. These plugins can be easily initialized and configured through
81//! the [`PhysicsPlugins`] plugin group.
82//!
83//! ```no_run
84#![cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
85#![cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
86//! use bevy::prelude::*;
87//!
88//! fn main() {
89//! App::new()
90//! .add_plugins((DefaultPlugins, PhysicsPlugins::default()))
91//! // ...your other plugins, systems and resources
92//! .run();
93//! }
94//! ```
95//!
96//! Now you can use all of Avian's components and resources to build whatever you want!
97//!
98//! For example, adding a [rigid body](RigidBody) with a [collider](Collider) is as simple as spawning an entity
99//! with the [`RigidBody`] and [`Collider`] components:
100//!
101//! ```
102#![cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
103#![cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
104//! use bevy::prelude::*;
105//!
106//! fn setup(mut commands: Commands) {
107#![cfg_attr(
108 feature = "2d",
109 doc = " commands.spawn((RigidBody::Dynamic, Collider::circle(0.5)));"
110)]
111#![cfg_attr(
112 feature = "3d",
113 doc = " commands.spawn((RigidBody::Dynamic, Collider::sphere(0.5)));"
114)]
115//! }
116//! ```
117//!
118//! You can find lots of [usage examples](https://github.com/Jondolf/avian#more-examples)
119//! in the project's [repository](https://github.com/Jondolf/avian).
120//!
121//! # Table of Contents
122//!
123//! Below is a structured overview of the documentation for the various
124//! features of the engine.
125//!
126//! ## Rigid Body Dynamics
127//!
128//! - [Rigid body types](RigidBody#rigid-body-types)
129//! - [Creating rigid bodies](RigidBody#creation)
130//! - [Movement](RigidBody#movement)
131//! - [Linear](LinearVelocity) and [angular](AngularVelocity) velocity
132//! - [External forces, impulses, and acceleration](dynamics::rigid_body::forces)
133//! - [Gravity] and [gravity scale](GravityScale)
134//! - [Mass properties](dynamics::rigid_body::mass_properties)
135//! - [Linear](LinearDamping) and [angular](AngularDamping) velocity damping
136//! - [Lock translational and rotational axes](LockedAxes)
137//! - [Dominance]
138//! - [Continuous Collision Detection (CCD)](dynamics::ccd)
139//! - [Speculative collision](dynamics::ccd#speculative-collision)
140//! - [Swept CCD](dynamics::ccd#swept-ccd)
141//! - [`Transform` interpolation and extrapolation](PhysicsInterpolationPlugin)
142//! - [Temporarily disabling a rigid body](RigidBodyDisabled)
143//! - [Automatic deactivation with sleeping](Sleeping)
144//!
145//! See the [`dynamics`] module for more details about rigid body dynamics in Avian.
146//!
147//! ## Collision Detection
148//!
149//! - [Colliders](Collider)
150//! - [Creation](Collider#creation)
151//! - [Density](ColliderDensity)
152//! - [Friction] and [restitution](Restitution) (bounciness)
153//! - [Collision layers](CollisionLayers)
154//! - [Sensors](Sensor)
155#![cfg_attr(
156 feature = "3d",
157 doc = "- Generating colliders for meshes and scenes with [`ColliderConstructor`] and [`ColliderConstructorHierarchy`]"
158)]
159//! - [Get colliding entities](CollidingEntities)
160//! - [Collision events](collision#collision-events)
161//! - [Accessing collision data](Collisions)
162//! - [Filtering and modifying contacts with hooks](CollisionHooks)
163//! - [Manual contact queries](collision::collider::contact_query)
164//! - [Temporarily disabling a collider](ColliderDisabled)
165//!
166//! See the [`collision`] module for more details about collision detection and colliders in Avian.
167//!
168//! ## Constraints and Joints
169//!
170//! - [Joints](dynamics::joints)
171//! - [Fixed joint](FixedJoint)
172//! - [Distance joint](DistanceJoint)
173//! - [Prismatic joint](PrismaticJoint)
174//! - [Revolute joint](RevoluteJoint)
175#")]
176//! - [Temporarily disabling a joint](JointDisabled)
177# (advanced)"
180)]
181//!
182//! Joint motors and articulations are not supported yet, but they will be implemented in a future release.
183//!
184//! ## Spatial Queries
185//!
186//! - [Spatial query types](spatial_query)
187//! - [Raycasting](spatial_query#raycasting) and [`RayCaster`]
188//! - [Shapecasting](spatial_query#shapecasting) and [`ShapeCaster`]
189//! - [Point projection](spatial_query#point-projection)
190//! - [Intersection tests](spatial_query#intersection-tests)
191//! - [Spatial query filters](SpatialQueryFilter)
192//! - [The `SpatialQuery` system parameter](SpatialQuery)
193//!
194//! ## Configuration
195//!
196//! - [Gravity]
197//! - [`Transform` interpolation and extrapolation](PhysicsInterpolationPlugin)
198//! - [Physics speed](Physics#physics-speed)
199//! - [Configure simulation fidelity with substeps](SubstepCount)
200//!
201//! ## Debugging and Profiling
202//!
203//! - [Physics debug rendering](PhysicsDebugPlugin)
204//! - [Physics diagnostics](diagnostics)
205//!
206//! ## Scheduling
207//!
208//! - [Schedules and sets](PhysicsSchedulePlugin#schedules-and-sets)
209//! - [`PhysicsSystems`]
210//! - [`PhysicsSchedule`] and [`PhysicsStepSystems`]
211//! - [`SubstepSchedule`]
212//! - [`SolverSystems`] and [`SubstepSolverSystems`](dynamics::solver::schedule::SubstepSolverSystems)
213//! - Many more internal system sets
214//! - [Configure the schedule used for running physics](PhysicsPlugins#custom-schedule)
215//! - [Pausing, resuming and stepping physics](Physics#pausing-resuming-and-stepping-physics)
216//! - [Usage on servers](#can-the-engine-be-used-on-servers)
217//!
218//! ## Architecture
219//!
220//! - [List of plugins and their responsibilities](PhysicsPlugins)
221//! - [Custom plugins](PhysicsPlugins#custom-plugins)
222//!
223//! # Frequently Asked Questions
224//!
225//! - [How does Avian compare to Rapier and bevy_rapier?](#how-does-avian-compare-to-rapier-and-bevy_rapier)
226//! - [Why is nothing happening?](#why-is-nothing-happening)
227//! - [Why is everything moving so slowly?](#why-is-everything-moving-so-slowly)
228//! - [Why did my rigid body suddenly vanish?](#why-did-my-rigid-body-suddenly-vanish)
229//! - [Why is performance so bad?](#why-is-performance-so-bad)
230//! - [Why does movement look choppy?](#why-does-movement-look-choppy)
231//! - [Is there a character controller?](#is-there-a-character-controller)
232//! - [Why are there separate `Position` and `Rotation` components?](#why-are-there-separate-position-and-rotation-components)
233//! - [Can the engine be used on servers?](#can-the-engine-be-used-on-servers)
234//! - [Something else?](#something-else)
235//!
236//! ## How does Avian compare to Rapier and bevy_rapier?
237//!
238//! Rapier is the biggest and most used physics engine in the Rust ecosystem, and it is currently
239//! the most mature and feature-rich option.
240//!
241//! `bevy_rapier` is a great physics integration for Bevy, but it does have several problems:
242//!
243//! - It has to maintain a separate physics world and synchronize a ton of data with Bevy each frame
244//! - The source code is difficult to inspect, as the vast majority of it is glue code and wrappers
245//! for Bevy
246//! - It has poor docs.rs documentation, and the documentation on rapier.rs is often outdated and
247//! missing features
248//! - Overall, it doesn't have a native ECS-like feel
249//!
250//! Avian on the other hand is built *for* Bevy *with* Bevy, and it uses the ECS for both the internals
251//! and the public API. This removes the need for a separate physics world, reduces overhead, and makes
252//! the source code much more approachable and easy to inspect for Bevy users.
253//!
254//! In part thanks to Bevy's modular architecture and the ECS, Avian is also highly composable,
255//! as it consists of several independent plugins and provides lots of options for configuration and extensions,
256//! from [custom schedules](PhysicsPlugins#custom-schedule) and [plugins](PhysicsPlugins#custom-plugins) to
257//! [custom joints](dynamics::joints#custom-joints) and [constraints](dynamics::solver::xpbd#custom-constraints).
258//!
259//! One disadvantage of Avian is that it is still relatively young, so it can have more bugs,
260//! some missing features, and fewer community resources and third party crates. However, it is growing quite
261//! rapidly, and it is already pretty close to feature-parity with Rapier.
262//!
263//! At the end of the day, both engines are solid options. If you are looking for a more mature and tested
264//! physics integration, `bevy_rapier` is the better choice, but if you prefer an engine with less overhead
265//! and a more native Bevy integration, consider using Avian. Their core APIs are also quite similar,
266//! so switching between them shouldn't be too difficult.
267//!
268//! ## Why is nothing happening?
269//!
270//! Make sure you have added the [`PhysicsPlugins`] plugin group and you have given your rigid bodies
271//! a [`RigidBody`] component. See the [getting started](#getting-started) section.
272//!
273//! ## Why is everything moving so slowly?
274//!
275//! If your application is in 2D, you might be using pixels as length units. This will require you to use
276//! larger velocities and forces than you would in 3D. Make sure you set [`Gravity`] to some larger value
277//! as well, because its magnitude is `9.81` by default, which is tiny in pixels.
278//!
279//! ## Why is performance so bad?
280//!
281//! It is highly recommended to enable some optimizations for debug builds, or to run your project
282//! in release mode. This can have over a 100x performance impact in some cases.
283//!
284//! Add the following to your `Cargo.toml` to enable optimizations for debug builds:
285//!
286//! ```toml
287//! # Enable a small amount of optimization in the dev profile.
288//! [profile.dev]
289//! opt-level = 1
290//!
291//! # Enable a large amount of optimization in the dev profile for dependencies.
292//! [profile.dev.package."*"]
293//! opt-level = 3
294//! ```
295//!
296//! You can also further optimize release builds by setting the number of codegen units to `1`,
297//! although this will also increase build times.
298//!
299//! ```toml
300//! [profile.release]
301//! codegen-units = 1
302//! ```
303//!
304//! If you still have performance issues, consider enabling the [`PhysicsDiagnosticsPlugin`]
305//! and [`PhysicsDiagnosticsUiPlugin`] (requires the `diagnostic_ui` feature) to see where time is being spent.
306//! See the [diagnostics](diagnostics) module for more information.
307//!
308//! ## Why does movement look choppy?
309//!
310//! To produce consistent, frame rate independent behavior, physics by default runs
311//! in the [`FixedPostUpdate`] schedule with a fixed timestep, meaning that the time between
312//! physics ticks remains constant. On some frames, physics can either not run at all or run
313//! more than once to catch up to real time. This can lead to visible stutter for movement.
314//!
315//! This stutter can be resolved by *interpolating* or *extrapolating* the positions of physics objects
316//! in between physics ticks. Avian has built-in support for this through the [`PhysicsInterpolationPlugin`],
317//! which is included in the [`PhysicsPlugins`] by default.
318//!
319//! Interpolation can be enabled for an individual entity by adding the [`TransformInterpolation`] component:
320//!
321//! ```
322#![cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
323#![cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
324//! # use bevy::prelude::*;
325//! #
326//! fn setup(mut commands: Commands) {
327//! // Enable interpolation for this rigid body.
328//! commands.spawn((
329//! RigidBody::Dynamic,
330//! Transform::default(),
331//! TransformInterpolation,
332//! ));
333//! }
334//! ```
335//!
336//! To make *all* rigid bodies interpolated by default, use [`PhysicsInterpolationPlugin::interpolate_all()`]:
337//!
338//! ```no_run
339#![cfg_attr(feature = "2d", doc = "# use avian2d::prelude::*;")]
340#![cfg_attr(feature = "3d", doc = "# use avian3d::prelude::*;")]
341//! # use bevy::prelude::*;
342//! #
343//! fn main() {
344//! App::new()
345//! .add_plugins(PhysicsPlugins::default().set(PhysicsInterpolationPlugin::interpolate_all()))
346//! // ...
347//! .run();
348//! }
349//! ```
350//!
351//! See the [`PhysicsInterpolationPlugin`] for more information.
352//!
353//! If this does not fix the choppiness, the problem could also be related to system ordering.
354//! If you have a system for camera following, make sure it runs *after* physics,
355//! but *before* Bevy's transform propagation in `PostUpdate`.
356//!
357//! ```
358//! # use bevy::prelude::*;
359//! #
360//! # let mut app = App::new();
361//! #
362//! app.add_systems(
363//! PostUpdate,
364//! camera_follow_player.before(TransformSystems::Propagate),
365//! );
366//! #
367//! # fn camera_follow_player() {}
368//! ```
369//!
370//! ## Is there a character controller?
371//!
372//! Avian does not have a built-in character controller yet, so if you need one,
373//! you will need to implement it yourself. However, third party character controllers
374//! like [`bevy_tnua`](https://github.com/idanarye/bevy-tnua) support Avian, and [`bevy_mod_wanderlust`](https://github.com/PROMETHIA-27/bevy_mod_wanderlust)
375//! and others are also likely to get Avian support soon.
376//!
377//! For custom character controllers, you can take a look at the
378#![cfg_attr(
379 feature = "2d",
380 doc = "[`dynamic_character_2d`] and [`kinematic_character_2d`] examples to get started."
381)]
382#![cfg_attr(
383 feature = "3d",
384 doc = "[`dynamic_character_3d`] and [`kinematic_character_3d`] examples to get started."
385)]
386//!
387#![cfg_attr(
388 feature = "2d",
389 doc = "[`dynamic_character_2d`]: https://github.com/Jondolf/avian/tree/main/crates/avian2d/examples/dynamic_character_2d
390[`kinematic_character_2d`]: https://github.com/Jondolf/avian/tree/main/crates/avian2d/examples/kinematic_character_2d"
391)]
392#![cfg_attr(
393 feature = "3d",
394 doc = "[`dynamic_character_3d`]: https://github.com/Jondolf/avian/tree/main/crates/avian3d/examples/dynamic_character_3d
395[`kinematic_character_3d`]: https://github.com/Jondolf/avian/tree/main/crates/avian3d/examples/kinematic_character_3d"
396)]
397//!
398//! ## Why are there separate `Position` and `Rotation` components?
399//!
400//! While `Transform` can be used for the vast majority of things, Avian internally
401//! uses separate [`Position`] and [`Rotation`] components. These are automatically
402//! kept in sync by the [`PhysicsTransformPlugin`].
403//!
404//! There are several reasons why the separate components are currently used.
405//!
406//! - Position and rotation should be global from the physics engine's point of view.
407//! - Transform scale and shearing can cause issues and rounding errors in physics.
408//! - Transform hierarchies can be problematic.
409//! - There is no `f64` version of `Transform`.
410//! - There is no 2D version of `Transform` (yet), and having a 2D version can optimize several computations.
411//! - When position and rotation are separate, we can technically have more systems running in parallel.
412//! - Only rigid bodies have rotation, particles typically don't (although we don't make a distinction yet).
413//!
414//! In external projects however, using [`Position`] and [`Rotation`] is only necessary when you
415//! need to manage positions within [`PhysicsSystems::StepSimulation`]. Elsewhere, you should be able to use `Transform`.
416//!
417//! There is also a possibility that we will revisit this if/when Bevy has a `Transform2d` component.
418//! Using `Transform` feels more idiomatic and simple, so it would be nice if it could be used directly
419//! as long as we can get around the drawbacks.
420//!
421//! ## Can the engine be used on servers?
422//!
423//! Yes! Networking often requires running the simulation in a specific schedule, and in Avian it is straightforward
424//! to [set the schedule that runs physics](PhysicsPlugins#custom-schedule) and [configure the timestep](Physics) if needed.
425//! By default, physics runs at a fixed timestep in [`FixedPostUpdate`].
426//!
427//! ## Something else?
428//!
429//! Physics engines are very large and Avian is still young, so stability issues and bugs are to be expected.
430//!
431//! If you encounter issues, please consider first taking a look at the
432//! [issues on GitHub](https://github.com/Jondolf/avian/issues) and
433//! [open a new issue](https://github.com/Jondolf/avian/issues/new) if there already isn't one regarding your problem.
434//!
435//! You can also come and say hello on the [Bevy Discord server](https://discord.com/invite/gMUk5Ph).
436//! There, you can find an Avian Physics topic on the `#ecosystem-crates` channel where you can ask questions.
437//!
438//! # License
439//!
440//! Avian is free and open source. All code in the Avian repository is dual-licensed under either:
441//!
442//! - MIT License ([LICENSE-MIT](https://github.com/Jondolf/avian/blob/main/LICENSE-MIT)
443//! or <http://opensource.org/licenses/MIT>)
444//! - Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/Jondolf/avian/blob/main/LICENSE-APACHE)
445//! or <http://www.apache.org/licenses/LICENSE-2.0>)
446//!
447//! at your option.
448
449#![doc(
450 html_logo_url = "https://raw.githubusercontent.com/Jondolf/avian/avian/assets/branding/icon.png",
451 html_favicon_url = "https://raw.githubusercontent.com/Jondolf/avian/avian/assets/branding/icon.png"
452)]
453#![allow(
454 unexpected_cfgs,
455 clippy::type_complexity,
456 clippy::too_many_arguments,
457 rustdoc::invalid_rust_codeblocks
458)]
459#![warn(clippy::doc_markdown, missing_docs)]
460
461#[cfg(all(not(feature = "f32"), not(feature = "f64")))]
462compile_error!("either feature \"f32\" or \"f64\" must be enabled");
463
464#[cfg(all(feature = "f32", feature = "f64"))]
465compile_error!("feature \"f32\" and feature \"f64\" cannot be enabled at the same time");
466
467#[cfg(all(not(feature = "2d"), not(feature = "3d")))]
468compile_error!("either feature \"2d\" or \"3d\" must be enabled");
469
470#[cfg(all(feature = "2d", feature = "3d"))]
471compile_error!("feature \"2d\" and feature \"3d\" cannot be enabled at the same time");
472
473#[cfg(all(
474 feature = "default-collider",
475 feature = "f32",
476 not(feature = "parry-f32")
477))]
478compile_error!(
479 "feature \"default-collider\" requires the feature \"parry-f32\" when \"f32\" is enabled"
480);
481
482#[cfg(all(
483 feature = "default-collider",
484 feature = "f64",
485 not(feature = "parry-f64")
486))]
487compile_error!(
488 "feature \"default-collider\" requires the feature \"parry-f64\" when \"f64\" is enabled"
489);
490
491extern crate alloc;
492
493#[cfg(all(feature = "2d", feature = "parry-f32"))]
494pub extern crate parry2d as parry;
495
496#[cfg(all(feature = "2d", feature = "parry-f64"))]
497pub extern crate parry2d_f64 as parry;
498
499#[cfg(all(feature = "3d", feature = "parry-f32"))]
500pub extern crate parry3d as parry;
501
502#[cfg(all(feature = "3d", feature = "parry-f64"))]
503pub extern crate parry3d_f64 as parry;
504
505pub mod collision;
506#[cfg(feature = "debug-plugin")]
507pub mod debug_render;
508pub mod diagnostics;
509pub mod dynamics;
510pub mod interpolation;
511pub mod math;
512pub mod physics_transform;
513#[cfg(feature = "bevy_picking")]
514pub mod picking;
515pub mod schedule;
516pub mod spatial_query;
517
518pub mod data_structures;
519
520// TODO: Where should this go?
521pub(crate) mod ancestor_marker;
522
523/// Re-exports common components, bundles, resources, plugins and types.
524pub mod prelude {
525 #[cfg(feature = "debug-plugin")]
526 pub use crate::debug_render::*;
527 #[cfg(feature = "bevy_diagnostic")]
528 pub use crate::diagnostics::PhysicsDiagnosticsPlugin;
529 #[cfg(feature = "diagnostic_ui")]
530 pub use crate::diagnostics::ui::{PhysicsDiagnosticsUiPlugin, PhysicsDiagnosticsUiSettings};
531 #[cfg(feature = "default-collider")]
532 pub(crate) use crate::physics_transform::RotationValue;
533 #[cfg(feature = "bevy_picking")]
534 pub use crate::picking::{
535 PhysicsPickable, PhysicsPickingFilter, PhysicsPickingPlugin, PhysicsPickingSettings,
536 };
537 #[expect(deprecated)]
538 pub use crate::{
539 PhysicsPlugins,
540 collision::prelude::*,
541 dynamics::{self, ccd::SpeculativeMargin, prelude::*},
542 interpolation::*,
543 physics_transform::{PhysicsTransformHelper, PhysicsTransformPlugin, Position, Rotation},
544 schedule::{
545 Physics, PhysicsSchedule, PhysicsSchedulePlugin, PhysicsSet, PhysicsStepSet,
546 PhysicsStepSystems, PhysicsSystems, PhysicsTime, Substeps,
547 },
548 spatial_query::{self, *},
549 };
550 pub(crate) use crate::{
551 diagnostics::AppDiagnosticsExt,
552 math::*,
553 physics_transform::{PreSolveDeltaPosition, PreSolveDeltaRotation},
554 schedule::TimePrecisionAdjusted,
555 };
556 pub use avian_derive::*;
557}
558
559mod utils;
560
561#[cfg(test)]
562mod tests;
563
564use bevy::{
565 app::PluginGroupBuilder,
566 ecs::{intern::Interned, schedule::ScheduleLabel, system::SystemParamItem},
567 prelude::*,
568};
569#[allow(unused_imports)]
570use prelude::*;
571
572/// A plugin group containing Avian's plugins.
573///
574/// # Plugins
575///
576/// By default, the following plugins will be added:
577///
578/// | Plugin | Description |
579/// | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
580/// | [`PhysicsSchedulePlugin`] | Sets up the physics engine by initializing the necessary schedules, sets and resources. |
581/// | [`ColliderBackendPlugin`] | Handles generic collider backend logic, like initializing colliders and AABBs and updating related components. |
582/// | [`ColliderHierarchyPlugin`] | Manages [`ColliderOf`] relationships based on the entity hierarchy. |
583/// | [`ColliderTransformPlugin`] | Propagates and updates transforms for colliders.
584#[cfg_attr(
585 all(feature = "collider-from-mesh", feature = "default-collider"),
586 doc = "| [`ColliderCachePlugin`] | Caches colliders created from meshes. Requires `collider-from-mesh` and `default-collider` features. |"
587)]
588/// | [`BroadPhasePlugin`] | Finds pairs of entities with overlapping [AABBs](ColliderAabb) to reduce the number of potential contacts for the [narrow phase](collision::narrow_phase). |
589/// | [`NarrowPhasePlugin`] | Manages contacts and generates contact constraints. |
590/// | [`SolverPlugins`] | A plugin group for the physics solver's plugins. See the plugin group's documentation for more information. |
591/// | [`JointPlugin`] | A plugin for managing and initializing [joints](dynamics::joints). Does *not* include the actual joint solver. |
592/// | [`MassPropertyPlugin`] | Manages mass properties of dynamic [rigid bodies](RigidBody). |
593/// | [`ForcePlugin`] | Manages and applies external forces, torques, and acceleration for rigid bodies. See the [module-level documentation](dynamics::rigid_body::forces). |
594/// | [`SpatialQueryPlugin`] | Handles spatial queries like [raycasting](spatial_query#raycasting) and [shapecasting](spatial_query#shapecasting). |
595/// | [`PhysicsInterpolationPlugin`] | [`Transform`] interpolation and extrapolation for rigid bodies. |
596/// | [`PhysicsTransformPlugin`] | Manages physics transforms and synchronizes them with [`Transform`]. |
597///
598/// Optional additional plugins include:
599///
600/// | Plugin | Description |
601/// | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
602/// | [`PhysicsPickingPlugin`] | Enables a physics picking backend for [`bevy_picking`](bevy::picking) (only with `bevy_picking` feature enabled). |
603/// | [`PhysicsDebugPlugin`] | Renders physics objects and events like [AABBs](ColliderAabb) and contacts for debugging purposes (only with `debug-plugin` feature enabled). |
604/// | [`PhysicsDiagnosticsPlugin`] | Writes [physics diagnostics](diagnostics) to the [`DiagnosticsStore`] (only with `bevy_diagnostic` feature enabled). |
605/// | [`PhysicsDiagnosticsUiPlugin`] | Displays [physics diagnostics](diagnostics) with a debug UI overlay (only with `diagnostic_ui` feature enabled). |
606///
607/// [`DiagnosticsStore`]: bevy::diagnostic::DiagnosticsStore
608///
609/// Refer to the documentation of the plugins for more information about their responsibilities and implementations.
610///
611/// # World Scale
612///
613/// The [`PhysicsLengthUnit`] resource is a units-per-meter scaling factor
614/// that adjusts the engine's internal properties to the scale of the world.
615/// It is recommended to configure the length unit to match the approximate length
616/// of the average dynamic object in the world to get the best simulation results.
617///
618/// For example, a 2D game might use pixels as units and have an average object size
619/// of around 100 pixels. By setting the length unit to `100.0`, the physics engine
620/// will interpret 100 pixels as 1 meter for internal thresholds, improving stability.
621///
622/// The length unit can be set by inserting the resource like normal,
623/// but it can also be specified through the [`PhysicsPlugins`] plugin group.
624///
625/// ```no_run
626/// # #[cfg(feature = "2d")]
627/// use avian2d::prelude::*;
628/// use bevy::prelude::*;
629///
630/// # #[cfg(feature = "2d")]
631/// fn main() {
632/// App::new()
633/// .add_plugins((
634/// DefaultPlugins,
635/// // A 2D game with 100 pixels per meter
636/// PhysicsPlugins::default().with_length_unit(100.0),
637/// ))
638/// .run();
639/// }
640/// # #[cfg(not(feature = "2d"))]
641/// # fn main() {} // Doc test needs main
642/// ```
643///
644/// # Custom Schedule
645///
646/// You can run the [`PhysicsSchedule`] in any schedule you want by specifying the schedule when adding the plugin group:
647///
648/// ```no_run
649#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
650#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
651/// use bevy::prelude::*;
652///
653/// fn main() {
654/// App::new()
655/// // Run physics at a variable timestep in `PostUpdate`.
656/// .add_plugins((DefaultPlugins, PhysicsPlugins::new(PostUpdate)))
657/// .run();
658/// }
659/// ```
660///
661/// # Custom Plugins
662///
663/// First, create a new plugin. If you want to run your systems in the engine's schedules, get either the [`PhysicsSchedule`]
664/// or the [`SubstepSchedule`]. Then you can add your systems to that schedule and control system ordering with system sets like
665/// [`PhysicsStepSystems`], [`SolverSystems`], or [`SubstepSolverSystems`](dynamics::solver::schedule::SubstepSolverSystems).
666///
667/// Here we will create a custom broad phase plugin that will replace the default [`BroadPhasePlugin`]:
668///
669/// ```
670#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
671#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
672/// use bevy::prelude::*;
673///
674/// pub struct CustomBroadPhasePlugin;
675///
676/// impl Plugin for CustomBroadPhasePlugin {
677/// fn build(&self, app: &mut App) {
678/// // Make sure the PhysicsSchedule is available
679/// let physics_schedule = app
680/// .get_schedule_mut(PhysicsSchedule)
681/// .expect("add PhysicsSchedule first");
682///
683/// // Add the system into the broad phase system set
684/// physics_schedule.add_systems(collect_collision_pairs.in_set(PhysicsStepSystems::BroadPhase));
685/// }
686/// }
687///
688/// fn collect_collision_pairs() {
689/// // Implementation goes here
690/// }
691/// ```
692///
693/// Next, when creating your app, simply disable the default [`BroadPhasePlugin`] and add your custom plugin:
694///
695/// ```no_run
696#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
697#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
698/// use bevy::prelude::*;
699///
700/// # struct CustomBroadPhasePlugin;
701/// # impl Plugin for CustomBroadPhasePlugin {
702/// # fn build(&self, app: &mut App) {}
703/// # }
704/// #
705/// fn main() {
706/// let mut app = App::new();
707///
708/// app.add_plugins(DefaultPlugins);
709///
710/// // Add PhysicsPlugins and replace default broad phase with our custom broad phase
711/// app.add_plugins(
712/// PhysicsPlugins::default()
713/// .build()
714/// .disable::<BroadPhasePlugin>()
715/// .add(CustomBroadPhasePlugin),
716/// );
717///
718/// app.run();
719/// }
720/// ```
721///
722/// You can find a full working example
723/// [here](https://github.com/Jondolf/avian/blob/main/crates/avian3d/examples/custom_broad_phase.rs).
724pub struct PhysicsPlugins {
725 schedule: Interned<dyn ScheduleLabel>,
726 length_unit: Scalar,
727}
728
729impl PhysicsPlugins {
730 /// Creates a [`PhysicsPlugins`] plugin group using the given schedule for running the [`PhysicsSchedule`].
731 ///
732 /// The default schedule is `FixedPostUpdate`.
733 pub fn new(schedule: impl ScheduleLabel) -> Self {
734 Self {
735 schedule: schedule.intern(),
736 length_unit: 1.0,
737 }
738 }
739
740 /// Adds the given [`CollisionHooks`] for user-defined contact filtering and modification.
741 ///
742 /// Returns a [`PhysicsPluginsWithHooks`] plugin group, which wraps the original [`PhysicsPlugins`],
743 /// and applies the provided hooks. Only one set of collision hooks can be defined per application.
744 pub fn with_collision_hooks<H: CollisionHooks + 'static>(self) -> PhysicsPluginsWithHooks<H>
745 where
746 for<'w, 's> SystemParamItem<'w, 's, H>: CollisionHooks,
747 {
748 PhysicsPluginsWithHooks::<H> {
749 plugins: self,
750 _phantom: core::marker::PhantomData,
751 }
752 }
753
754 /// Sets the value used for the [`PhysicsLengthUnit`], a units-per-meter scaling factor
755 /// that adjusts the engine's internal properties to the scale of the world.
756 ///
757 /// For example, a 2D game might use pixels as units and have an average object size
758 /// of around 100 pixels. By setting the length unit to `100.0`, the physics engine
759 /// will interpret 100 pixels as 1 meter for internal thresholds, improving stability.
760 ///
761 /// Note that this is *not* used to scale forces or any other user-facing inputs or outputs.
762 /// Instead, the value is only used to scale some internal length-based tolerances, such as
763 /// [`SleepingThreshold::linear`] and [`NarrowPhaseConfig::default_speculative_margin`],
764 /// as well as the scale used for [debug rendering](PhysicsDebugPlugin).
765 ///
766 /// Choosing the appropriate length unit can help improve stability and robustness.
767 ///
768 /// # Example
769 ///
770 /// ```no_run
771 /// # #[cfg(feature = "2d")]
772 /// use avian2d::prelude::*;
773 /// use bevy::prelude::*;
774 ///
775 /// # #[cfg(feature = "2d")]
776 /// fn main() {
777 /// App::new()
778 /// .add_plugins((
779 /// DefaultPlugins,
780 /// // A 2D game with 100 pixels per meter
781 /// PhysicsPlugins::default().with_length_unit(100.0),
782 /// ))
783 /// .run();
784 /// }
785 /// # #[cfg(not(feature = "2d"))]
786 /// # fn main() {} // Doc test needs main
787 /// ```
788 pub fn with_length_unit(mut self, unit: Scalar) -> Self {
789 self.length_unit = unit;
790 self
791 }
792}
793
794impl Default for PhysicsPlugins {
795 fn default() -> Self {
796 Self::new(FixedPostUpdate)
797 }
798}
799
800impl PluginGroup for PhysicsPlugins {
801 fn build(self) -> PluginGroupBuilder {
802 let builder = PluginGroupBuilder::start::<Self>()
803 .add(PhysicsSchedulePlugin::new(self.schedule))
804 .add(MassPropertyPlugin::new(self.schedule))
805 .add(ForcePlugin)
806 .add(ColliderHierarchyPlugin)
807 .add(ColliderTransformPlugin::new(self.schedule));
808
809 #[cfg(all(feature = "collider-from-mesh", feature = "default-collider"))]
810 let builder = builder.add(ColliderCachePlugin);
811
812 #[cfg(all(
813 feature = "default-collider",
814 any(feature = "parry-f32", feature = "parry-f64")
815 ))]
816 let builder = builder
817 .add(ColliderBackendPlugin::<Collider>::new(self.schedule))
818 .add(NarrowPhasePlugin::<Collider>::default());
819
820 // Add solver plugins.
821 let builder = builder.add_group(SolverPlugins::new_with_length_unit(self.length_unit));
822
823 builder
824 .add(BroadPhasePlugin::<()>::default())
825 .add(JointPlugin)
826 .add(SpatialQueryPlugin)
827 .add(PhysicsTransformPlugin::new(self.schedule))
828 .add(PhysicsInterpolationPlugin::default())
829 }
830}
831
832// This type is separate from `PhysicsPlugins` to avoid requiring users to use generics
833// like `PhysicsPlugins::<()>::default()` unless they actually want to use collision hooks.
834/// A [`PhysicsPlugins`] plugin group with [`CollisionHooks`] specified.
835pub struct PhysicsPluginsWithHooks<H: CollisionHooks> {
836 plugins: PhysicsPlugins,
837 _phantom: core::marker::PhantomData<H>,
838}
839
840impl<H: CollisionHooks> PhysicsPluginsWithHooks<H> {
841 /// Creates a new [`PhysicsPluginsWithHooks`] plugin group using the given [`CollisionHooks`]
842 /// and schedule for running the [`PhysicsSchedule`].
843 ///
844 /// The default schedule is [`FixedPostUpdate`].
845 pub fn new(schedule: impl ScheduleLabel) -> Self {
846 Self {
847 plugins: PhysicsPlugins::new(schedule),
848 _phantom: core::marker::PhantomData,
849 }
850 }
851
852 /// Sets the value used for the [`PhysicsLengthUnit`], a units-per-meter scaling factor
853 /// that adjusts the engine's internal properties to the scale of the world.
854 ///
855 /// See [`PhysicsPlugins::with_length_unit`] for more information.
856 pub fn with_length_unit(mut self, unit: Scalar) -> Self {
857 self.plugins.length_unit = unit;
858 self
859 }
860}
861
862impl<H: CollisionHooks> Default for PhysicsPluginsWithHooks<H> {
863 fn default() -> Self {
864 Self {
865 plugins: PhysicsPlugins::default(),
866 _phantom: core::marker::PhantomData,
867 }
868 }
869}
870
871impl<H: CollisionHooks + 'static> PluginGroup for PhysicsPluginsWithHooks<H>
872where
873 for<'w, 's> SystemParamItem<'w, 's, H>: CollisionHooks,
874{
875 fn build(self) -> PluginGroupBuilder {
876 // Replace the default collision hooks with the user-defined ones.
877 let builder = self
878 .plugins
879 .build()
880 .disable::<BroadPhasePlugin>()
881 .add(BroadPhasePlugin::<H>::default());
882
883 #[cfg(all(
884 feature = "default-collider",
885 any(feature = "parry-f32", feature = "parry-f64")
886 ))]
887 let builder = builder
888 .disable::<NarrowPhasePlugin<Collider>>()
889 .add(NarrowPhasePlugin::<Collider, H>::default());
890
891 builder
892 }
893}