Skip to content

Commit d151bc2

Browse files
committed
attempt to reflect integration parameters 🤔
1 parent 6b6e0c8 commit d151bc2

File tree

3 files changed

+123
-11
lines changed

3 files changed

+123
-11
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub mod pipeline;
5757
/// The physics plugin and systems.
5858
pub mod plugin;
5959

60+
/// Reflection utilities.
61+
pub mod reflect;
62+
6063
#[cfg(feature = "picking-backend")]
6164
pub mod picking_backend;
6265

src/plugin/plugin.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bevy::ecs::{
88
use bevy::utils::HashSet;
99
use bevy::{prelude::*, transform::TransformSystem};
1010
use rapier::dynamics::IntegrationParameters;
11+
use reflect::IntegrationParametersWrapper;
1112
use std::marker::PhantomData;
1213

1314
use super::context::DefaultRapierContext;
@@ -52,8 +53,12 @@ where
5253
/// likely always be 1.0 in 3D. In 2D, this is useful to specify a "pixels-per-meter"
5354
/// conversion ratio.
5455
pub fn with_length_unit(mut self, length_unit: f32) -> Self {
55-
self.default_world_setup =
56-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit };
56+
self.default_world_setup = RapierContextInitialization::InitializeDefaultRapierContext {
57+
integration_parameters: IntegrationParameters {
58+
length_unit,
59+
..default()
60+
},
61+
};
5762
self
5863
}
5964

@@ -85,7 +90,10 @@ where
8590
Self {
8691
default_system_setup: true,
8792
default_world_setup: RapierContextInitialization::InitializeDefaultRapierContext {
88-
length_unit: pixels_per_meter,
93+
integration_parameters: IntegrationParameters {
94+
length_unit: pixels_per_meter,
95+
..default()
96+
},
8997
},
9098
..default()
9199
}
@@ -370,14 +378,17 @@ pub enum RapierContextInitialization {
370378
/// [`RapierPhysicsPlugin`] will spawn an entity containing a [`RapierContextSimulation`]
371379
/// automatically during [`PreStartup`], with the [`DefaultRapierContext`] marker component.
372380
InitializeDefaultRapierContext {
373-
/// See [`IntegrationParameters::length_unit`]
374-
length_unit: f32,
381+
/// Integration
382+
#[reflect(remote = IntegrationParametersWrapper)]
383+
integration_parameters: IntegrationParameters,
375384
},
376385
}
377386

378387
impl Default for RapierContextInitialization {
379388
fn default() -> Self {
380-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit: 1f32 }
389+
RapierContextInitialization::InitializeDefaultRapierContext {
390+
integration_parameters: IntegrationParameters::default(),
391+
}
381392
}
382393
}
383394

@@ -387,14 +398,13 @@ pub fn insert_default_context(
387398
) {
388399
match initialization_data.as_ref() {
389400
RapierContextInitialization::NoAutomaticRapierContext => {}
390-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit } => {
401+
RapierContextInitialization::InitializeDefaultRapierContext {
402+
integration_parameters,
403+
} => {
391404
commands.spawn((
392405
Name::new("Rapier Context"),
393406
RapierContextSimulation {
394-
integration_parameters: IntegrationParameters {
395-
length_unit: *length_unit,
396-
..default()
397-
},
407+
integration_parameters: *integration_parameters,
398408
..RapierContextSimulation::default()
399409
},
400410
DefaultRapierContext,

src/reflect/mod.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use crate::math::Real;
2+
use bevy::reflect::reflect_remote;
3+
use rapier::dynamics::IntegrationParameters;
4+
5+
#[reflect_remote(IntegrationParameter)]
6+
#[derive(Copy, Clone, Debug, PartialEq)]
7+
/// Parameters affecting the physical behavior of a wheel.
8+
pub struct IntegrationParameterWrapper {
9+
/// The timestep length (default: `1.0 / 60.0`).
10+
pub dt: Real,
11+
/// Minimum timestep size when using CCD with multiple substeps (default: `1.0 / 60.0 / 100.0`).
12+
///
13+
/// When CCD with multiple substeps is enabled, the timestep is subdivided
14+
/// into smaller pieces. This timestep subdivision won't generate timestep
15+
/// lengths smaller than `min_ccd_dt`.
16+
///
17+
/// Setting this to a large value will reduce the opportunity to performing
18+
/// CCD substepping, resulting in potentially more time dropped by the
19+
/// motion-clamping mechanism. Setting this to an very small value may lead
20+
/// to numerical instabilities.
21+
pub min_ccd_dt: Real,
22+
23+
/// > 0: the damping ratio used by the springs for contact constraint stabilization.
24+
///
25+
/// Larger values make the constraints more compliant (allowing more visible
26+
/// penetrations before stabilization).
27+
/// (default `5.0`).
28+
pub contact_damping_ratio: Real,
29+
30+
/// > 0: the natural frequency used by the springs for contact constraint regularization.
31+
///
32+
/// Increasing this value will make it so that penetrations get fixed more quickly at the
33+
/// expense of potential jitter effects due to overshooting. In order to make the simulation
34+
/// look stiffer, it is recommended to increase the [`Self::contact_damping_ratio`] instead of this
35+
/// value.
36+
/// (default: `30.0`).
37+
pub contact_natural_frequency: Real,
38+
39+
/// > 0: the natural frequency used by the springs for joint constraint regularization.
40+
///
41+
/// Increasing this value will make it so that penetrations get fixed more quickly.
42+
/// (default: `1.0e6`).
43+
pub joint_natural_frequency: Real,
44+
45+
/// The fraction of critical damping applied to the joint for constraints regularization.
46+
///
47+
/// Larger values make the constraints more compliant (allowing more joint
48+
/// drift before stabilization).
49+
/// (default `1.0`).
50+
pub joint_damping_ratio: Real,
51+
52+
/// The coefficient in `[0, 1]` applied to warmstart impulses, i.e., impulses that are used as the
53+
/// initial solution (instead of 0) at the next simulation step.
54+
///
55+
/// This should generally be set to 1.
56+
///
57+
/// (default `1.0`).
58+
pub warmstart_coefficient: Real,
59+
60+
/// The approximate size of most dynamic objects in the scene.
61+
///
62+
/// This value is used internally to estimate some length-based tolerance. In particular, the
63+
/// values [`IntegrationParameters::allowed_linear_error`],
64+
/// [`IntegrationParameters::max_corrective_velocity`],
65+
/// [`IntegrationParameters::prediction_distance`], [`RigidBodyActivation::normalized_linear_threshold`]
66+
/// are scaled by this value implicitly.
67+
///
68+
/// This value can be understood as the number of units-per-meter in your physical world compared
69+
/// to a human-sized world in meter. For example, in a 2d game, if your typical object size is 100
70+
/// pixels, set the [`Self::length_unit`] parameter to 100.0. The physics engine will interpret
71+
/// it as if 100 pixels is equivalent to 1 meter in its various internal threshold.
72+
/// (default `1.0`).
73+
pub length_unit: Real,
74+
75+
/// Amount of penetration the engine won’t attempt to correct (default: `0.001m`).
76+
///
77+
/// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
78+
pub normalized_allowed_linear_error: Real,
79+
/// Maximum amount of penetration the solver will attempt to resolve in one timestep (default: `10.0`).
80+
///
81+
/// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
82+
pub normalized_max_corrective_velocity: Real,
83+
/// The maximal distance separating two objects that will generate predictive contacts (default: `0.002m`).
84+
///
85+
/// This value is implicitly scaled by [`IntegrationParameters::length_unit`].
86+
pub normalized_prediction_distance: Real,
87+
/// The number of solver iterations run by the constraints solver for calculating forces (default: `4`).
88+
pub num_solver_iterations: NonZeroUsize,
89+
/// Number of addition friction resolution iteration run during the last solver sub-step (default: `0`).
90+
pub num_additional_friction_iterations: usize,
91+
/// Number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration (default: `1`).
92+
pub num_internal_pgs_iterations: usize,
93+
/// The number of stabilization iterations run at each solver iterations (default: `2`).
94+
pub num_internal_stabilization_iterations: usize,
95+
/// Minimum number of dynamic bodies in each active island (default: `128`).
96+
pub min_island_size: usize,
97+
/// Maximum number of substeps performed by the solver (default: `1`).
98+
pub max_ccd_substeps: usize,
99+
}

0 commit comments

Comments
 (0)