From bcaa1d13e8f95fb8dec2ba14b9d5f6d0dd3261fb Mon Sep 17 00:00:00 2001 From: rezural Date: Sat, 19 Dec 2020 21:54:38 +1100 Subject: extract PhysicsState, PhysicsSnapshot & PhysicsEvents cargo fmt changes to make rapier compile --- src_testbed/physics/mod.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src_testbed/physics/mod.rs (limited to 'src_testbed/physics') diff --git a/src_testbed/physics/mod.rs b/src_testbed/physics/mod.rs new file mode 100644 index 0000000..09673d0 --- /dev/null +++ b/src_testbed/physics/mod.rs @@ -0,0 +1,112 @@ +use crossbeam::channel::Receiver; +use rapier::math::Vector; +use rapier::pipeline::{PhysicsPipeline, QueryPipeline}; +use rapier::dynamics::{ + IntegrationParameters, JointSet,RigidBodySet, +}; +use rapier::geometry::{ + BroadPhase, ColliderSet, ContactEvent, NarrowPhase, ProximityEvent, +}; + +pub struct PhysicsSnapshot { + timestep_id: usize, + broad_phase: Vec, + narrow_phase: Vec, + bodies: Vec, + colliders: Vec, + joints: Vec, +} + +impl PhysicsSnapshot { + pub fn new( + timestep_id: usize, + broad_phase: &BroadPhase, + narrow_phase: &NarrowPhase, + bodies: &RigidBodySet, + colliders: &ColliderSet, + joints: &JointSet, + ) -> bincode::Result { + Ok(Self { + timestep_id, + broad_phase: bincode::serialize(broad_phase)?, + narrow_phase: bincode::serialize(narrow_phase)?, + bodies: bincode::serialize(bodies)?, + colliders: bincode::serialize(colliders)?, + joints: bincode::serialize(joints)?, + }) + } + + pub fn restore( + &self, + ) -> bincode::Result<( + usize, + BroadPhase, + NarrowPhase, + RigidBodySet, + ColliderSet, + JointSet, + )> { + Ok(( + self.timestep_id, + bincode::deserialize(&self.broad_phase)?, + bincode::deserialize(&self.narrow_phase)?, + bincode::deserialize(&self.bodies)?, + bincode::deserialize(&self.colliders)?, + bincode::deserialize(&self.joints)?, + )) + } + + pub fn print_snapshot_len(&self) { + let total = self.broad_phase.len() + + self.narrow_phase.len() + + self.bodies.len() + + self.colliders.len() + + self.joints.len(); + println!("Snapshot length: {}B", total); + println!("|_ broad_phase: {}B", self.broad_phase.len()); + println!("|_ narrow_phase: {}B", self.narrow_phase.len()); + println!("|_ bodies: {}B", self.bodies.len()); + println!("|_ colliders: {}B", self.colliders.len()); + println!("|_ joints: {}B", self.joints.len()); + } +} + +pub struct PhysicsState { + pub broad_phase: BroadPhase, + pub narrow_phase: NarrowPhase, + pub bodies: RigidBodySet, + pub colliders: ColliderSet, + pub joints: JointSet, + pub pipeline: PhysicsPipeline, + pub query_pipeline: QueryPipeline, + pub integration_parameters: IntegrationParameters, + pub gravity: Vector, +} + +impl PhysicsState { + pub fn new() -> Self { + Self { + broad_phase: BroadPhase::new(), + narrow_phase: NarrowPhase::new(), + bodies: RigidBodySet::new(), + colliders: ColliderSet::new(), + joints: JointSet::new(), + pipeline: PhysicsPipeline::new(), + query_pipeline: QueryPipeline::new(), + integration_parameters: IntegrationParameters::default(), + gravity: Vector::y() * -9.81, + } + } +} + +pub struct PhysicsEvents { + pub contact_events: Receiver, + pub proximity_events: Receiver, +} + +impl PhysicsEvents { + pub fn poll_all(&self) { + while let Ok(_) = self.contact_events.try_recv() {} + while let Ok(_) = self.proximity_events.try_recv() {} + } +} -- cgit From b2db9e0a06490a644b12ce777fe27bbfa99c37b1 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 20 Dec 2020 14:57:33 +1100 Subject: cargo fmt --- src_testbed/physics/mod.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src_testbed/physics') diff --git a/src_testbed/physics/mod.rs b/src_testbed/physics/mod.rs index 09673d0..4b1923f 100644 --- a/src_testbed/physics/mod.rs +++ b/src_testbed/physics/mod.rs @@ -1,12 +1,8 @@ use crossbeam::channel::Receiver; +use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; +use rapier::geometry::{BroadPhase, ColliderSet, ContactEvent, NarrowPhase, ProximityEvent}; use rapier::math::Vector; use rapier::pipeline::{PhysicsPipeline, QueryPipeline}; -use rapier::dynamics::{ - IntegrationParameters, JointSet,RigidBodySet, -}; -use rapier::geometry::{ - BroadPhase, ColliderSet, ContactEvent, NarrowPhase, ProximityEvent, -}; pub struct PhysicsSnapshot { timestep_id: usize, -- cgit