avian3d/dynamics/solver/mod.rs
1//! The physics solver responsible for simulating rigid body dynamics, contacts, and joints.
2//!
3//! See [`SolverPlugins`] for a plugin group that contains Avian's default solver plugins.
4
5mod plugin;
6pub use plugin::*;
7
8pub mod constraint_graph;
9pub mod contact;
10pub mod islands;
11pub mod schedule;
12pub mod softness_parameters;
13pub mod solver_body;
14#[cfg(feature = "xpbd_joints")]
15pub mod xpbd;
16
17mod diagnostics;
18pub use diagnostics::SolverDiagnostics;
19
20use crate::{
21 dynamics::{joints::joint_graph::JointGraphPlugin, solver::solver_body::SolverBodyPlugin},
22 prelude::*,
23};
24use bevy::{app::PluginGroupBuilder, prelude::*};
25
26/// A plugin group that contains Avian's default solver plugins.
27///
28/// # Plugins
29///
30/// By default, the following plugins will be added:
31///
32/// | Plugin | Description |
33/// | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
34/// | [`SolverSchedulePlugin`] | Sets up the solver and substepping loop by initializing the necessary schedules, sets and resources. |
35/// | [`SolverBodyPlugin`] | Manages [solver bodies](dynamics::solver::solver_body::SolverBody). |
36/// | [`IntegratorPlugin`] | Handles motion caused by velocity, and applies external forces and gravity. |
37/// | [`SolverPlugin`] | Manages and solves contacts, [joints](dynamics::joints), and other constraints. |
38/// | [`CcdPlugin`] | Performs [Continuous Collision Detection (CCD)](dynamics::ccd) for fast dynamic bodies. |
39/// | [`IslandPlugin`] | Manages [simulation islands](dynamics::solver::islands) for sleeping and waking. |
40/// | [`IslandSleepingPlugin`] | Manages sleeping and waking of [simulation islands](dynamics::solver::islands). |
41/// | [`JointGraphPlugin`] | Manages the [`JointGraph`] for each joint type. |
42/// | [`XpbdSolverPlugin`] | Solves joints using Extended Position-Based Dynamics (XPBD). Requires the `xpbd_joints` feature. |
43///
44/// Refer to the documentation of the plugins for more information about their responsibilities and implementations.
45#[derive(Debug, Default)]
46pub struct SolverPlugins {
47 length_unit: f32,
48}
49
50impl SolverPlugins {
51 /// Creates a [`SolverPlugins`] plugin group with the given approximate dimensions of most objects.
52 ///
53 /// The length unit will be used for initializing the [`PhysicsLengthUnit`]
54 /// resource unless it already exists.
55 pub fn new_with_length_unit(unit: f32) -> Self {
56 Self { length_unit: unit }
57 }
58}
59
60impl PluginGroup for SolverPlugins {
61 fn build(self) -> PluginGroupBuilder {
62 let builder = PluginGroupBuilder::start::<Self>()
63 .add(SolverBodyPlugin)
64 .add(SolverSchedulePlugin)
65 .add(IntegratorPlugin::default())
66 .add(SolverPlugin::new_with_length_unit(self.length_unit))
67 .add(CcdPlugin)
68 .add(IslandPlugin)
69 .add(IslandSleepingPlugin)
70 .add(JointGraphPlugin::<FixedJoint>::default())
71 .add(JointGraphPlugin::<RevoluteJoint>::default())
72 .add(JointGraphPlugin::<PrismaticJoint>::default())
73 .add(JointGraphPlugin::<DistanceJoint>::default());
74
75 #[cfg(feature = "3d")]
76 let builder = builder.add(JointGraphPlugin::<SphericalJoint>::default());
77
78 #[cfg(feature = "xpbd_joints")]
79 let builder = builder.add(XpbdSolverPlugin);
80
81 builder
82 }
83}