Skip to content

Commit

Permalink
rename rapier context system params
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Aug 12, 2024
1 parent 07eefbb commit 4cccde1
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<mut RapierContext>` -> `DefaultRapierContextAccessMut`
- `Res<RapierContext>` -> `DefaultRapierContextAccess`
- `ResMut<mut RapierContext>` -> `WriteDefaultRapierContext`
- `Res<RapierContext>` -> `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,
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/testbed2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/joints3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/ray_casting3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn setup_physics(mut commands: Commands) {
pub fn cast_ray(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
rapier_context: DefaultRapierContextAccess,
rapier_context: ReadDefaultRapierContext,
cameras: Query<(&Camera, &GlobalTransform)>,
) {
let window = windows.single();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/context/systemparams/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>>,
}

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.
Expand All @@ -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`].
Expand All @@ -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<T>>,
}

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`].
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/systems/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/systems/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<
Expand Down Expand Up @@ -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<Entity, With<DefaultRapierContext>>,
colliders: Query<(ColliderComponents, Option<&GlobalTransform>), Without<RapierColliderHandle>>,
mut rigid_body_mprops: Query<&mut ReadMassProperties>,
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/systems/joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity, With<DefaultRapierContext>>,
impulse_joints: Query<
(Entity, Option<&RapierContextEntityLink>, &ImpulseJoint),
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions src/plugin/systems/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RapierRigidBodyHandle>,
mut removed_colliders: RemovedComponents<RapierColliderHandle>,
mut removed_impulse_joints: RemovedComponents<RapierImpulseJointHandle>,
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<T>,
) -> 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)))
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/systems/rigid_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<TimestepMode>,
config: Query<&RapierConfiguration>,
sim_to_render_time: Query<&SimulationToRenderTime>,
Expand Down Expand Up @@ -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<
Expand Down

0 comments on commit 4cccde1

Please sign in to comment.