Skip to content

Commit

Permalink
Merge pull request bevyengine#15 from Bobox214/remove_entity_to_body
Browse files Browse the repository at this point in the history
Remove EntityToBody resource
  • Loading branch information
sebcrozet authored Oct 4, 2020
2 parents 2280ec1 + ba1cc83 commit e07e449
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/physics/plugins.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(),
Expand Down
15 changes: 0 additions & 15 deletions src/physics/resources.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Entity, RigidBodyHandle>);

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<f32>);

Expand Down
35 changes: 19 additions & 16 deletions src/physics/systems.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,13 +22,11 @@ pub fn create_body_and_collider_system(
mut commands: Commands,
mut bodies: ResMut<RigidBodySet>,
mut colliders: ResMut<ColliderSet>,
mut entity2body: ResMut<EntityToBody>,
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::<RigidBodyBuilder>(entity);

Expand All @@ -41,16 +39,15 @@ pub fn create_body_and_collider_system(
pub fn create_joints_system(
mut commands: Commands,
mut bodies: ResMut<RigidBodySet>,
entity2body: Res<EntityToBody>,
mut joints: ResMut<JointSet>,
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::<RigidBodyHandleComponent>(joint.entity1);
let body2 = query_bodyhandle.get::<RigidBodyHandleComponent>(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),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e07e449

Please sign in to comment.