rapier3d/dynamics/integration_parameters.rs
1#[cfg(doc)]
2use super::RigidBodyActivation;
3use crate::math::Real;
4use simba::simd::SimdRealField;
5
6// TODO: enabling the block solver in 3d introduces a lot of jitters in
7// the 3D domino demo. So for now we dont enable it in 3D.
8pub(crate) static BLOCK_SOLVER_ENABLED: bool = cfg!(feature = "dim2");
9
10/// Friction models used for all contact constraints between two rigid-bodies.
11///
12/// This selection does not apply to multibodies that always rely on the [`FrictionModel::Coulomb`].
13#[cfg(feature = "dim3")]
14#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
16pub enum FrictionModel {
17 /// A simplified friction model significantly faster to solve than [`Self::Coulomb`]
18 /// but less accurate.
19 ///
20 /// Instead of solving one Coulomb friction constraint per contact in a contact manifold,
21 /// this approximation only solves one Coulomb friction constraint per group of 4 contacts
22 /// in a contact manifold, plus one "twist" constraint. The "twist" constraint is purely
23 /// rotational and aims to eliminate angular movement in the manifold’s tangent plane.
24 #[default]
25 Simplified,
26 /// The coulomb friction model.
27 ///
28 /// This results in one Coulomb friction constraint per contact point.
29 Coulomb,
30}
31
32#[derive(Copy, Clone, Debug, PartialEq)]
33#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
34// TODO: we should be able to combine this with MotorModel.
35/// Coefficients for a spring, typically used for configuring constraint softness for contacts and
36/// joints.
37pub struct SpringCoefficients<N> {
38 /// Sets the natural frequency (Hz) of the spring-like constraint.
39 ///
40 /// Higher values make the constraint stiffer and resolve constraint violations more quickly.
41 pub natural_frequency: N,
42 /// Sets the damping ratio for the spring-like constraint.
43 ///
44 /// Larger values make the joint more compliant (allowing more drift before stabilization).
45 pub damping_ratio: N,
46}
47
48impl<N: SimdRealField<Element = Real> + Copy> SpringCoefficients<N> {
49 /// Initializes spring coefficients from the spring natural frequency and damping ratio.
50 pub fn new(natural_frequency: N, damping_ratio: N) -> Self {
51 Self {
52 natural_frequency,
53 damping_ratio,
54 }
55 }
56
57 /// Default softness coefficients for contacts.
58 pub fn contact_defaults() -> Self {
59 Self {
60 natural_frequency: N::splat(30.0),
61 damping_ratio: N::splat(5.0),
62 }
63 }
64
65 /// Default softness coefficients for joints.
66 pub fn joint_defaults() -> Self {
67 Self {
68 natural_frequency: N::splat(1.0e6),
69 damping_ratio: N::splat(1.0),
70 }
71 }
72
73 /// The contact’s spring angular frequency for constraints regularization.
74 pub fn angular_frequency(&self) -> N {
75 self.natural_frequency * N::simd_two_pi()
76 }
77
78 /// The [`Self::erp`] coefficient, multiplied by the inverse timestep length.
79 pub fn erp_inv_dt(&self, dt: N) -> N {
80 let ang_freq = self.angular_frequency();
81 ang_freq / (dt * ang_freq + N::splat(2.0) * self.damping_ratio)
82 }
83
84 /// The effective Error Reduction Parameter applied for calculating regularization forces.
85 ///
86 /// This parameter is computed automatically from [`Self::natural_frequency`],
87 /// [`Self::damping_ratio`] and the substep length.
88 pub fn erp(&self, dt: N) -> N {
89 dt * self.erp_inv_dt(dt)
90 }
91
92 /// Compute CFM assuming a critically damped spring multiplied by the damping ratio.
93 ///
94 /// This coefficient softens the impulse applied at each solver iteration.
95 pub fn cfm_coeff(&self, dt: N) -> N {
96 let one = N::one();
97 let erp = self.erp(dt);
98 let erp_is_not_zero = erp.simd_ne(N::zero());
99 let inv_erp_minus_one = one / erp - one;
100
101 // let stiffness = 4.0 * damping_ratio * damping_ratio * projected_mass
102 // / (dt * dt * inv_erp_minus_one * inv_erp_minus_one);
103 // let damping = 4.0 * damping_ratio * damping_ratio * projected_mass
104 // / (dt * inv_erp_minus_one);
105 // let cfm = 1.0 / (dt * dt * stiffness + dt * damping);
106 // NOTE: This simplifies to cfm = cfm_coeff / projected_mass:
107 let result = inv_erp_minus_one * inv_erp_minus_one
108 / ((one + inv_erp_minus_one) * N::splat(4.0) * self.damping_ratio * self.damping_ratio);
109 result.select(erp_is_not_zero, N::zero())
110 }
111
112 /// The CFM factor to be used in the constraint resolution.
113 ///
114 /// This parameter is computed automatically from [`Self::natural_frequency`],
115 /// [`Self::damping_ratio`] and the substep length.
116 pub fn cfm_factor(&self, dt: N) -> N {
117 let one = N::one();
118 let cfm_coeff = self.cfm_coeff(dt);
119
120 // We use this coefficient inside the impulse resolution.
121 // Surprisingly, several simplifications happen there.
122 // Let `m` the projected mass of the constraint.
123 // Let `m’` the projected mass that includes CFM: `m’ = 1 / (1 / m + cfm_coeff / m) = m / (1 + cfm_coeff)`
124 // We have:
125 // new_impulse = old_impulse - m’ (delta_vel - cfm * old_impulse)
126 // = old_impulse - m / (1 + cfm_coeff) * (delta_vel - cfm_coeff / m * old_impulse)
127 // = old_impulse * (1 - cfm_coeff / (1 + cfm_coeff)) - m / (1 + cfm_coeff) * delta_vel
128 // = old_impulse / (1 + cfm_coeff) - m * delta_vel / (1 + cfm_coeff)
129 // = 1 / (1 + cfm_coeff) * (old_impulse - m * delta_vel)
130 // So, setting cfm_factor = 1 / (1 + cfm_coeff).
131 // We obtain:
132 // new_impulse = cfm_factor * (old_impulse - m * delta_vel)
133 //
134 // The value returned by this function is this cfm_factor that can be used directly
135 // in the constraint solver.
136 one / (one + cfm_coeff)
137 }
138}
139
140/// Configuration parameters that control the physics simulation quality and behavior.
141///
142/// These parameters affect how the physics engine advances time, resolves collisions, and
143/// maintains stability. The defaults work well for most games, but you may want to adjust
144/// them based on your specific needs.
145///
146/// # Key parameters for beginners
147///
148/// - **`dt`**: Timestep duration (default: 1/60 second). Most games run physics at 60Hz.
149/// - **`num_solver_iterations`**: More iterations = more accurate but slower (default: 4)
150/// - **`length_unit`**: Scale factor if your world units aren't meters (e.g., 100 for pixel-based games)
151///
152/// # Example
153///
154/// ```
155/// # use rapier3d::prelude::*;
156/// // Standard 60 FPS physics with default settings
157/// let mut integration_params = IntegrationParameters::default();
158///
159/// // For a more accurate (but slower) simulation:
160/// integration_params.num_solver_iterations = 8;
161///
162/// // For pixel-based 2D games where 100 pixels = 1 meter:
163/// integration_params.length_unit = 100.0;
164/// ```
165///
166/// Most other parameters are advanced settings for fine-tuning stability and performance.
167#[derive(Copy, Clone, Debug, PartialEq)]
168#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
169pub struct IntegrationParameters {
170 /// The timestep length - how much simulated time passes per physics step (default: `1.0 / 60.0`).
171 ///
172 /// Set this to `1.0 / your_target_fps`. For example:
173 /// - 60 FPS: `1.0 / 60.0` ≈ 0.0167 seconds
174 /// - 120 FPS: `1.0 / 120.0` ≈ 0.0083 seconds
175 ///
176 /// Smaller timesteps are more accurate but require more CPU time per second of simulated time.
177 pub dt: Real,
178 /// Minimum timestep size when using CCD with multiple substeps (default: `1.0 / 60.0 / 100.0`).
179 ///
180 /// When CCD with multiple substeps is enabled, the timestep is subdivided
181 /// into smaller pieces. This timestep subdivision won't generate timestep
182 /// lengths smaller than `min_ccd_dt`.
183 ///
184 /// Setting this to a large value will reduce the opportunity to performing
185 /// CCD substepping, resulting in potentially more time dropped by the
186 /// motion-clamping mechanism. Setting this to an very small value may lead
187 /// to numerical instabilities.
188 pub min_ccd_dt: Real,
189
190 /// Softness coefficients for contact constraints.
191 pub contact_softness: SpringCoefficients<Real>,
192
193 /// The coefficient in `[0, 1]` applied to warmstart impulses, i.e., impulses that are used as the
194 /// initial solution (instead of 0) at the next simulation step.
195 ///
196 /// This should generally be set to 1.
197 ///
198 /// (default `1.0`).
199 pub warmstart_coefficient: Real,
200
201 /// The scale factor for your world if you're not using meters (default: `1.0`).
202 ///
203 /// Rapier is tuned for human-scale objects measured in meters. If your game uses different
204 /// units, set this to how many of your units equal 1 meter in the real world.
205 ///
206 /// **Examples:**
207 /// - Your game uses meters: `length_unit = 1.0` (default)
208 /// - Your game uses centimeters: `length_unit = 100.0` (100 cm = 1 m)
209 /// - Pixel-based 2D game where typical objects are 100 pixels tall: `length_unit = 100.0`
210 /// - Your game uses feet: `length_unit = 3.28` (approximately)
211 ///
212 /// This automatically scales various internal tolerances and thresholds to work correctly
213 /// with your chosen units.
214 pub length_unit: Real,
215
216 /// Amount of penetration the engine won’t attempt to correct (default: `0.001m`).
217 ///
218 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
219 pub normalized_allowed_linear_error: Real,
220 /// Maximum amount of penetration the solver will attempt to resolve in one timestep (default: `10.0`).
221 ///
222 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
223 pub normalized_max_corrective_velocity: Real,
224 /// The maximal distance separating two objects that will generate predictive contacts (default: `0.002m`).
225 ///
226 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
227 pub normalized_prediction_distance: Real,
228 /// The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
229 ///
230 /// Higher values produce more accurate and stable simulations at the cost of performance.
231 /// - `4` (default): Good balance for most games
232 /// - `8-12`: Use for demanding scenarios (stacks of objects, complex machinery)
233 /// - `1-2`: Use if performance is critical and accuracy can be sacrificed
234 pub num_solver_iterations: usize,
235 /// Number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration (default: `1`).
236 pub num_internal_pgs_iterations: usize,
237 /// The number of stabilization iterations run at each solver iterations (default: `1`).
238 pub num_internal_stabilization_iterations: usize,
239 /// Minimum number of dynamic bodies on each active island (default: `128`).
240 pub min_island_size: usize,
241 /// Maximum number of substeps performed by the solver (default: `1`).
242 pub max_ccd_substeps: usize,
243 /// The type of friction constraints used in the simulation.
244 #[cfg(feature = "dim3")]
245 pub friction_model: FrictionModel,
246}
247
248impl IntegrationParameters {
249 /// The inverse of the time-stepping length, i.e. the steps per seconds (Hz).
250 ///
251 /// This is zero if `self.dt` is zero.
252 #[inline]
253 pub fn inv_dt(&self) -> Real {
254 if self.dt == 0.0 { 0.0 } else { 1.0 / self.dt }
255 }
256
257 /// Sets the time-stepping length.
258 #[inline]
259 #[deprecated = "You can just set the `IntegrationParams::dt` value directly"]
260 pub fn set_dt(&mut self, dt: Real) {
261 assert!(dt >= 0.0, "The time-stepping length cannot be negative.");
262 self.dt = dt;
263 }
264
265 /// Sets the inverse time-stepping length (i.e. the frequency).
266 ///
267 /// This automatically recompute `self.dt`.
268 #[inline]
269 pub fn set_inv_dt(&mut self, inv_dt: Real) {
270 if inv_dt == 0.0 {
271 self.dt = 0.0
272 } else {
273 self.dt = 1.0 / inv_dt
274 }
275 }
276
277 /// Amount of penetration the engine won't attempt to correct (default: `0.001` multiplied by
278 /// [`Self::length_unit`]).
279 pub fn allowed_linear_error(&self) -> Real {
280 self.normalized_allowed_linear_error * self.length_unit
281 }
282
283 /// Maximum amount of penetration the solver will attempt to resolve in one timestep.
284 ///
285 /// This is equal to [`Self::normalized_max_corrective_velocity`] multiplied by
286 /// [`Self::length_unit`].
287 pub fn max_corrective_velocity(&self) -> Real {
288 if self.normalized_max_corrective_velocity != Real::MAX {
289 self.normalized_max_corrective_velocity * self.length_unit
290 } else {
291 Real::MAX
292 }
293 }
294
295 /// The maximal distance separating two objects that will generate predictive contacts
296 /// (default: `0.002m` multiped by [`Self::length_unit`]).
297 pub fn prediction_distance(&self) -> Real {
298 self.normalized_prediction_distance * self.length_unit
299 }
300}
301
302impl Default for IntegrationParameters {
303 fn default() -> Self {
304 Self {
305 dt: 1.0 / 60.0,
306 min_ccd_dt: 1.0 / 60.0 / 100.0,
307 contact_softness: SpringCoefficients::contact_defaults(),
308 warmstart_coefficient: 1.0,
309 num_internal_pgs_iterations: 1,
310 num_internal_stabilization_iterations: 1,
311 num_solver_iterations: 4,
312 // TODO: what is the optimal value for min_island_size?
313 // It should not be too big so that we don't end up with
314 // huge islands that don't fit in cache.
315 // However we don't want it to be too small and end up with
316 // tons of islands, reducing SIMD parallelism opportunities.
317 min_island_size: 128,
318 normalized_allowed_linear_error: 0.001,
319 normalized_max_corrective_velocity: 10.0,
320 normalized_prediction_distance: 0.002,
321 max_ccd_substeps: 1,
322 length_unit: 1.0,
323 #[cfg(feature = "dim3")]
324 friction_model: FrictionModel::default(),
325 }
326 }
327}