From ddde4b8fdb9ad5dd5e308d237360dcb019fa22a0 Mon Sep 17 00:00:00 2001 From: kel Date: Tue, 18 Dec 2018 20:29:29 -0500 Subject: [PATCH] Add log crate logging --- Cargo.toml | 1 + README.md | 9 ++-- src/bodies.rs | 6 +-- src/lib.rs | 3 ++ src/systems/physics_stepper.rs | 11 +++-- src/systems/sync_bodies_from_physics.rs | 28 ++++++++---- src/systems/sync_bodies_to_physics.rs | 54 +++++++++++++++--------- src/systems/sync_colliders_to_physics.rs | 35 +++++++++------ 8 files changed, 95 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 852c5fe..8f2f4a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ num-traits = "0.2" derive-new = "0.5.6" derive_builder = "0.7.0" serde = { version = "1.0", features = ["derive"] } +log = "*" [dependencies.amethyst] git = "https://github.com/amethyst/amethyst" diff --git a/README.md b/README.md index dc6b81b..b51495a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ both amethyst and plain specs interfaces to be exposed, also behind configuratio I'll update this as I go along. 1. - - `"sync_force_generators_to_physics_system"` - Synchronize changes to force generators to physics world - `"sync_bodies_to_physics_system"` - Synchronize changes to dynamics bodies to physics world - `"sync_gravity_to_physics_system"` - Update gravity of physics world from resource 1. `"physics_stepper_system"` - Step physics world simulation @@ -25,15 +24,15 @@ Full *TODO* sheet can be found in [this nphysics issue](https://github.com/rusts Ongoing work: - [x] RigidBody Components -- [ ] Force Generator Components +- [x] External force property +- [x] `log` based logging - [ ] Collider Components +- [ ] Proximity & Curve-based external force utility - [ ] Proximity and Contact EventChannels +- [ ] Handling Body Activation & Sleeping Investigating: - [ ] Multibody-based Component Joints - [ ] Constraint-based Joints - [ ] Kinematics -- [ ] Body Activation & Sleeping. - -Also, implement logging using the log crate. diff --git a/src/bodies.rs b/src/bodies.rs index 4cda1e6..ed363d0 100644 --- a/src/bodies.rs +++ b/src/bodies.rs @@ -6,7 +6,7 @@ use nphysics3d::object::BodyHandle; use std::collections::HashMap; /// Physics body component for describing (currently) rigid body dynamics. -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub enum DynamicBody { RigidBody(RigidPhysicsBody), Multibody(PhysicsMultibody), @@ -57,7 +57,7 @@ impl Component for DynamicBody { /// Rigid physics body, for use in `PhysicsBody` Component. /// Currently only the velocity is read and updated at runtime. /// The properties of mass are only written at physics body creation time. -#[derive(Serialize, Deserialize, Clone, Debug, new)] +#[derive(Serialize, Deserialize, Clone, Copy, Debug, new)] pub struct RigidPhysicsBody { #[serde(skip)] #[new(default)] @@ -73,7 +73,7 @@ pub struct RigidPhysicsBody { } /// Multipart physics body, for use in `PhysicsBody` Component. Not implemented yet. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Copy, Debug)] pub struct PhysicsMultibody { #[serde(skip)] pub handle: Option, diff --git a/src/lib.rs b/src/lib.rs index 4efc936..24cde28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,9 @@ extern crate derive_new; #[macro_use] extern crate serde; +#[macro_use] +extern crate log; + pub mod bodies; pub mod colliders; pub mod systems; diff --git a/src/systems/physics_stepper.rs b/src/systems/physics_stepper.rs index cb04299..1b1a4c0 100644 --- a/src/systems/physics_stepper.rs +++ b/src/systems/physics_stepper.rs @@ -15,10 +15,15 @@ impl PhysicsStepperSystem { impl<'a> System<'a> for PhysicsStepperSystem { type SystemData = (WriteExpect<'a, PhysicsWorld>, Read<'a, Time>); + // Simulate world using the current time frame + // TODO: Bound timestep deltas fn run(&mut self, (mut physical_world, time): Self::SystemData) { - // Simulate world using the current time frame - // TODO: Bound timestep deltas - physical_world.set_timestep(time.delta_seconds()); + let delta = time.delta_seconds(); + + trace!("Setting timestep with delta: {}", delta); + physical_world.set_timestep(delta); + + trace!("Stepping physical world simulation."); physical_world.step(); } } diff --git a/src/systems/sync_bodies_from_physics.rs b/src/systems/sync_bodies_from_physics.rs index dd7ad69..797ccf3 100644 --- a/src/systems/sync_bodies_from_physics.rs +++ b/src/systems/sync_bodies_from_physics.rs @@ -47,6 +47,8 @@ impl<'a> System<'a> for SyncBodiesFromPhysicsSystem { colliders, ) = data; + trace!("Synchronizing bodies from physical world."); + // Apply the updated values of the simulated world to our Components #[allow(unused_mut)] for (mut global_transform, mut body, local_transform) in ( @@ -59,6 +61,7 @@ impl<'a> System<'a> for SyncBodiesFromPhysicsSystem { let updated_body = physical_world.body(body.handle().unwrap()); if updated_body.is_ground() || !updated_body.is_active() || updated_body.is_static() { + trace!("Skipping synchronizing data from non-dynamic body: {:?}", updated_body.handle()); continue; } @@ -67,11 +70,9 @@ impl<'a> System<'a> for SyncBodiesFromPhysicsSystem { DynamicBody::RigidBody(ref mut rigid_body), Body::RigidBody(ref updated_rigid_body), ) => { - println!( - "super power: change mehhh! new pos: {:?}", - updated_rigid_body.position() - ); + trace!("Synchronizing RigidBody from handle: {:?}", updated_rigid_body.handle()); + trace!("Synchronized RigidBody's updated position: {}", updated_rigid_body.position()); // TODO: Might get rid of the scale!!! global_transform.0 = updated_rigid_body .position() @@ -82,25 +83,32 @@ impl<'a> System<'a> for SyncBodiesFromPhysicsSystem { .unwrap_or(&Vector3::new(1.0, 1.0, 1.0)), ); + trace!("Synchronized RigidBody's updated velocity: {:?}", updated_rigid_body.velocity()); rigid_body.velocity = *updated_rigid_body.velocity(); + + trace!("Synchronized RigidBody's updated inertia: {:?}", updated_rigid_body.inertia()); let inertia = updated_rigid_body.inertia(); rigid_body.mass = inertia.linear; rigid_body.angular_mass = inertia.angular; + + trace!("Synchronized RigidBody's updated center of mass: {}", updated_rigid_body.center_of_mass()); rigid_body.center_of_mass = updated_rigid_body.center_of_mass(); } (DynamicBody::Multibody(_multibody), Body::Multibody(_updated_multibody)) => { - // match updated_multibody.links().next() { - // Some(link) => link.position(), - // None => continue, - // }; + error!("Multibody found; not implemented currently, sorry!") } - _ => println!("Unexpected dynamics body pair."), + // TODO: Add data to unexpected pair message. Not straightforward. + _ => error!("Unexpected dynamics body pair!"), }; } + trace!("Iterating collision events."); + let collision_world = physical_world.collision_world(); contact_events.iter_write(collision_world.contact_events().iter().cloned().map(|ev| { + trace!("Emitting contact event: {}", ev); + let (handle1, handle2) = match ev { ContactEvent::Started(h1, h2) => (h1, h2), ContactEvent::Stopped(h1, h2) => (h1, h2), @@ -119,6 +127,8 @@ impl<'a> System<'a> for SyncBodiesFromPhysicsSystem { .iter() .cloned() .map(|ev| { + trace!("Emitting proximity event: {}", ev); + let e1 = entity_from_handle(&entities, &colliders, &ev.collider1) .expect("Failed to find entity for collider."); let e2 = entity_from_handle(&entities, &colliders, &ev.collider2) diff --git a/src/systems/sync_bodies_to_physics.rs b/src/systems/sync_bodies_to_physics.rs index 7d28db4..eb57fdb 100644 --- a/src/systems/sync_bodies_to_physics.rs +++ b/src/systems/sync_bodies_to_physics.rs @@ -8,7 +8,7 @@ use amethyst::ecs::{ }; use core::ops::Deref; use nalgebra::try_convert; -use nphysics3d::math::Inertia; +use nphysics3d::math::{Inertia, Force, Isometry}; #[derive(Default)] pub struct SyncBodiesToPhysicsSystem { @@ -39,6 +39,7 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem { let mut modified_physics_bodies = BitSet::new(); // Get change flag events for transforms, removing deleted ones from the physics world. + trace!("Iterating transform storage events."); iterate_events( &transforms, self.transforms_reader_id.as_mut().unwrap(), @@ -50,6 +51,7 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem { ); // Get change flag events for physics bodies, removing deleted ones from the physics world. + trace!("Iterating physics body storage events."); iterate_events( &physics_bodies, self.physics_bodies_reader_id.as_mut().unwrap(), @@ -74,16 +76,16 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem { .join() { if inserted_transforms.contains(id) || inserted_physics_bodies.contains(id) { - println!("Detected inserted dynamics body with id {:?}", id); + trace!("Detected inserted dynamics body with id {}", id); + match body { DynamicBody::RigidBody(ref mut rigid_body) => { // Just inserted. Remove old one and insert new. - if rigid_body.handle.is_some() - && physical_world - .rigid_body(rigid_body.handle.unwrap()) - .is_some() - { - physical_world.remove_bodies(&[rigid_body.handle.unwrap()]); + if let Some(handle) = rigid_body.handle { + if physical_world.rigid_body(handle).is_some() { + trace!("Removing body marked as inserted that already exists with handle: {:?}", handle); + physical_world.remove_bodies(&[handle]); + } } rigid_body.handle = Some(physical_world.add_rigid_body( @@ -92,33 +94,45 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem { rigid_body.center_of_mass, )); + trace!("Inserted rigid body to world with values: {:?}", rigid_body); + let physical_body = physical_world .rigid_body_mut(rigid_body.handle.unwrap()) .unwrap(); physical_body.set_velocity(rigid_body.velocity); physical_body.apply_force(&rigid_body.external_forces); + rigid_body.external_forces = Force::::zero(); + + trace!("Velocity and external forces applied, external forces reset to zero, for body with handle: {:?}", rigid_body.handle); } DynamicBody::Multibody(_) => { // TODO + error!("Multibody found; not implemented currently, sorry!") } } } else if modified_transforms.contains(id) || modified_physics_bodies.contains(id) { - println!("Detected changed dynamics body with id {:?}", id); + trace!("Detected changed dynamics body with id {}", id); match body { DynamicBody::RigidBody(ref mut rigid_body) => { - let physical_body = physical_world - .rigid_body_mut(rigid_body.handle.unwrap()) - .unwrap(); - - physical_body.set_position(try_convert(transform.0).unwrap()); - physical_body.set_velocity(rigid_body.velocity); - physical_body.apply_force(&rigid_body.external_forces); + match physical_world.rigid_body_mut(rigid_body.handle.unwrap()) { + Some(physical_body) => { + let position: Isometry = try_convert(transform.0).unwrap(); + trace!("Updating rigid body in physics world with isometry: {}", position); + physical_body.set_position(position); + physical_body.set_velocity(rigid_body.velocity); + physical_body.apply_force(&rigid_body.external_forces); + + // if you changed the mass properties at all... too bad! + }, + None => { - // if you changed the mass properties at all... too bad! + } + } } DynamicBody::Multibody(_) => { // TODO + error!("Multibody found; not implemented currently, sorry!") } } } @@ -165,15 +179,17 @@ fn iterate_events<'a, T, D, S>( Some(body) => { match body.handle() { Some(handle) => { + trace!("Removing body with id: {}", id); + world.remove_bodies(&[handle]); } None => { - // TODO: Log missing handle + error!("Missing handle in body: {}", id); } }; } None => { - // TODO: Log missing body + error!("Missing body with id: {}", id); } }; } diff --git a/src/systems/sync_colliders_to_physics.rs b/src/systems/sync_colliders_to_physics.rs index 59d8b20..3dcead6 100644 --- a/src/systems/sync_colliders_to_physics.rs +++ b/src/systems/sync_colliders_to_physics.rs @@ -49,19 +49,26 @@ impl<'a> System<'a> for SyncCollidersToPhysicsSystem { .join() { if inserted_colliders.contains(id) { - println!("Detected inserted collider with id {:?}", id); + trace!("Detected inserted collider with id {}", id); + // Just inserted. Remove old one and insert new. - if collider.handle.is_some() - && physical_world.collider(collider.handle.unwrap()).is_some() - { - physical_world.remove_colliders(&[collider.handle.unwrap()]); + if let Some(handle) = collider.handle { + if physical_world.collider(handle).is_some() { + trace!("Removing collider marked as inserted that already exists with handle: {:?}", handle); + + physical_world.remove_colliders(&[handle]); + } } let parent = if let Some(rb) = rigid_bodies.get(entity) { + trace!("Attaching inserted collider to rigid body: {}", entity); + rb.handle().expect( "You should normally have a body handle at this point. This is a bug.", ) } else { + trace!("Attaching inserted collider to ground."); + BodyHandle::ground() }; @@ -73,21 +80,21 @@ impl<'a> System<'a> for SyncCollidersToPhysicsSystem { collider.physics_material.clone(), )); + trace!("Inserted collider to world with values: {}", collider); + let prediction = physical_world.prediction(); let angular_prediction = physical_world.angular_prediction(); let collision_world = physical_world.collision_world_mut(); - let collider_handle = collision_world - .collision_object(collider.handle.unwrap()) - .unwrap() - .handle() - .clone(); - collision_world.set_collision_group(collider_handle, collider.collision_group); let collider_object = collision_world .collision_object_mut(collider.handle.unwrap()) .unwrap(); + let collider_handle = collider_object.handle().clone(); + + collision_world.set_collision_group(collider_handle, collider.collision_group); + collider_object.set_query_type(collider.query_type.to_geometric_query_type( collider.margin, prediction, @@ -163,15 +170,17 @@ fn iterate_events<'a, T, D, S>( Some(collider) => { match collider.handle { Some(handle) => { + trace!("Removing collider with id: {}", id); + world.remove_colliders(&[handle]); } None => { - // TODO: Log missing handle + error!("Missing handle in collider: {}", id); } }; } None => { - // TODO: Log missing body + error!("Missing collider with id: {}", id); } }; }