bevy_rapier2d/plugin/configuration.rs
1//! Components used to configure a simulation run by rapier, these are not modified by bevy_rapier.
2
3use bevy::{
4 prelude::{Component, Resource},
5 reflect::Reflect,
6};
7
8use crate::math::{Real, Vect};
9
10#[cfg(doc)]
11use {crate::prelude::TransformInterpolation, rapier::dynamics::IntegrationParameters};
12
13/// The different ways of adjusting the timestep length each frame.
14#[derive(Copy, Clone, Debug, PartialEq, Resource)]
15pub enum TimestepMode {
16 /// Use a fixed timestep: the physics simulation will be advanced by the fixed value
17 /// `dt` seconds at each Bevy tick by performing `substeps` of length `dt / substeps`.
18 Fixed {
19 /// The physics simulation will be advanced by this total amount at each Bevy tick.
20 dt: f32,
21 /// This number of substeps of length `dt / substeps` will be performed at each Bevy tick.
22 substeps: usize,
23 },
24 /// Use a variable timestep: the physics simulation will be advanced by the variable value
25 /// `min(max_dt, Time::delta_seconds() * time_scale)` seconds at each Bevy tick. If
26 /// `time_scale > 1.0` then the simulation will appear to run faster than real-time whereas
27 /// `time_scale < 1.0` makes the simulation run in slow-motion.
28 Variable {
29 /// Maximum amount of time the physics simulation may be advanced at each Bevy tick.
30 max_dt: f32,
31 /// Multiplier controlling if the physics simulation should advance faster (> 1.0),
32 /// at the same speed (= 1.0) or slower (< 1.0) than the real time.
33 time_scale: f32,
34 /// The number of substeps that will be performed at each tick.
35 substeps: usize,
36 },
37 /// Use a fixed timestep equal to `IntegrationParameters::dt`, but don't step if the
38 /// physics simulation advanced by a time greater than the real-world elapsed time multiplied by `time_scale`.
39 /// Rigid-bodies with a component [`TransformInterpolation`] attached will use interpolation to
40 /// estimate the rigid-bodies position in-between steps.
41 Interpolated {
42 /// The physics simulation will be advanced by this total amount at each Bevy tick, unless
43 /// the physics simulation time is ahead of a the real time.
44 dt: f32,
45 /// Multiplier controlling if the physics simulation should advance faster (> 1.0),
46 /// at the same speed (= 1.0) or slower (< 1.0) than the real time.
47 time_scale: f32,
48 /// The number of substeps that will be performed whenever the physics simulation is advanced.
49 substeps: usize,
50 },
51}
52
53impl Default for TimestepMode {
54 fn default() -> Self {
55 TimestepMode::Variable {
56 max_dt: 1.0 / 60.0,
57 time_scale: 1.0,
58 substeps: 1,
59 }
60 }
61}
62
63#[derive(Component, Copy, Clone, Debug, Reflect)]
64/// A component for specifying configuration information for the physics simulation
65pub struct RapierConfiguration {
66 /// Specifying the gravity of the physics simulation.
67 pub gravity: Vect,
68 /// Specifies if the physics simulation is active and update the physics world.
69 pub physics_pipeline_active: bool,
70 /// Specifies the number of subdivisions along each axes a shape should be subdivided
71 /// if its scaled representation cannot be represented with the same shape type.
72 ///
73 /// For example, a ball subject to a non-uniform scaling cannot be represented as a ball
74 /// (it’s an ellipsoid). Thus, in order to be compatible with Rapier, the shape is automatically
75 /// discretized into a convex polyhedron, using `scaled_shape_subdivision` as the number of subdivisions
76 /// along each spherical coordinates angle.
77 pub scaled_shape_subdivision: u32,
78 /// Specifies if backend sync should always accept transform changes, which may be from the writeback stage.
79 pub force_update_from_transform_changes: bool,
80}
81
82impl RapierConfiguration {
83 /// Configures rapier with the specified length unit.
84 ///
85 /// See the documentation of [`IntegrationParameters::length_unit`] for additional details
86 /// on that argument.
87 ///
88 /// The default gravity is automatically scaled by that length unit.
89 pub fn new(length_unit: Real) -> Self {
90 Self {
91 gravity: Vect::Y * -9.81 * length_unit,
92 physics_pipeline_active: true,
93 scaled_shape_subdivision: 10,
94 force_update_from_transform_changes: false,
95 }
96 }
97}