Skip to content

Commit

Permalink
cargo clippy + pr feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jul 1, 2024
1 parent f900dac commit d171fe4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/geometry/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
#[cfg(all(feature = "dim3", feature = "async-collider"))]
use {crate::geometry::VHACDParameters, bevy::utils::HashMap};

use bevy::{prelude::*, reflect::impl_reflect};
use bevy::prelude::*;

use bevy::utils::HashSet;
use rapier::geometry::Shape;
Expand Down
39 changes: 19 additions & 20 deletions src/plugin/context/systemparams/rapier_context_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::ops::{Deref, DerefMut};
use super::super::{DefaultRapierContext, RapierContext, RapierContextEntityLink};
/// Utility [`SystemParam`] to easily access the default world [`RapierContext`] immutably
#[derive(SystemParam)]
pub struct DefaultRapierContextAccess<'w, 's> {
rapier_context: Query<'w, 's, &'static RapierContext, With<DefaultRapierContext>>,
pub struct DefaultRapierContextAccess<'w, 's, T: Component = DefaultRapierContext> {
rapier_context: Query<'w, 's, &'static RapierContext, With<T>>,
}

impl<'w, 's> DefaultRapierContextAccess<'w, 's> {
impl<'w, 's, T: Component> DefaultRapierContextAccess<'w, 's, T> {
/// Use this method if you only have one world.
pub fn single(&'_ self) -> &RapierContext {
self.rapier_context.single()
Expand All @@ -26,11 +26,11 @@ impl<'w, 's> Deref for DefaultRapierContextAccess<'w, 's> {

/// Utility [`SystemParam`] to easily access the default world [`RapierContext`] mutably
#[derive(SystemParam)]
pub struct DefaultRapierContextAccessMut<'w, 's> {
rapier_context: Query<'w, 's, &'static mut RapierContext, With<DefaultRapierContext>>,
pub struct DefaultRapierContextAccessMut<'w, 's, T: Component = DefaultRapierContext> {
rapier_context: Query<'w, 's, &'static mut RapierContext, With<T>>,
}

impl<'w, 's> Deref for DefaultRapierContextAccessMut<'w, 's> {
impl<'w, 's, T: Component> Deref for DefaultRapierContextAccessMut<'w, 's, T> {
type Target = RapierContext;

fn deref(&self) -> &Self::Target {
Expand All @@ -48,10 +48,16 @@ impl<'w, 's> DerefMut for DefaultRapierContextAccessMut<'w, 's> {
/// Utility [`SystemParam`] to easily access any [`RapierContext`] immutably
#[derive(SystemParam)]
pub struct RapierContextAccess<'w, 's> {
rapier_context: Query<'w, 's, &'static RapierContext>,
/// 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:
/// - a marker component such as [`DefaultRapierContext`]
/// - a [`RapierContextEntityLink`]. See [context](RapierContextAccess::context)
pub rapier_context: Query<'w, 's, &'static RapierContext>,
}

impl<'w, 's> RapierContextAccess<'w, 's> {
/// Retrieves the rapier context responsible for the entity owning the given [`RapierContextEntityLink`].
pub fn context(&self, link: RapierContextEntityLink) -> &'_ RapierContext {
self.rapier_context
.get(link.0)
Expand All @@ -70,27 +76,20 @@ impl<'w, 's> Deref for RapierContextAccess<'w, 's> {
/// Utility [`SystemParam`] to easily access any [`RapierContext`] mutably
#[derive(SystemParam)]
pub struct RapierContextAccessMut<'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:
/// - a marker component such as [`DefaultRapierContext`]
/// - a [`RapierContextEntityLink`]. See [context](RapierContextAccess::context)
pub rapier_context: Query<'w, 's, &'static mut RapierContext>,
}

impl<'w, 's> RapierContextAccessMut<'w, 's> {
/// Retrieves the rapier context responsible for the entity owning the given [`RapierContextEntityLink`].
pub fn context(&mut self, link: RapierContextEntityLink) -> &'_ mut RapierContext {
self.rapier_context
.get_mut(link.0)
.expect("RapierContextEntityLink.0 refers to an entity without RapierContext.")
.into_inner()
}
}

pub fn try_retrieve_context<'a>(
link: Option<&RapierContextEntityLink>,
context: &Query<Entity, With<RapierContext>>,
) -> Result<Entity, Entity> {
link.map_or_else(
|| {
let context_entity = context.iter().next().unwrap();
Err(context_entity)
},
|link| Ok(link.0),
)
}
22 changes: 14 additions & 8 deletions src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ where
/// likely always be 1.0 in 3D. In 2D, this is useful to specify a "pixels-per-meter"
/// conversion ratio.
pub fn with_length_unit(mut self, length_unit: f32) -> Self {
self.default_world_setup = RapierContextInitialization::InitializeDefaultRapierContext {
length_unit: length_unit,
};
self.default_world_setup =
RapierContextInitialization::InitializeDefaultRapierContext { length_unit };
self
}

/// Specifies a default world initialization strategy.
///
/// The default is to initialize a [`RapierContext`] with a length unit of 1.
pub fn with_default_world(
mut self,
default_world_initialization: RapierContextInitialization,
Expand Down Expand Up @@ -106,20 +109,17 @@ where
.in_set(RapierTransformPropagateSet),
systems::on_add_entity_with_parent,
systems::on_change_world,
apply_deferred,
//
systems::sync_removals,
apply_deferred,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_scene_colliders,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_colliders,
systems::init_rigid_bodies,
systems::init_colliders,
systems::init_joints,
apply_deferred,
//systems::sync_removals,
// Run this here so the following systems do not have a 1 frame delay.
apply_deferred,
systems::apply_scale,
systems::apply_collider_user_changes,
systems::apply_rigid_body_user_changes,
Expand Down Expand Up @@ -292,8 +292,14 @@ where
/// and as long as any [`RapierContextEntityLink`] has a reference to its [`RapierContext`].
#[derive(Resource, Debug, Reflect, Clone)]
pub enum RapierContextInitialization {
/// [`RapierPhysicsPlugin`] will not spawn any entity containing [`RapierContext`] automatically.
NoAutomaticRapierContext,
InitializeDefaultRapierContext { length_unit: f32 },
/// [`RapierPhysicsPlugin`] will spawn an entity containing a [`RapierContext`]
/// automatically during [`PreStartup`], with the [`DefaultRapierContext`] marker component.
InitializeDefaultRapierContext {
/// See [`IntegrationParameters::length_unit`]
length_unit: f32,
},
}

impl Default for RapierContextInitialization {
Expand Down
5 changes: 2 additions & 3 deletions src/plugin/systems/collider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::dynamics::ReadMassProperties;
use crate::geometry::Collider;
use crate::plugin::context::systemparams::{try_retrieve_context, RapierEntity};
use crate::plugin::context::systemparams::RapierEntity;
use crate::plugin::context::RapierContextEntityLink;
use crate::plugin::{RapierConfiguration, RapierContext, RapierContextAccessMut};
use crate::prelude::{
Expand Down Expand Up @@ -142,11 +142,10 @@ pub fn apply_collider_user_changes(
) {
for (rapier_entity, handle, transform) in changed_collider_transforms.iter() {
let context = context.context(*rapier_entity.rapier_context_link);
let config = config.get(rapier_entity.rapier_context_link.0).unwrap();
if context.collider_parent(rapier_entity.entity).is_some() {
let (_, collider_position) = collider_offset(
rapier_entity.entity,
&context,
context,
&parent_query,
&transform_query,
);
Expand Down
1 change: 0 additions & 1 deletion src/plugin/systems/joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::dynamics::ImpulseJoint;
use crate::dynamics::MultibodyJoint;
use crate::dynamics::RapierImpulseJointHandle;
use crate::dynamics::RapierMultibodyJointHandle;
use crate::plugin::context::systemparams::try_retrieve_context;
use crate::plugin::context::RapierContextEntityLink;
use crate::plugin::RapierContext;
use crate::plugin::RapierContextAccessMut;
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/systems/multiple_rapier_contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use bevy::prelude::*;
///
/// If this fails to happen, weirdness will ensue.
pub fn on_add_entity_with_parent(
q_add_entity_without_parent: Query<(Entity, &Parent), Changed<Parent>>,
q_add_entity_without_parent: Query<
(Entity, &Parent),
(With<RapierContextEntityLink>, Changed<Parent>),
>,
q_parent: Query<&Parent>,
q_physics_world: Query<&RapierContextEntityLink>,
mut commands: Commands,
Expand Down
51 changes: 21 additions & 30 deletions src/plugin/systems/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::geometry::Collider;
use crate::geometry::ColliderDisabled;
use crate::geometry::RapierColliderHandle;
use crate::plugin::context::systemparams::RapierContextAccessMut;
use crate::plugin::context::RapierContextEntityLink;
use crate::plugin::RapierContext;
use crate::prelude::MassModifiedEvent;
use crate::prelude::RigidBodyDisabled;
Expand All @@ -20,7 +19,7 @@ use bevy::prelude::*;
/// despawn).
pub fn sync_removals(
mut commands: Commands,
mut context: RapierContextAccessMut,
mut context_accessor: RapierContextAccessMut,
mut removed_bodies: RemovedComponents<RapierRigidBodyHandle>,
mut removed_colliders: RemovedComponents<RapierColliderHandle>,
mut removed_impulse_joints: RemovedComponents<RapierImpulseJointHandle>,
Expand All @@ -43,9 +42,9 @@ pub fn sync_removals(
* Rigid-bodies removal detection.
*/
for entity in removed_bodies.read() {
let Some((mut context, handle)) =
find_context(&mut context, |context| context.entity2body.remove(&entity))
else {
let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2body.remove(&entity)
}) else {
continue;
};
let context = &mut *context;
Expand All @@ -62,9 +61,9 @@ pub fn sync_removals(
}

for entity in orphan_bodies.iter() {
if let Some((mut context, handle)) =
find_context(&mut context, |context| context.entity2body.remove(&entity))
{
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2body.remove(&entity)
}) {
let context = &mut *context;
let _ = context.last_body_transform_set.remove(&handle);
context.bodies.remove(
Expand All @@ -83,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, |context| {
let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2collider.remove(&entity)
}) else {
continue;
Expand All @@ -100,7 +99,7 @@ pub fn sync_removals(
}

for entity in orphan_colliders.iter() {
if let Some((mut context, handle)) = find_context(&mut context, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2collider.remove(&entity)
}) {
let context = &mut *context;
Expand All @@ -120,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, |context| {
let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2impulse_joint.remove(&entity)
}) else {
continue;
Expand All @@ -130,7 +129,7 @@ pub fn sync_removals(
}

for entity in orphan_impulse_joints.iter() {
if let Some((mut context, handle)) = find_context(&mut context, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2impulse_joint.remove(&entity)
}) {
let context = &mut *context;
Expand All @@ -143,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, |context| {
let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2multibody_joint.remove(&entity)
}) else {
continue;
Expand All @@ -153,7 +152,7 @@ pub fn sync_removals(
}

for entity in orphan_multibody_joints.iter() {
if let Some((mut context, handle)) = find_context(&mut context, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2multibody_joint.remove(&entity)
}) {
let context = &mut *context;
Expand All @@ -168,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, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2collider.get(&entity).copied()
}) {
if let Some(co) = context.colliders.get_mut(handle) {
Expand All @@ -178,7 +177,7 @@ pub fn sync_removals(
}

for entity in removed_colliders_disabled.read() {
if let Some((mut context, handle)) = find_context(&mut context, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2collider.get(&entity).copied()
}) {
if let Some(co) = context.colliders.get_mut(handle) {
Expand All @@ -188,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, |context| {
if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| {
context.entity2body.get(&entity).copied()
}) {
if let Some(rb) = context.bodies.get_mut(handle) {
Expand All @@ -201,19 +200,11 @@ pub fn sync_removals(
}

fn find_context<'a, T>(
context: &'a mut RapierContextAccessMut,
context_accessor: &'a mut RapierContextAccessMut,
item_finder: impl Fn(&mut RapierContext) -> Option<T>,
) -> Option<(Mut<'a, RapierContext>, T)> {
let finder = item_finder;
let context = {
let mut result = None;
for mut context in context.rapier_context.iter_mut() {
if let Some(handle) = finder(&mut *context) {
result = Some((context, handle));
break;
}
}
result
};
context
context_accessor
.rapier_context
.iter_mut()
.find_map(|mut context| item_finder(&mut context).map(|handle| (context, handle)))
}

0 comments on commit d171fe4

Please sign in to comment.