From ac4fb0351b182575f914e9c226153a282422b86b Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 10 Dec 2024 09:17:52 +0100 Subject: [PATCH] Split RapierContext (#585) --- CHANGELOG.md | 16 +- bevy_rapier2d/examples/debugdump2.rs | 2 +- bevy_rapier2d/examples/testbed2.rs | 9 +- bevy_rapier3d/examples/joints3.rs | 4 +- .../{multi_world3.rs => multi_contexts3.rs} | 24 +- .../examples/rapier_context_component.rs | 62 + bevy_rapier3d/examples/ray_casting3.rs | 6 +- bevy_rapier_benches3d/src/lib.rs | 4 +- src/control/character_controller.rs | 6 +- src/dynamics/revolute_joint.rs | 17 +- src/geometry/shape_views/ball.rs | 2 +- src/geometry/shape_views/capsule.rs | 2 +- src/geometry/shape_views/collider_view.rs | 2 +- src/geometry/shape_views/compound.rs | 2 +- src/geometry/shape_views/cone.rs | 2 +- src/geometry/shape_views/convex_polygon.rs | 4 +- src/geometry/shape_views/convex_polyhedron.rs | 4 +- src/geometry/shape_views/cuboid.rs | 2 +- src/geometry/shape_views/cylinder.rs | 2 +- src/geometry/shape_views/halfspace.rs | 2 +- src/geometry/shape_views/heightfield.rs | 4 +- src/geometry/shape_views/polyline.rs | 2 +- src/geometry/shape_views/segment.rs | 2 +- src/geometry/shape_views/triangle.rs | 2 +- src/geometry/shape_views/trimesh.rs | 2 +- src/lib.rs | 2 + src/pipeline/events.rs | 4 +- src/pipeline/physics_hooks.rs | 4 +- src/pipeline/query_filter.rs | 8 +- src/plugin/configuration.rs | 9 +- src/plugin/context/mod.rs | 1649 +++++++++-------- src/plugin/context/systemparams/mod.rs | 9 + .../rapier_context_systemparam.rs | 737 +++++++- src/plugin/mod.rs | 13 +- src/plugin/narrow_phase.rs | 117 +- src/plugin/plugin.rs | 122 +- src/plugin/systems/character_controller.rs | 46 +- src/plugin/systems/collider.rs | 166 +- src/plugin/systems/joint.rs | 37 +- src/plugin/systems/mod.rs | 59 +- .../systems/multiple_rapier_contexts.rs | 36 +- src/plugin/systems/remove.rs | 146 +- src/plugin/systems/rigid_body.rs | 160 +- src/plugin/systems/writeback.rs | 13 +- src/render/mod.rs | 51 +- 45 files changed, 2262 insertions(+), 1312 deletions(-) rename bevy_rapier3d/examples/{multi_world3.rs => multi_contexts3.rs} (80%) create mode 100644 bevy_rapier3d/examples/rapier_context_component.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index b8640af85..d5fd12acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,27 @@ ### Modified +- `RapierContext` has been split in multiple `Component`s: + - `RapierContextColliders` + - `RapierContextJoints` + - `RapierContextSimulation` + - `RapierRigidBodySet` +- Renamed `DefaultReadRapierContext` and `DefaultWriteRapierContext`. + - Use `ReadRapierContext` and its associated `single()` method. + +## v0.28.0 (09 December 2024) + +### Modified + - Update from rapier `0.21` to rapier `0.22`, see [rapier's changelog](https://github.com/dimforge/rapier/blob/master/CHANGELOG.md). - Update bevy to 0.15. -- `RapierContext`, `RapierConfiguration` and `RenderToSimulationTime` are now a `Component` instead of resources. +- `RapierContext`, `RapierConfiguration` and `SimulationToRenderTime` are now a `Component` instead of resources. - Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details. - Migration guide: - `ResMut` -> `WriteDefaultRapierContext` - `Res` -> `ReadDefaultRapierContext` - - Access to `RapierConfiguration` and `RenderToSimulationTime` should query for it + - Access to `RapierConfiguration` and `SimulationToRenderTime` should query for it on the responsible entity owning the `RenderContext`. - If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too, you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545) diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs index 190a5079b..4d133d918 100644 --- a/bevy_rapier2d/examples/debugdump2.rs +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -1,6 +1,6 @@ //! Example using bevy_mod_debugdump to output a graph of systems execution order. //! run with: -//! `cargo run --example debugdump2 > dump.dot && dot -Tsvg dump.dot > dump.svg` +//! `cargo run --example debugdump2 > dump.dot && dot -Tsvg dump.dot > dump.svg` use bevy::prelude::*; use bevy_mod_debugdump::{schedule_graph, schedule_graph_dot}; diff --git a/bevy_rapier2d/examples/testbed2.rs b/bevy_rapier2d/examples/testbed2.rs index cbb3cf03e..c1bcc9d00 100644 --- a/bevy_rapier2d/examples/testbed2.rs +++ b/bevy_rapier2d/examples/testbed2.rs @@ -205,11 +205,12 @@ fn main() { OnExit(Examples::PlayerMovement2), ( cleanup, - |mut rapier_config: Query<&mut RapierConfiguration>, - ctxt: ReadDefaultRapierContext| { + |mut rapier_config: Query<&mut RapierConfiguration>, ctxt: ReadRapierContext| { let mut rapier_config = rapier_config.single_mut(); - rapier_config.gravity = - RapierConfiguration::new(ctxt.integration_parameters.length_unit).gravity; + rapier_config.gravity = RapierConfiguration::new( + ctxt.single().simulation.integration_parameters.length_unit, + ) + .gravity; }, ), ) diff --git a/bevy_rapier3d/examples/joints3.rs b/bevy_rapier3d/examples/joints3.rs index 3edecc74e..3ded4ade7 100644 --- a/bevy_rapier3d/examples/joints3.rs +++ b/bevy_rapier3d/examples/joints3.rs @@ -279,7 +279,7 @@ pub fn setup_physics(mut commands: Commands) { } pub fn print_impulse_revolute_joints( - context: ReadDefaultRapierContext, + context: ReadRapierContext, joints: Query<(Entity, &ImpulseJoint)>, ) { for (entity, impulse_joint) in joints.iter() { @@ -288,7 +288,7 @@ pub fn print_impulse_revolute_joints( println!( "angle for {}: {:?}", entity, - context.impulse_revolute_joint_angle(entity), + context.single().impulse_revolute_joint_angle(entity), ); } _ => {} diff --git a/bevy_rapier3d/examples/multi_world3.rs b/bevy_rapier3d/examples/multi_contexts3.rs similarity index 80% rename from bevy_rapier3d/examples/multi_world3.rs rename to bevy_rapier3d/examples/multi_contexts3.rs index 54a0ad939..8e74acbb1 100644 --- a/bevy_rapier3d/examples/multi_world3.rs +++ b/bevy_rapier3d/examples/multi_contexts3.rs @@ -1,7 +1,7 @@ use bevy::{input::common_conditions::input_just_pressed, prelude::*}; use bevy_rapier3d::prelude::*; -const N_WORLDS: usize = 2; +const N_CONTEXTS: usize = 2; fn main() { App::new() @@ -18,21 +18,21 @@ fn main() { )) .add_systems( Startup, - ((create_worlds, setup_physics).chain(), setup_graphics), + ((create_contexts, setup_physics).chain(), setup_graphics), ) .add_systems(Update, move_platforms) .add_systems( Update, - change_world.run_if(input_just_pressed(KeyCode::KeyC)), + change_context.run_if(input_just_pressed(KeyCode::KeyC)), ) .run(); } -fn create_worlds(mut commands: Commands) { - for i in 0..N_WORLDS { - let mut world = commands.spawn((RapierContext::default(), WorldId(i))); +fn create_contexts(mut commands: Commands) { + for i in 0..N_CONTEXTS { + let mut context = commands.spawn((RapierContextSimulation::default(), ContextId(i))); if i == 0 { - world.insert(DefaultRapierContext); + context.insert((DefaultRapierContext, RapierContextSimulation::default())); } } } @@ -45,7 +45,7 @@ fn setup_graphics(mut commands: Commands) { } #[derive(Component)] -pub struct WorldId(pub usize); +pub struct ContextId(pub usize); #[derive(Component)] struct Platform { @@ -58,8 +58,8 @@ fn move_platforms(time: Res