diff --git a/src/physics/plugins.rs b/src/physics/plugins.rs index 2fb1d5a35bece..22d5c020a5ee4 100644 --- a/src/physics/plugins.rs +++ b/src/physics/plugins.rs @@ -1,5 +1,5 @@ use crate::physics; -use crate::physics::{EntityToBody, EventQueue, Gravity, RapierPhysicsScale}; +use crate::physics::{EventQueue, Gravity, RapierPhysicsScale}; use bevy::prelude::*; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; @@ -33,7 +33,6 @@ impl Plugin for RapierPhysicsPlugin { // TODO: can we avoid this map? We are only using this // to avoid some borrowing issue when joints creations // are needed. - .add_resource(EntityToBody::new()) .add_system_to_stage_front( stage::PRE_UPDATE, physics::create_body_and_collider_system.system(), diff --git a/src/physics/resources.rs b/src/physics/resources.rs index 27f3776ea1bca..79bf637c49c92 100644 --- a/src/physics/resources.rs +++ b/src/physics/resources.rs @@ -1,10 +1,7 @@ use crate::rapier::geometry::{ContactEvent, ProximityEvent}; use crate::rapier::pipeline::EventHandler; -use bevy::ecs::Entity; use concurrent_queue::ConcurrentQueue; -use rapier::dynamics::RigidBodyHandle; use rapier::math::Vector; -use std::collections::HashMap; #[derive(Copy, Clone)] /// A component describing a scale ration between the physics world and the bevy transforms. @@ -13,18 +10,6 @@ use std::collections::HashMap; /// Each Rapier rigid-body position will have its coordinates multiplied by this scale factor. pub struct RapierPhysicsScale(pub f32); -/// A map between bevy's entities and Rapier's handles. -/// -/// This map will likely be removed in the future. -pub struct EntityToBody(pub(crate) HashMap); - -impl EntityToBody { - /// Create a new empty map. - pub fn new() -> Self { - EntityToBody(HashMap::new()) - } -} - /// A resource for specifying the gravity of the physics simulation. pub struct Gravity(pub Vector); diff --git a/src/physics/systems.rs b/src/physics/systems.rs index 8b5886b4b9d61..4bd8c2965959b 100644 --- a/src/physics/systems.rs +++ b/src/physics/systems.rs @@ -1,6 +1,6 @@ use crate::physics::{ - ColliderHandleComponent, EntityToBody, EventQueue, Gravity, JointBuilderComponent, - JointHandleComponent, RapierPhysicsScale, RigidBodyHandleComponent, + ColliderHandleComponent, EventQueue, Gravity, JointBuilderComponent, JointHandleComponent, + RapierPhysicsScale, RigidBodyHandleComponent, }; use bevy::ecs::Mut; @@ -22,13 +22,11 @@ pub fn create_body_and_collider_system( mut commands: Commands, mut bodies: ResMut, mut colliders: ResMut, - mut entity2body: ResMut, entity: Entity, body_builder: &RigidBodyBuilder, collider_builder: &ColliderBuilder, ) { let handle = bodies.insert(body_builder.build()); - entity2body.0.insert(entity, handle); commands.insert_one(entity, RigidBodyHandleComponent::from(handle)); commands.remove_one::(entity); @@ -41,16 +39,15 @@ pub fn create_body_and_collider_system( pub fn create_joints_system( mut commands: Commands, mut bodies: ResMut, - entity2body: Res, mut joints: ResMut, mut query: Query<(Entity, &JointBuilderComponent)>, + query_bodyhandle: Query<&RigidBodyHandleComponent>, ) { for (entity, joint) in &mut query.iter() { - let body1 = entity2body.0.get(&joint.entity1); - let body2 = entity2body.0.get(&joint.entity2); - - if let (Some(body1), Some(body2)) = (body1, body2) { - let handle = joints.insert(&mut bodies, *body1, *body2, joint.params); + let body1 = query_bodyhandle.get::(joint.entity1); + let body2 = query_bodyhandle.get::(joint.entity2); + if let (Ok(body1), Ok(body2)) = (body1, body2) { + let handle = joints.insert(&mut bodies, body1.handle(), body2.handle(), joint.params); commands.insert_one( entity, JointHandleComponent::new(handle, joint.entity1, joint.entity2), @@ -101,17 +98,23 @@ pub fn sync_transform_system( #[cfg(feature = "dim2")] { let rot = na::UnitQuaternion::new(na::Vector3::z() * pos.rotation.angle()); - transform.set_translation(Vec3::new(pos.translation.vector.x * scale.0, pos.translation.vector.y * scale.0, 0.0)); + transform.set_translation(Vec3::new( + pos.translation.vector.x * scale.0, + pos.translation.vector.y * scale.0, + 0.0, + )); transform.set_rotation(Quat::from_xyzw(rot.i, rot.j, rot.k, rot.w)); } #[cfg(feature = "dim3")] { - transform.set_translation(Vec3::new( - pos.translation.vector.x, - pos.translation.vector.y, - pos.translation.vector.z, - ) * scale.0); + transform.set_translation( + Vec3::new( + pos.translation.vector.x, + pos.translation.vector.y, + pos.translation.vector.z, + ) * scale.0, + ); transform.set_rotation(Quat::from_xyzw( pos.rotation.i,