diff --git a/CHANGELOG.md b/CHANGELOG.md index 42c72011..324f85e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,8 @@ which was its hardcoded behaviour. - `RapierContext`, `RapierConfiguration` and `RenderToSimulationTime` are now a `Component` instead of resources. - Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details. - Migration guide: - - `ResMut` -> `DefaultRapierContextAccessMut` - - `Res` -> `DefaultRapierContextAccess` + - `ResMut` -> `WriteDefaultRapierContext` + - `Res` -> `ReadDefaultRapierContext` - Access to `RapierConfiguration` and `RenderToSimulationTime` 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, diff --git a/bevy_rapier2d/examples/testbed2.rs b/bevy_rapier2d/examples/testbed2.rs index f0317c6b..8a433e22 100644 --- a/bevy_rapier2d/examples/testbed2.rs +++ b/bevy_rapier2d/examples/testbed2.rs @@ -205,7 +205,7 @@ fn main() { ( cleanup, |mut rapier_config: Query<&mut RapierConfiguration>, - ctxt: DefaultRapierContextAccess| { + ctxt: ReadDefaultRapierContext| { let mut rapier_config = rapier_config.single_mut(); rapier_config.gravity = RapierConfiguration::new(ctxt.integration_parameters.length_unit).gravity; diff --git a/bevy_rapier3d/examples/joints3.rs b/bevy_rapier3d/examples/joints3.rs index d49dd716..2864d33b 100644 --- a/bevy_rapier3d/examples/joints3.rs +++ b/bevy_rapier3d/examples/joints3.rs @@ -284,7 +284,7 @@ pub fn setup_physics(mut commands: Commands) { } pub fn print_impulse_revolute_joints( - context: DefaultRapierContextAccess, + context: ReadDefaultRapierContext, joints: Query<(Entity, &ImpulseJoint)>, ) { for (entity, impulse_joint) in joints.iter() { diff --git a/bevy_rapier3d/examples/ray_casting3.rs b/bevy_rapier3d/examples/ray_casting3.rs index 89d22377..d5e45d9e 100644 --- a/bevy_rapier3d/examples/ray_casting3.rs +++ b/bevy_rapier3d/examples/ray_casting3.rs @@ -76,7 +76,7 @@ pub fn setup_physics(mut commands: Commands) { pub fn cast_ray( mut commands: Commands, windows: Query<&Window, With>, - rapier_context: DefaultRapierContextAccess, + rapier_context: ReadDefaultRapierContext, cameras: Query<(&Camera, &GlobalTransform)>, ) { let window = windows.single(); diff --git a/src/plugin/context/mod.rs b/src/plugin/context/mod.rs index bf3541c7..f3c7ad13 100644 --- a/src/plugin/context/mod.rs +++ b/src/plugin/context/mod.rs @@ -29,7 +29,7 @@ use crate::prelude::{ImpulseJoint, MultibodyJoint, RevoluteJoint, TypedJoint}; /// Marker component for to access the default [`RapierContext`]. /// -/// This is used by [`systemparams::DefaultRapierContextAccess`] and other default accesses +/// This is used by [`systemparams::ReadDefaultRapierContext`] and other default accesses /// to help with getting a reference to the correct RapierContext. /// /// If you're making a library, you might be interested in [`RapierContextEntityLink`] diff --git a/src/plugin/context/systemparams/mod.rs b/src/plugin/context/systemparams/mod.rs index 910ad2cf..93a630e6 100644 --- a/src/plugin/context/systemparams/mod.rs +++ b/src/plugin/context/systemparams/mod.rs @@ -1,7 +1,7 @@ -mod rapier_context_access; +mod rapier_context_systemparam; use bevy::{ecs::query::QueryData, prelude::Entity}; -pub use rapier_context_access::*; +pub use rapier_context_systemparam::*; use super::RapierContextEntityLink; diff --git a/src/plugin/context/systemparams/rapier_context_access.rs b/src/plugin/context/systemparams/rapier_context_systemparam.rs similarity index 90% rename from src/plugin/context/systemparams/rapier_context_access.rs rename to src/plugin/context/systemparams/rapier_context_systemparam.rs index 501dceb4..0bc5be72 100644 --- a/src/plugin/context/systemparams/rapier_context_access.rs +++ b/src/plugin/context/systemparams/rapier_context_systemparam.rs @@ -8,11 +8,11 @@ use super::super::{DefaultRapierContext, RapierContext, RapierContextEntityLink} /// SAFETY: Dereferencing this struct will panic if its underlying query fails. /// See [`RapierContextAccess`] for a safer alternative. #[derive(SystemParam)] -pub struct DefaultRapierContextAccess<'w, 's, T: Component = DefaultRapierContext> { +pub struct ReadDefaultRapierContext<'w, 's, T: Component = DefaultRapierContext> { rapier_context: Query<'w, 's, &'static RapierContext, With>, } -impl<'w, 's, T: Component> DefaultRapierContextAccess<'w, 's, T> { +impl<'w, 's, T: Component> ReadDefaultRapierContext<'w, 's, T> { /// Use this method if you only have one [`RapierContext`]. /// /// SAFETY: This method will panic if its underlying query fails. @@ -22,7 +22,7 @@ impl<'w, 's, T: Component> DefaultRapierContextAccess<'w, 's, T> { } } -impl<'w, 's> Deref for DefaultRapierContextAccess<'w, 's> { +impl<'w, 's> Deref for ReadDefaultRapierContext<'w, 's> { type Target = RapierContext; /// Use this method if you only have one [`RapierContext`]. @@ -39,11 +39,11 @@ impl<'w, 's> Deref for DefaultRapierContextAccess<'w, 's> { /// SAFETY: Dereferencing this struct will panic if its underlying query fails. /// See [`RapierContextAccess`] for a safer alternative. #[derive(SystemParam)] -pub struct DefaultRapierContextAccessMut<'w, 's, T: Component = DefaultRapierContext> { +pub struct WriteDefaultRapierContext<'w, 's, T: Component = DefaultRapierContext> { rapier_context: Query<'w, 's, &'static mut RapierContext, With>, } -impl<'w, 's, T: Component> Deref for DefaultRapierContextAccessMut<'w, 's, T> { +impl<'w, 's, T: Component> Deref for WriteDefaultRapierContext<'w, 's, T> { type Target = RapierContext; /// Use this method if you only have one [`RapierContext`]. @@ -55,7 +55,7 @@ impl<'w, 's, T: Component> Deref for DefaultRapierContextAccessMut<'w, 's, T> { } } -impl<'w, 's> DerefMut for DefaultRapierContextAccessMut<'w, 's> { +impl<'w, 's> DerefMut for WriteDefaultRapierContext<'w, 's> { /// Use this method if you only have one [`RapierContext`]. /// /// SAFETY: This method will panic if its underlying query fails. @@ -103,7 +103,7 @@ impl<'w, 's> Deref for RapierContextAccess<'w, 's> { /// Utility [`SystemParam`] to easily access any [`RapierContext`] mutably #[derive(SystemParam)] -pub struct RapierContextAccessMut<'w, 's> { +pub struct WriteRapierContext<'w, 's> { /// Query used to retrieve a [`RapierContext`]. /// It's helpful to iterate over every rapier contexts, /// or get a handle over a specific context, for example through: @@ -112,7 +112,7 @@ pub struct RapierContextAccessMut<'w, 's> { pub rapier_context: Query<'w, 's, &'static mut RapierContext>, } -impl<'w, 's> RapierContextAccessMut<'w, 's> { +impl<'w, 's> WriteRapierContext<'w, 's> { /// Retrieves the rapier context responsible for the entity owning the given [`RapierContextEntityLink`]. /// /// SAFETY: This method will panic if its underlying query fails. diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 16ad310e..195bcc2b 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,6 +1,6 @@ pub use self::configuration::{RapierConfiguration, SimulationToRenderTime, TimestepMode}; pub use self::context::{ - systemparams::{DefaultRapierContextAccess, RapierContextAccess, RapierContextAccessMut}, + systemparams::{RapierContextAccess, ReadDefaultRapierContext, WriteRapierContext}, DefaultRapierContext, RapierContext, RapierContextEntityLink, }; pub use self::plugin::{ diff --git a/src/plugin/systems/character_controller.rs b/src/plugin/systems/character_controller.rs index de67606b..e349db78 100644 --- a/src/plugin/systems/character_controller.rs +++ b/src/plugin/systems/character_controller.rs @@ -3,7 +3,7 @@ use crate::dynamics::RapierRigidBodyHandle; use crate::geometry::RapierColliderHandle; use crate::plugin::context::RapierContextEntityLink; use crate::plugin::RapierConfiguration; -use crate::plugin::RapierContextAccessMut; +use crate::plugin::WriteRapierContext; use crate::prelude::KinematicCharacterController; use crate::prelude::KinematicCharacterControllerOutput; use crate::utils; @@ -17,7 +17,7 @@ use rapier::pipeline::QueryFilter; pub fn update_character_controls( mut commands: Commands, config: Query<&RapierConfiguration>, - mut context_access: RapierContextAccessMut, + mut context_access: WriteRapierContext, mut character_controllers: Query<( Entity, &RapierContextEntityLink, diff --git a/src/plugin/systems/collider.rs b/src/plugin/systems/collider.rs index be3aa755..273d561b 100644 --- a/src/plugin/systems/collider.rs +++ b/src/plugin/systems/collider.rs @@ -3,7 +3,7 @@ use crate::geometry::Collider; use crate::plugin::context::systemparams::RapierEntity; use crate::plugin::context::RapierContextEntityLink; use crate::plugin::{ - DefaultRapierContext, RapierConfiguration, RapierContext, RapierContextAccessMut, + DefaultRapierContext, RapierConfiguration, RapierContext, WriteRapierContext, }; use crate::prelude::{ ActiveCollisionTypes, ActiveEvents, ActiveHooks, ColliderDisabled, ColliderMassProperties, @@ -85,7 +85,7 @@ pub fn apply_scale( /// System responsible for applying changes the user made to a collider-related component. pub fn apply_collider_user_changes( - mut context: RapierContextAccessMut, + mut context: WriteRapierContext, config: Query<&RapierConfiguration>, (changed_collider_transforms, parent_query, transform_query): ( Query< @@ -343,7 +343,7 @@ pub(crate) fn collider_offset( pub fn init_colliders( mut commands: Commands, config: Query<&RapierConfiguration>, - mut context_access: RapierContextAccessMut, + mut context_access: WriteRapierContext, default_context_access: Query>, colliders: Query<(ColliderComponents, Option<&GlobalTransform>), Without>, mut rigid_body_mprops: Query<&mut ReadMassProperties>, diff --git a/src/plugin/systems/joint.rs b/src/plugin/systems/joint.rs index 626bdf07..f162a0ef 100644 --- a/src/plugin/systems/joint.rs +++ b/src/plugin/systems/joint.rs @@ -4,13 +4,13 @@ use crate::dynamics::RapierImpulseJointHandle; use crate::dynamics::RapierMultibodyJointHandle; use crate::plugin::context::RapierContextEntityLink; use crate::plugin::DefaultRapierContext; -use crate::plugin::RapierContextAccessMut; +use crate::plugin::WriteRapierContext; use bevy::prelude::*; /// System responsible for creating new Rapier joints from the related `bevy_rapier` components. pub fn init_joints( mut commands: Commands, - mut context_access: RapierContextAccessMut, + mut context_access: WriteRapierContext, default_context_access: Query>, impulse_joints: Query< (Entity, Option<&RapierContextEntityLink>, &ImpulseJoint), @@ -111,7 +111,7 @@ pub fn init_joints( /// System responsible for applying changes the user made to a joint component. pub fn apply_joint_user_changes( - mut context: RapierContextAccessMut, + mut context: WriteRapierContext, changed_impulse_joints: Query< ( &RapierContextEntityLink, diff --git a/src/plugin/systems/remove.rs b/src/plugin/systems/remove.rs index f1ca65b0..0342a1b9 100644 --- a/src/plugin/systems/remove.rs +++ b/src/plugin/systems/remove.rs @@ -7,7 +7,7 @@ use crate::dynamics::RigidBody; use crate::geometry::Collider; use crate::geometry::ColliderDisabled; use crate::geometry::RapierColliderHandle; -use crate::plugin::context::systemparams::RapierContextAccessMut; +use crate::plugin::context::systemparams::WriteRapierContext; use crate::plugin::RapierContext; use crate::prelude::MassModifiedEvent; use crate::prelude::RigidBodyDisabled; @@ -19,7 +19,7 @@ use bevy::prelude::*; /// despawn). pub fn sync_removals( mut commands: Commands, - mut context_accessor: RapierContextAccessMut, + mut context_writer: WriteRapierContext, mut removed_bodies: RemovedComponents, mut removed_colliders: RemovedComponents, mut removed_impulse_joints: RemovedComponents, @@ -42,7 +42,7 @@ pub fn sync_removals( * Rigid-bodies removal detection. */ for entity in removed_bodies.read() { - let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2body.remove(&entity) }) else { continue; @@ -61,7 +61,7 @@ pub fn sync_removals( } for entity in orphan_bodies.iter() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2body.remove(&entity) }) { let context = &mut *context; @@ -82,7 +82,7 @@ pub fn sync_removals( * Collider removal detection. */ for entity in removed_colliders.read() { - let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2collider.remove(&entity) }) else { continue; @@ -99,7 +99,7 @@ pub fn sync_removals( } for entity in orphan_colliders.iter() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2collider.remove(&entity) }) { let context = &mut *context; @@ -119,7 +119,7 @@ pub fn sync_removals( * Impulse joint removal detection. */ for entity in removed_impulse_joints.read() { - let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2impulse_joint.remove(&entity) }) else { continue; @@ -129,7 +129,7 @@ pub fn sync_removals( } for entity in orphan_impulse_joints.iter() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2impulse_joint.remove(&entity) }) { let context = &mut *context; @@ -142,7 +142,7 @@ pub fn sync_removals( * Multibody joint removal detection. */ for entity in removed_multibody_joints.read() { - let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2multibody_joint.remove(&entity) }) else { continue; @@ -152,7 +152,7 @@ pub fn sync_removals( } for entity in orphan_multibody_joints.iter() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2multibody_joint.remove(&entity) }) { let context = &mut *context; @@ -167,7 +167,7 @@ pub fn sync_removals( * Marker components removal detection. */ for entity in removed_sensors.read() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2collider.get(&entity).copied() }) { if let Some(co) = context.colliders.get_mut(handle) { @@ -177,7 +177,7 @@ pub fn sync_removals( } for entity in removed_colliders_disabled.read() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2collider.get(&entity).copied() }) { if let Some(co) = context.colliders.get_mut(handle) { @@ -187,7 +187,7 @@ pub fn sync_removals( } for entity in removed_rigid_body_disabled.read() { - if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + if let Some((mut context, handle)) = find_context(&mut context_writer, |context| { context.entity2body.get(&entity).copied() }) { if let Some(rb) = context.bodies.get_mut(handle) { @@ -200,10 +200,10 @@ pub fn sync_removals( } fn find_context<'a, T>( - context_accessor: &'a mut RapierContextAccessMut, + context_writer: &'a mut WriteRapierContext, item_finder: impl Fn(&mut RapierContext) -> Option, ) -> Option<(Mut<'a, RapierContext>, T)> { - context_accessor + context_writer .rapier_context .iter_mut() .find_map(|mut context| item_finder(&mut context).map(|handle| (context, handle))) diff --git a/src/plugin/systems/rigid_body.rs b/src/plugin/systems/rigid_body.rs index 571235b5..67a91886 100644 --- a/src/plugin/systems/rigid_body.rs +++ b/src/plugin/systems/rigid_body.rs @@ -39,7 +39,7 @@ pub type RigidBodyComponents<'a> = ( /// System responsible for applying changes the user made to a rigid-body-related component. pub fn apply_rigid_body_user_changes( - mut context: RapierContextAccessMut, + mut context: WriteRapierContext, config: Query<&RapierConfiguration>, changed_rb_types: Query< (&RapierRigidBodyHandle, &RapierContextEntityLink, &RigidBody), @@ -353,7 +353,7 @@ pub fn apply_rigid_body_user_changes( /// System responsible for writing the result of the last simulation step into our `bevy_rapier` /// components and the [`GlobalTransform`] component. pub fn writeback_rigid_bodies( - mut context: RapierContextAccessMut, + mut context: WriteRapierContext, timestep_mode: Res, config: Query<&RapierConfiguration>, sim_to_render_time: Query<&SimulationToRenderTime>, @@ -643,7 +643,7 @@ pub fn init_rigid_bodies( /// mass to be available, which it was not because colliders were not created yet. As a /// result, we run this system after the collider creation. pub fn apply_initial_rigid_body_impulses( - mut context: RapierContextAccessMut, + mut context: WriteRapierContext, // We can’t use RapierRigidBodyHandle yet because its creation command hasn’t been // executed yet. mut init_impulses: Query<