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