Skip to content

Commit 11e8d08

Browse files
Simplify custom default rapier context + update rapier to 0.24 (#639)
1 parent ff08dd5 commit 11e8d08

File tree

7 files changed

+150
-16
lines changed

7 files changed

+150
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
- Added a serialization `serialization2` example for `bevy_rapier2d`.
88

9+
## Modified
10+
11+
- Update from rapier `0.23` to rapier `0.24`,
12+
see [rapier's changelog](https://github.com/dimforge/rapier/blob/master/CHANGELOG.md).
13+
- `RapierContextInitialization::InitializeDefaultRapierContext` now has more fields for better control over default physics context.
14+
915
## Fixed
1016

1117
- Fix position being incorrect when a rigidbody bevy entity has a scaled parent. [#646](https://github.com/dimforge/bevy_rapier/pull/646)

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ codegen-units = 1
1313
#nalgebra = { path = "../nalgebra" }
1414
#parry2d = { path = "../parry/crates/parry2d" }
1515
#parry3d = { path = "../parry/crates/parry3d" }
16-
#rapier2d = { path = "../rapier/crates/rapier2d" }
17-
#rapier3d = { path = "../rapier/crates/rapier3d" }
16+
# rapier2d = { path = "../rapier/crates/rapier2d" }
17+
# rapier3d = { path = "../rapier/crates/rapier3d" }
1818
#nalgebra = { git = "https://github.com/dimforge/nalgebra", branch = "dev" }
1919
#parry2d = { git = "https://github.com/dimforge/parry", branch = "master" }
2020
#parry3d = { git = "https://github.com/dimforge/parry", branch = "master" }

bevy_rapier2d/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ to-bevy-mesh = ["bevy/bevy_render", "bevy/bevy_asset"]
6666
[dependencies]
6767
bevy = { version = "0.15", default-features = false }
6868
nalgebra = { version = "0.33", features = ["convert-glam029"] }
69-
rapier2d = "0.23"
69+
rapier2d = "0.24"
7070
bitflags = "2.4"
7171
log = "0.4"
7272
serde = { version = "1", features = ["derive"], optional = true }

bevy_rapier3d/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ to-bevy-mesh = ["bevy/bevy_render", "bevy/bevy_asset"]
6767
[dependencies]
6868
bevy = { version = "0.15", default-features = false }
6969
nalgebra = { version = "0.33", features = ["convert-glam029"] }
70-
rapier3d = "0.23"
70+
rapier3d = "0.24"
7171
bitflags = "2.4"
7272
log = "0.4"
7373
serde = { version = "1", features = ["derive"], optional = true }

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: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::pipeline::{CollisionEvent, ContactForceEvent};
22
use crate::prelude::*;
3+
use crate::reflect::IntegrationParametersWrapper;
34
use bevy::ecs::{
45
intern::Interned,
56
schedule::{ScheduleLabel, SystemConfigs},
@@ -53,7 +54,7 @@ where
5354
/// conversion ratio.
5455
pub fn with_length_unit(mut self, length_unit: f32) -> Self {
5556
self.default_world_setup =
56-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit };
57+
RapierContextInitialization::default_with_length_unit(length_unit);
5758
self
5859
}
5960

@@ -84,9 +85,9 @@ where
8485
pub fn pixels_per_meter(pixels_per_meter: f32) -> Self {
8586
Self {
8687
default_system_setup: true,
87-
default_world_setup: RapierContextInitialization::InitializeDefaultRapierContext {
88-
length_unit: pixels_per_meter,
89-
},
88+
default_world_setup: RapierContextInitialization::default_with_length_unit(
89+
pixels_per_meter,
90+
),
9091
..default()
9192
}
9293
}
@@ -370,14 +371,37 @@ pub enum RapierContextInitialization {
370371
/// [`RapierPhysicsPlugin`] will spawn an entity containing a [`RapierContextSimulation`]
371372
/// automatically during [`PreStartup`], with the [`DefaultRapierContext`] marker component.
372373
InitializeDefaultRapierContext {
373-
/// See [`IntegrationParameters::length_unit`]
374-
length_unit: f32,
374+
/// Integration parameters component which will be added to the default rapier context.
375+
#[reflect(remote = IntegrationParametersWrapper)]
376+
integration_parameters: IntegrationParameters,
377+
/// Rapier configuration component which will be added to the default rapier context.
378+
rapier_configuration: RapierConfiguration,
375379
},
376380
}
377381

378382
impl Default for RapierContextInitialization {
379383
fn default() -> Self {
380-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit: 1f32 }
384+
Self::default_with_length_unit(1f32)
385+
}
386+
}
387+
388+
impl RapierContextInitialization {
389+
/// Configures rapier with the specified length unit.
390+
///
391+
/// See the documentation of [`IntegrationParameters::length_unit`] for additional details
392+
/// on that argument.
393+
///
394+
/// The default gravity is automatically scaled by that length unit.
395+
pub fn default_with_length_unit(length_unit: f32) -> Self {
396+
let integration_parameters = IntegrationParameters {
397+
length_unit,
398+
..default()
399+
};
400+
401+
RapierContextInitialization::InitializeDefaultRapierContext {
402+
integration_parameters,
403+
rapier_configuration: RapierConfiguration::new(length_unit),
404+
}
381405
}
382406
}
383407

@@ -387,16 +411,17 @@ pub fn insert_default_context(
387411
) {
388412
match initialization_data.as_ref() {
389413
RapierContextInitialization::NoAutomaticRapierContext => {}
390-
RapierContextInitialization::InitializeDefaultRapierContext { length_unit } => {
414+
RapierContextInitialization::InitializeDefaultRapierContext {
415+
integration_parameters,
416+
rapier_configuration,
417+
} => {
391418
commands.spawn((
392419
Name::new("Rapier Context"),
393420
RapierContextSimulation {
394-
integration_parameters: IntegrationParameters {
395-
length_unit: *length_unit,
396-
..default()
397-
},
421+
integration_parameters: *integration_parameters,
398422
..RapierContextSimulation::default()
399423
},
424+
*rapier_configuration,
400425
DefaultRapierContext,
401426
));
402427
}

src/reflect/mod.rs

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

0 commit comments

Comments
 (0)