Skip to content

Commit efb71e5

Browse files
committed
Revert "Rapier context as a Component (dimforge#545)"
This reverts commit bab3431.
1 parent 8d10abc commit efb71e5

25 files changed

+418
-1558
lines changed

CHANGELOG.md

-13
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ which was its hardcoded behaviour.
2121
`RapierDebugColliderPlugin` and `DebugRenderContext`, as well as individual collider setup via
2222
a `ColliderDebug` component.
2323

24-
### Modified
25-
26-
- `RapierContext`, `RapierConfiguration` and `RenderToSimulationTime` are now a `Component` instead of resources.
27-
- Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details.
28-
- Migration guide:
29-
- `ResMut<mut RapierContext>` -> `WriteDefaultRapierContext`
30-
- `Res<RapierContext>` -> `ReadDefaultRapierContext`
31-
- Access to `RapierConfiguration` and `RenderToSimulationTime` should query for it
32-
on the responsible entity owning the `RenderContext`.
33-
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too,
34-
you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545)
35-
to get more context and information.
36-
3724
## v0.27.0 (07 July 2024)
3825

3926
**This is an update from rapier 0.19 to Rapier 0.21 which includes several stability improvements

bevy_rapier2d/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ serde = { version = "1", features = ["derive"], optional = true }
6060
bevy = { version = "0.14", default-features = false, features = [
6161
"x11",
6262
"bevy_state",
63-
"bevy_debug_stepping",
6463
] }
6564
oorandom = "11"
6665
approx = "0.5.1"

bevy_rapier2d/examples/player_movement2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ fn main() {
2424
#[derive(Component)]
2525
pub struct Player(f32);
2626

27-
pub fn spawn_player(mut commands: Commands, mut rapier_config: Query<&mut RapierConfiguration>) {
28-
let mut rapier_config = rapier_config.single_mut();
27+
pub fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
2928
// Set gravity to 0.0 and spawn camera.
3029
rapier_config.gravity = Vec2::ZERO;
3130
commands.spawn(Camera2dBundle::default());

bevy_rapier2d/examples/testbed2.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ fn main() {
204204
OnExit(Examples::PlayerMovement2),
205205
(
206206
cleanup,
207-
|mut rapier_config: Query<&mut RapierConfiguration>,
208-
ctxt: ReadDefaultRapierContext| {
209-
let mut rapier_config = rapier_config.single_mut();
207+
|mut rapier_config: ResMut<RapierConfiguration>, ctxt: Res<RapierContext>| {
210208
rapier_config.gravity =
211209
RapierConfiguration::new(ctxt.integration_parameters.length_unit).gravity;
212210
},

bevy_rapier3d/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ bevy = { version = "0.14", default-features = false, features = [
6262
"x11",
6363
"tonemapping_luts",
6464
"bevy_state",
65-
"bevy_debug_stepping",
6665
] }
6766
approx = "0.5.1"
6867
glam = { version = "0.27", features = ["approx"] }

bevy_rapier3d/examples/joints3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub fn setup_physics(mut commands: Commands) {
284284
}
285285

286286
pub fn print_impulse_revolute_joints(
287-
context: ReadDefaultRapierContext,
287+
context: Res<RapierContext>,
288288
joints: Query<(Entity, &ImpulseJoint)>,
289289
) {
290290
for (entity, impulse_joint) in joints.iter() {

bevy_rapier3d/examples/multi_world3.rs

-120
This file was deleted.

bevy_rapier3d/examples/ray_casting3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn setup_physics(mut commands: Commands) {
7676
pub fn cast_ray(
7777
mut commands: Commands,
7878
windows: Query<&Window, With<PrimaryWindow>>,
79-
rapier_context: ReadDefaultRapierContext,
79+
rapier_context: Res<RapierContext>,
8080
cameras: Query<(&Camera, &GlobalTransform)>,
8181
) {
8282
let window = windows.single();

bevy_rapier_benches3d/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) {
2626
app.update();
2727
timer_full_update.pause();
2828
let elapsed_time = timer_full_update.time() as f32;
29-
let rc = app
30-
.world_mut()
31-
.query::<&RapierContext>()
32-
.single(app.world());
29+
let rc = app.world().resource::<RapierContext>();
3330
rapier_step_times.push(rc.pipeline.counters.step_time.time() as f32);
3431
total_update_times.push(elapsed_time);
3532
}

src/pipeline/events.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::math::{Real, Vect};
2-
use bevy::prelude::{Entity, Event};
2+
use bevy::prelude::{Entity, Event, EventWriter};
33
use rapier::dynamics::RigidBodySet;
44
use rapier::geometry::{
55
ColliderHandle, ColliderSet, CollisionEvent as RapierCollisionEvent, CollisionEventFlags,
@@ -58,8 +58,8 @@ pub(crate) struct EventQueue<'a> {
5858
// Used to retrieve the entity of colliders that have been removed from the simulation
5959
// since the last physics step.
6060
pub deleted_colliders: &'a HashMap<ColliderHandle, Entity>,
61-
pub collision_events: RwLock<Vec<CollisionEvent>>,
62-
pub contact_force_events: RwLock<Vec<ContactForceEvent>>,
61+
pub collision_events: RwLock<EventWriter<'a, CollisionEvent>>,
62+
pub contact_force_events: RwLock<EventWriter<'a, ContactForceEvent>>,
6363
}
6464

6565
impl<'a> EventQueue<'a> {
@@ -94,7 +94,7 @@ impl<'a> EventHandler for EventQueue<'a> {
9494
};
9595

9696
if let Ok(mut events) = self.collision_events.write() {
97-
events.push(event);
97+
events.send(event);
9898
}
9999
}
100100

@@ -118,7 +118,7 @@ impl<'a> EventHandler for EventQueue<'a> {
118118
};
119119

120120
if let Ok(mut events) = self.contact_force_events.write() {
121-
events.push(event);
121+
events.send(event);
122122
}
123123
}
124124
}
@@ -155,13 +155,15 @@ mod test {
155155
pub struct EventsSaver<E: Event> {
156156
pub events: Vec<E>,
157157
}
158+
158159
impl<E: Event> Default for EventsSaver<E> {
159160
fn default() -> Self {
160161
Self {
161162
events: Default::default(),
162163
}
163164
}
164165
}
166+
165167
pub fn save_events<E: Event + Clone>(
166168
mut events: EventReader<E>,
167169
mut saver: ResMut<EventsSaver<E>>,
@@ -170,6 +172,7 @@ mod test {
170172
saver.events.push(event.clone());
171173
}
172174
}
175+
173176
fn run_test(app: &mut App) {
174177
app.add_systems(PostUpdate, save_events::<CollisionEvent>)
175178
.add_systems(PostUpdate, save_events::<ContactForceEvent>)
@@ -189,12 +192,12 @@ mod test {
189192
.world()
190193
.get_resource::<EventsSaver<CollisionEvent>>()
191194
.unwrap();
192-
assert!(saved_collisions.events.len() > 0);
195+
assert_eq!(saved_collisions.events.len(), 3);
193196
let saved_contact_forces = app
194197
.world()
195-
.get_resource::<EventsSaver<CollisionEvent>>()
198+
.get_resource::<EventsSaver<ContactForceEvent>>()
196199
.unwrap();
197-
assert!(saved_contact_forces.events.len() > 0);
200+
assert_eq!(saved_contact_forces.events.len(), 1);
198201
}
199202

200203
/// Adapted from events example
@@ -229,7 +232,7 @@ mod test {
229232
TransformBundle::from(Transform::from_xyz(0.0, 13.0, 0.0)),
230233
RigidBody::Dynamic,
231234
cuboid(0.5, 0.5, 0.5),
232-
ActiveEvents::COLLISION_EVENTS,
235+
ActiveEvents::COLLISION_EVENTS | ActiveEvents::CONTACT_FORCE_EVENTS,
233236
ContactForceEventThreshold(30.0),
234237
));
235238
}
@@ -244,10 +247,13 @@ mod test {
244247
TransformPlugin,
245248
RapierPhysicsPlugin::<NoUserData>::default(),
246249
))
247-
.insert_resource(TimestepMode::Interpolated {
248-
dt: 1.0 / 30.0,
249-
time_scale: 1.0,
250-
substeps: 2,
250+
.insert_resource(RapierConfiguration {
251+
timestep_mode: TimestepMode::Interpolated {
252+
dt: 1.0 / 30.0,
253+
time_scale: 1.0,
254+
substeps: 2,
255+
},
256+
..RapierConfiguration::new(1f32)
251257
})
252258
.add_systems(Startup, setup_physics)
253259
.add_systems(Update, remove_collider);

src/plugin/configuration.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
use bevy::{
2-
prelude::{Component, Resource},
3-
reflect::Reflect,
4-
};
1+
use bevy::prelude::{FromWorld, Resource, World};
52

63
use crate::math::{Real, Vect};
4+
use crate::plugin::RapierContext;
75

86
#[cfg(doc)]
97
use {crate::prelude::TransformInterpolation, rapier::dynamics::IntegrationParameters};
108

119
/// Difference between simulation and rendering time
12-
#[derive(Component, Default, Reflect)]
10+
#[derive(Resource, Default)]
1311
pub struct SimulationToRenderTime {
1412
/// Difference between simulation and rendering time
1513
pub diff: f32,
1614
}
1715

18-
/// The different ways of adjusting the timestep length each frame.
19-
#[derive(Copy, Clone, Debug, PartialEq, Resource)]
16+
/// The different ways of adjusting the timestep length.
17+
#[derive(Copy, Clone, Debug, PartialEq)]
2018
pub enum TimestepMode {
2119
/// Use a fixed timestep: the physics simulation will be advanced by the fixed value
2220
/// `dt` seconds at each Bevy tick by performing `substeps` of length `dt / substeps`.
@@ -55,25 +53,17 @@ pub enum TimestepMode {
5553
},
5654
}
5755

58-
impl Default for TimestepMode {
59-
fn default() -> Self {
60-
TimestepMode::Variable {
61-
max_dt: 1.0 / 60.0,
62-
time_scale: 1.0,
63-
substeps: 1,
64-
}
65-
}
66-
}
67-
68-
#[derive(Component, Copy, Clone, Debug, Reflect)]
69-
/// A component for specifying configuration information for the physics simulation
56+
#[derive(Resource, Copy, Clone, Debug)]
57+
/// A resource for specifying configuration information for the physics simulation
7058
pub struct RapierConfiguration {
7159
/// Specifying the gravity of the physics simulation.
7260
pub gravity: Vect,
7361
/// Specifies if the physics simulation is active and update the physics world.
7462
pub physics_pipeline_active: bool,
7563
/// Specifies if the query pipeline is active and update the query pipeline.
7664
pub query_pipeline_active: bool,
65+
/// Specifies the way the timestep length should be adjusted at each frame.
66+
pub timestep_mode: TimestepMode,
7767
/// Specifies the number of subdivisions along each axes a shape should be subdivided
7868
/// if its scaled representation cannot be represented with the same shape type.
7969
///
@@ -86,6 +76,16 @@ pub struct RapierConfiguration {
8676
pub force_update_from_transform_changes: bool,
8777
}
8878

79+
impl FromWorld for RapierConfiguration {
80+
fn from_world(world: &mut World) -> Self {
81+
let length_unit = world
82+
.get_resource::<RapierContext>()
83+
.map(|ctxt| ctxt.integration_parameters.length_unit)
84+
.unwrap_or(1.0);
85+
Self::new(length_unit)
86+
}
87+
}
88+
8989
impl RapierConfiguration {
9090
/// Configures rapier with the specified length unit.
9191
///
@@ -98,6 +98,11 @@ impl RapierConfiguration {
9898
gravity: Vect::Y * -9.81 * length_unit,
9999
physics_pipeline_active: true,
100100
query_pipeline_active: true,
101+
timestep_mode: TimestepMode::Variable {
102+
max_dt: 1.0 / 60.0,
103+
time_scale: 1.0,
104+
substeps: 1,
105+
},
101106
scaled_shape_subdivision: 10,
102107
force_update_from_transform_changes: false,
103108
}

0 commit comments

Comments
 (0)