rapier3d/dynamics/integration_parameters.rs
1use crate::math::Real;
2use na::RealField;
3use std::num::NonZeroUsize;
4
5#[cfg(doc)]
6use super::RigidBodyActivation;
7
8// TODO: enabling the block solver in 3d introduces a lot of jitters in
9// the 3D domino demo. So for now we dont enable it in 3D.
10pub(crate) static BLOCK_SOLVER_ENABLED: bool = cfg!(feature = "dim2");
11
12/// Parameters for a time-step of the physics engine.
13#[derive(Copy, Clone, Debug, PartialEq)]
14#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
15pub struct IntegrationParameters {
16 /// The timestep length (default: `1.0 / 60.0`).
17 pub dt: Real,
18 /// Minimum timestep size when using CCD with multiple substeps (default: `1.0 / 60.0 / 100.0`).
19 ///
20 /// When CCD with multiple substeps is enabled, the timestep is subdivided
21 /// into smaller pieces. This timestep subdivision won't generate timestep
22 /// lengths smaller than `min_ccd_dt`.
23 ///
24 /// Setting this to a large value will reduce the opportunity to performing
25 /// CCD substepping, resulting in potentially more time dropped by the
26 /// motion-clamping mechanism. Setting this to an very small value may lead
27 /// to numerical instabilities.
28 pub min_ccd_dt: Real,
29
30 /// > 0: the damping ratio used by the springs for contact constraint stabilization.
31 ///
32 /// Larger values make the constraints more compliant (allowing more visible
33 /// penetrations before stabilization).
34 /// (default `5.0`).
35 pub contact_damping_ratio: Real,
36
37 /// > 0: the natural frequency used by the springs for contact constraint regularization.
38 ///
39 /// Increasing this value will make it so that penetrations get fixed more quickly at the
40 /// expense of potential jitter effects due to overshooting. In order to make the simulation
41 /// look stiffer, it is recommended to increase the [`Self::contact_damping_ratio`] instead of this
42 /// value.
43 /// (default: `30.0`).
44 pub contact_natural_frequency: Real,
45
46 /// > 0: the natural frequency used by the springs for joint constraint regularization.
47 ///
48 /// Increasing this value will make it so that penetrations get fixed more quickly.
49 /// (default: `1.0e6`).
50 pub joint_natural_frequency: Real,
51
52 /// The fraction of critical damping applied to the joint for constraints regularization.
53 ///
54 /// Larger values make the constraints more compliant (allowing more joint
55 /// drift before stabilization).
56 /// (default `1.0`).
57 pub joint_damping_ratio: Real,
58
59 /// The coefficient in `[0, 1]` applied to warmstart impulses, i.e., impulses that are used as the
60 /// initial solution (instead of 0) at the next simulation step.
61 ///
62 /// This should generally be set to 1.
63 ///
64 /// (default `1.0`).
65 pub warmstart_coefficient: Real,
66
67 /// The approximate size of most dynamic objects in the scene.
68 ///
69 /// This value is used internally to estimate some length-based tolerance. In particular, the
70 /// values [`IntegrationParameters::allowed_linear_error`],
71 /// [`IntegrationParameters::max_corrective_velocity`],
72 /// [`IntegrationParameters::prediction_distance`], [`RigidBodyActivation::normalized_linear_threshold`]
73 /// are scaled by this value implicitly.
74 ///
75 /// This value can be understood as the number of units-per-meter in your physical world compared
76 /// to a human-sized world in meter. For example, in a 2d game, if your typical object size is 100
77 /// pixels, set the [`Self::length_unit`] parameter to 100.0. The physics engine will interpret
78 /// it as if 100 pixels is equivalent to 1 meter in its various internal threshold.
79 /// (default `1.0`).
80 pub length_unit: Real,
81
82 /// Amount of penetration the engine won’t attempt to correct (default: `0.001m`).
83 ///
84 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
85 pub normalized_allowed_linear_error: Real,
86 /// Maximum amount of penetration the solver will attempt to resolve in one timestep (default: `10.0`).
87 ///
88 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
89 pub normalized_max_corrective_velocity: Real,
90 /// The maximal distance separating two objects that will generate predictive contacts (default: `0.002m`).
91 ///
92 /// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
93 pub normalized_prediction_distance: Real,
94 /// The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
95 pub num_solver_iterations: NonZeroUsize,
96 /// Number of addition friction resolution iteration run during the last solver sub-step (default: `0`).
97 pub num_additional_friction_iterations: usize,
98 /// Number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration (default: `1`).
99 pub num_internal_pgs_iterations: usize,
100 /// The number of stabilization iterations run at each solver iterations (default: `2`).
101 pub num_internal_stabilization_iterations: usize,
102 /// Minimum number of dynamic bodies in each active island (default: `128`).
103 pub min_island_size: usize,
104 /// Maximum number of substeps performed by the solver (default: `1`).
105 pub max_ccd_substeps: usize,
106}
107
108impl IntegrationParameters {
109 /// The inverse of the time-stepping length, i.e. the steps per seconds (Hz).
110 ///
111 /// This is zero if `self.dt` is zero.
112 #[inline(always)]
113 pub fn inv_dt(&self) -> Real {
114 if self.dt == 0.0 { 0.0 } else { 1.0 / self.dt }
115 }
116
117 /// Sets the time-stepping length.
118 #[inline]
119 #[deprecated = "You can just set the `IntegrationParams::dt` value directly"]
120 pub fn set_dt(&mut self, dt: Real) {
121 assert!(dt >= 0.0, "The time-stepping length cannot be negative.");
122 self.dt = dt;
123 }
124
125 /// Sets the inverse time-stepping length (i.e. the frequency).
126 ///
127 /// This automatically recompute `self.dt`.
128 #[inline]
129 pub fn set_inv_dt(&mut self, inv_dt: Real) {
130 if inv_dt == 0.0 {
131 self.dt = 0.0
132 } else {
133 self.dt = 1.0 / inv_dt
134 }
135 }
136
137 /// The contact’s spring angular frequency for constraints regularization.
138 pub fn contact_angular_frequency(&self) -> Real {
139 self.contact_natural_frequency * Real::two_pi()
140 }
141
142 /// The [`Self::contact_erp`] coefficient, multiplied by the inverse timestep length.
143 pub fn contact_erp_inv_dt(&self) -> Real {
144 let ang_freq = self.contact_angular_frequency();
145 ang_freq / (self.dt * ang_freq + 2.0 * self.contact_damping_ratio)
146 }
147
148 /// The effective Error Reduction Parameter applied for calculating regularization forces
149 /// on contacts.
150 ///
151 /// This parameter is computed automatically from [`Self::contact_natural_frequency`],
152 /// [`Self::contact_damping_ratio`] and the substep length.
153 pub fn contact_erp(&self) -> Real {
154 self.dt * self.contact_erp_inv_dt()
155 }
156
157 /// The joint’s spring angular frequency for constraint regularization.
158 pub fn joint_angular_frequency(&self) -> Real {
159 self.joint_natural_frequency * Real::two_pi()
160 }
161
162 /// The [`Self::joint_erp`] coefficient, multiplied by the inverse timestep length.
163 pub fn joint_erp_inv_dt(&self) -> Real {
164 let ang_freq = self.joint_angular_frequency();
165 ang_freq / (self.dt * ang_freq + 2.0 * self.joint_damping_ratio)
166 }
167
168 /// The effective Error Reduction Parameter applied for calculating regularization forces
169 /// on joints.
170 ///
171 /// This parameter is computed automatically from [`Self::joint_natural_frequency`],
172 /// [`Self::joint_damping_ratio`] and the substep length.
173 pub fn joint_erp(&self) -> Real {
174 self.dt * self.joint_erp_inv_dt()
175 }
176
177 /// The CFM factor to be used in the constraint resolution.
178 ///
179 /// This parameter is computed automatically from [`Self::contact_natural_frequency`],
180 /// [`Self::contact_damping_ratio`] and the substep length.
181 pub fn contact_cfm_factor(&self) -> Real {
182 // Compute CFM assuming a critically damped spring multiplied by the damping ratio.
183 // The logic is similar to [`Self::joint_cfm_coeff`].
184 let contact_erp = self.contact_erp();
185 if contact_erp == 0.0 {
186 return 0.0;
187 }
188 let inv_erp_minus_one = 1.0 / contact_erp - 1.0;
189
190 // let stiffness = 4.0 * damping_ratio * damping_ratio * projected_mass
191 // / (dt * dt * inv_erp_minus_one * inv_erp_minus_one);
192 // let damping = 4.0 * damping_ratio * damping_ratio * projected_mass
193 // / (dt * inv_erp_minus_one);
194 // let cfm = 1.0 / (dt * dt * stiffness + dt * damping);
195 // NOTE: This simplifies to cfm = cfm_coeff / projected_mass:
196 let cfm_coeff = inv_erp_minus_one * inv_erp_minus_one
197 / ((1.0 + inv_erp_minus_one)
198 * 4.0
199 * self.contact_damping_ratio
200 * self.contact_damping_ratio);
201
202 // Furthermore, we use this coefficient inside of the impulse resolution.
203 // Surprisingly, several simplifications happen there.
204 // Let `m` the projected mass of the constraint.
205 // Let `m’` the projected mass that includes CFM: `m’ = 1 / (1 / m + cfm_coeff / m) = m / (1 + cfm_coeff)`
206 // We have:
207 // new_impulse = old_impulse - m’ (delta_vel - cfm * old_impulse)
208 // = old_impulse - m / (1 + cfm_coeff) * (delta_vel - cfm_coeff / m * old_impulse)
209 // = old_impulse * (1 - cfm_coeff / (1 + cfm_coeff)) - m / (1 + cfm_coeff) * delta_vel
210 // = old_impulse / (1 + cfm_coeff) - m * delta_vel / (1 + cfm_coeff)
211 // = 1 / (1 + cfm_coeff) * (old_impulse - m * delta_vel)
212 // So, setting cfm_factor = 1 / (1 + cfm_coeff).
213 // We obtain:
214 // new_impulse = cfm_factor * (old_impulse - m * delta_vel)
215 //
216 // The value returned by this function is this cfm_factor that can be used directly
217 // in the constraint solver.
218 1.0 / (1.0 + cfm_coeff)
219 }
220
221 /// The CFM (constraints force mixing) coefficient applied to all joints for constraints regularization.
222 ///
223 /// This parameter is computed automatically from [`Self::joint_natural_frequency`],
224 /// [`Self::joint_damping_ratio`] and the substep length.
225 pub fn joint_cfm_coeff(&self) -> Real {
226 // Compute CFM assuming a critically damped spring multiplied by the damping ratio.
227 // The logic is similar to `Self::contact_cfm_factor`.
228 let joint_erp = self.joint_erp();
229 if joint_erp == 0.0 {
230 return 0.0;
231 }
232 let inv_erp_minus_one = 1.0 / joint_erp - 1.0;
233 inv_erp_minus_one * inv_erp_minus_one
234 / ((1.0 + inv_erp_minus_one)
235 * 4.0
236 * self.joint_damping_ratio
237 * self.joint_damping_ratio)
238 }
239
240 /// Amount of penetration the engine won’t attempt to correct (default: `0.001` multiplied by
241 /// [`Self::length_unit`]).
242 pub fn allowed_linear_error(&self) -> Real {
243 self.normalized_allowed_linear_error * self.length_unit
244 }
245
246 /// Maximum amount of penetration the solver will attempt to resolve in one timestep.
247 ///
248 /// This is equal to [`Self::normalized_max_corrective_velocity`] multiplied by
249 /// [`Self::length_unit`].
250 pub fn max_corrective_velocity(&self) -> Real {
251 if self.normalized_max_corrective_velocity != Real::MAX {
252 self.normalized_max_corrective_velocity * self.length_unit
253 } else {
254 Real::MAX
255 }
256 }
257
258 /// The maximal distance separating two objects that will generate predictive contacts
259 /// (default: `0.002m` multiped by [`Self::length_unit`]).
260 pub fn prediction_distance(&self) -> Real {
261 self.normalized_prediction_distance * self.length_unit
262 }
263
264 /// Initialize the simulation parameters with settings matching the TGS-soft solver
265 /// with warmstarting.
266 ///
267 /// This is the default configuration, equivalent to [`IntegrationParameters::default()`].
268 pub fn tgs_soft() -> Self {
269 Self {
270 dt: 1.0 / 60.0,
271 min_ccd_dt: 1.0 / 60.0 / 100.0,
272 contact_natural_frequency: 30.0,
273 contact_damping_ratio: 5.0,
274 joint_natural_frequency: 1.0e6,
275 joint_damping_ratio: 1.0,
276 warmstart_coefficient: 1.0,
277 num_internal_pgs_iterations: 1,
278 num_internal_stabilization_iterations: 2,
279 num_additional_friction_iterations: 0,
280 num_solver_iterations: NonZeroUsize::new(4).unwrap(),
281 // TODO: what is the optimal value for min_island_size?
282 // It should not be too big so that we don't end up with
283 // huge islands that don't fit in cache.
284 // However we don't want it to be too small and end up with
285 // tons of islands, reducing SIMD parallelism opportunities.
286 min_island_size: 128,
287 normalized_allowed_linear_error: 0.001,
288 normalized_max_corrective_velocity: 10.0,
289 normalized_prediction_distance: 0.002,
290 max_ccd_substeps: 1,
291 length_unit: 1.0,
292 }
293 }
294
295 /// Initialize the simulation parameters with settings matching the TGS-soft solver
296 /// **without** warmstarting.
297 ///
298 /// The [`IntegrationParameters::tgs_soft()`] configuration should be preferred unless
299 /// warmstarting proves to be undesirable for your use-case.
300 pub fn tgs_soft_without_warmstart() -> Self {
301 Self {
302 contact_damping_ratio: 0.25,
303 warmstart_coefficient: 0.0,
304 num_additional_friction_iterations: 4,
305 ..Self::tgs_soft()
306 }
307 }
308
309 /// Initializes the integration parameters to match the legacy PGS solver from Rapier version <= 0.17.
310 ///
311 /// This exists mainly for testing and comparison purpose.
312 pub fn pgs_legacy() -> Self {
313 Self {
314 num_solver_iterations: NonZeroUsize::new(1).unwrap(),
315 num_internal_pgs_iterations: 4,
316 ..Self::tgs_soft_without_warmstart()
317 }
318 }
319}
320
321impl Default for IntegrationParameters {
322 fn default() -> Self {
323 Self::tgs_soft()
324 }
325}