From c56ebcc663d15ded86233d8caa11d00179e5ec16 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 17:58:37 +1100 Subject: [PATCH 01/31] refactor testbed to use harness --- examples2d/platform2.rs | 6 +- examples3d/platform3.rs | 6 +- src_testbed/harness/mod.rs | 39 +++-- src_testbed/harness/plugin.rs | 4 +- src_testbed/testbed.rs | 301 +++++++++++++--------------------- 5 files changed, 147 insertions(+), 209 deletions(-) diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 20b2e59..3b543db 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,13 +60,13 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.add_callback(move |_, physics, _, _, time| { + testbed.add_callback(move |_, physics, _, _, run_state| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); let dt = 0.016; - next_pos.translation.vector.y += (time * 5.0).sin() * dt; - next_pos.translation.vector.x += time.sin() * 5.0 * dt; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; if next_pos.translation.vector.x >= rad * 10.0 { next_pos.translation.vector.x -= dt; diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 0975f39..1e8d330 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -65,7 +65,7 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a callback to control the platform. */ let mut count = 0; - testbed.add_callback(move |_, physics, _, _, time| { + testbed.add_callback(move |_, physics, _, _, run_state| { count += 1; if count % 100 > 50 { return; @@ -75,8 +75,8 @@ pub fn init_world(testbed: &mut Testbed) { let mut next_pos = *platform.position(); let dt = 0.016; - next_pos.translation.vector.y += (time * 5.0).sin() * dt; - next_pos.translation.vector.z += time.sin() * 5.0 * dt; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; if next_pos.translation.vector.z >= rad * 10.0 { next_pos.translation.vector.z -= dt; diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index fa6c4c6..6467db5 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -7,24 +7,35 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; pub mod plugin; -pub struct HarnessState { +pub struct RunState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, pub timestep_id: usize, + pub time: f32, +} + +impl RunState { + pub fn new() -> Self { + Self { + #[cfg(feature = "parallel")] + thread_pool: rapier::rayon::ThreadPool, + timestep_id: 0, + time: 0.0 + } + } } pub struct Harness { - physics: PhysicsState, + pub physics: PhysicsState, max_steps: usize, callbacks: Callbacks, plugins: Vec>, - time: f32, events: PhysicsEvents, event_handler: ChannelEventCollector, - pub state: HarnessState, + pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { @@ -46,18 +57,13 @@ impl Harness { proximity_events: proximity_channel.1, }; let physics = PhysicsState::new(); - let state = HarnessState { - #[cfg(feature = "parallel")] - thread_pool, - timestep_id: 0, - }; + let state = RunState::new(); Self { physics, max_steps: 1000, callbacks: Vec::new(), plugins: Vec::new(), - time: 0.0, events, event_handler, state, @@ -101,7 +107,6 @@ impl Harness { self.physics.joints = joints; self.physics.broad_phase = BroadPhase::new(); self.physics.narrow_phase = NarrowPhase::new(); - self.time = 0.0; self.state.timestep_id = 0; self.physics.query_pipeline = QueryPipeline::new(); self.physics.pipeline = PhysicsPipeline::new(); @@ -113,7 +118,7 @@ impl Harness { } pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, + F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState, f32) + 'static, >( &mut self, callback: F, @@ -165,22 +170,22 @@ impl Harness { } for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state, self.time) + f(&mut self.physics, &self.events, &self.state, self.state.time) } for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.time) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.state.time) } self.events.poll_all(); - self.time += self.physics.integration_parameters.dt(); + self.state.time += self.physics.integration_parameters.dt(); + self.state.timestep_id += 1; } pub fn run(&mut self) { for _ in 0..self.max_steps { self.step(); - self.state.timestep_id += 1; } } } diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index 078219e..702efe2 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -1,4 +1,4 @@ -use crate::harness::HarnessState; +use crate::harness::RunState; use crate::physics::PhysicsEvents; use crate::PhysicsState; @@ -7,7 +7,7 @@ pub trait HarnessPlugin { &mut self, physics: &mut PhysicsState, events: &PhysicsEvents, - harness_state: &HarnessState, + harness_state: &RunState, t: f32, ); fn step(&mut self, physics: &mut PhysicsState); diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 4fac8e1..ddfb770 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -33,6 +33,7 @@ use crate::box2d_backend::Box2dWorld; use crate::nphysics_backend::NPhysicsWorld; #[cfg(all(feature = "dim3", feature = "other-backends"))] use crate::physx_backend::PhysxWorld; +use crate::harness::{Harness, RunState}; const RAPIER_BACKEND: usize = 0; const NPHYSICS_BACKEND: usize = 1; @@ -116,14 +117,13 @@ pub struct TestbedState { pub physx_use_two_friction_directions: bool, pub num_threads: usize, pub snapshot: Option, - #[cfg(feature = "parallel")] - pub thread_pool: rapier::rayon::ThreadPool, + // #[cfg(feature = "parallel")] + // pub thread_pool: rapier::rayon::ThreadPool, pub timestep_id: usize, } pub struct Testbed { builders: Vec<(&'static str, fn(&mut Testbed))>, - physics: PhysicsState, graphics: GraphicsManager, nsteps: usize, camera_locked: bool, // Used so that the camera can remain the same before and after we change backend or press the restart button. @@ -138,6 +138,7 @@ pub struct Testbed { event_handler: ChannelEventCollector, ui: Option, state: TestbedState, + harness: Harness, #[cfg(all(feature = "dim2", feature = "other-backends"))] box2d: Option, #[cfg(all(feature = "dim3", feature = "other-backends"))] @@ -147,7 +148,7 @@ pub struct Testbed { } type Callbacks = - Vec>; + Vec>; impl Testbed { pub fn new_empty() -> Testbed { @@ -201,6 +202,8 @@ impl Testbed { thread_pool, }; + let harness = Harness::new_empty(); + let contact_channel = crossbeam::channel::unbounded(); let proximity_channel = crossbeam::channel::unbounded(); let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); @@ -208,11 +211,9 @@ impl Testbed { contact_events: contact_channel.1, proximity_events: proximity_channel.1, }; - let physics = PhysicsState::new(); Testbed { builders: Vec::new(), - physics, callbacks: Vec::new(), plugins: Vec::new(), graphics, @@ -227,6 +228,7 @@ impl Testbed { event_handler, events, state, + harness, #[cfg(all(feature = "dim2", feature = "other-backends"))] box2d: None, #[cfg(all(feature = "dim3", feature = "other-backends"))] @@ -238,7 +240,7 @@ impl Testbed { pub fn new(bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) -> Self { let mut res = Self::new_empty(); - res.set_world(bodies, colliders, joints); + res.harness.set_world(bodies, colliders, joints); res } @@ -269,11 +271,11 @@ impl Testbed { } pub fn integration_parameters_mut(&mut self) -> &mut IntegrationParameters { - &mut self.physics.integration_parameters + &mut self.harness.physics.integration_parameters } pub fn physics_state_mut(&mut self) -> &mut PhysicsState { - &mut self.physics + &mut self.harness.physics } pub fn set_world(&mut self, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) { @@ -287,32 +289,30 @@ impl Testbed { joints: JointSet, gravity: Vector, ) { + println!("Num bodies: {}", bodies.len()); println!("Num joints: {}", joints.len()); - self.physics.gravity = gravity; - self.physics.bodies = bodies; - self.physics.colliders = colliders; - self.physics.joints = joints; - self.physics.broad_phase = BroadPhase::new(); - self.physics.narrow_phase = NarrowPhase::new(); + self.harness.set_world_with_gravity(bodies, colliders, joints, gravity); + self.state .action_flags .set(TestbedActionFlags::RESET_WORLD_GRAPHICS, true); + + //FIXME: remove time & state.timestep_id if appropriate self.time = 0.0; self.state.timestep_id = 0; self.state.highlighted_body = None; - self.physics.query_pipeline = QueryPipeline::new(); - self.physics.pipeline = PhysicsPipeline::new(); - self.physics.pipeline.counters.enable(); + + let physics = &self.harness.physics; #[cfg(all(feature = "dim2", feature = "other-backends"))] { if self.state.selected_backend == BOX2D_BACKEND { self.box2d = Some(Box2dWorld::from_rapier( - self.physics.gravity, - &self.physics.bodies, - &self.physics.colliders, - &self.physics.joints, + physics.gravity, + &physics.bodies, + &physics.colliders, + &physics.joints, )); } } @@ -323,13 +323,13 @@ impl Testbed { || self.state.selected_backend == PHYSX_BACKEND_TWO_FRICTION_DIR { self.physx = Some(PhysxWorld::from_rapier( - self.physics.gravity, - &self.physics.integration_parameters, - &self.physics.bodies, - &self.physics.colliders, - &self.physics.joints, + physics.gravity, + &physics.integration_parameters, + &physics.bodies, + &physics.colliders, + &physics.joints, self.state.selected_backend == PHYSX_BACKEND_TWO_FRICTION_DIR, - self.state.num_threads, + self.harness.state.num_threads, )); } } @@ -338,10 +338,10 @@ impl Testbed { { if self.state.selected_backend == NPHYSICS_BACKEND { self.nphysics = Some(NPhysicsWorld::from_rapier( - self.physics.gravity, - &self.physics.bodies, - &self.physics.colliders, - &self.physics.joints, + physics.gravity, + &physics.bodies, + &physics.colliders, + &physics.joints, )); } } @@ -438,7 +438,7 @@ impl Testbed { } pub fn add_callback< - F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, f32) + 'static, + F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, &RunState) + 'static, >( &mut self, callback: F, @@ -484,11 +484,11 @@ impl Testbed { && (backend_id == PHYSX_BACKEND_PATCH_FRICTION || backend_id == PHYSX_BACKEND_TWO_FRICTION_DIR) { - self.physics.integration_parameters.max_velocity_iterations = 1; - self.physics.integration_parameters.max_position_iterations = 4; + self.harness.physics.integration_parameters.max_velocity_iterations = 1; + self.harness.physics.integration_parameters.max_position_iterations = 4; } else { - self.physics.integration_parameters.max_velocity_iterations = 4; - self.physics.integration_parameters.max_position_iterations = 1; + self.harness.physics.integration_parameters.max_velocity_iterations = 4; + self.harness.physics.integration_parameters.max_position_iterations = 1; } // Init world. (builder.1)(&mut self); @@ -498,55 +498,19 @@ impl Testbed { { // FIXME: code duplicated from self.step() if self.state.selected_backend == RAPIER_BACKEND { - #[cfg(feature = "parallel")] - { - let physics = &mut self.physics; - let event_handler = &self.event_handler; - self.state.thread_pool.install(|| { - physics.pipeline.step( - &physics.gravity, - &physics.integration_parameters, - &mut physics.broad_phase, - &mut physics.narrow_phase, - &mut physics.bodies, - &mut physics.colliders, - &mut physics.joints, - None, - None, - event_handler, - ); - }); - } - - #[cfg(not(feature = "parallel"))] - self.physics.pipeline.step( - &self.physics.gravity, - &self.physics.integration_parameters, - &mut self.physics.broad_phase, - &mut self.physics.narrow_phase, - &mut self.physics.bodies, - &mut self.physics.colliders, - &mut self.physics.joints, - None, - None, - &self.event_handler, - ); - - self.physics - .query_pipeline - .update(&self.physics.bodies, &self.physics.colliders); + self.harness.step(); } #[cfg(all(feature = "dim2", feature = "other-backends"))] { if self.state.selected_backend == BOX2D_BACKEND { self.box2d.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut self.harness.physics.pipeline.counters, + &physics.integration_parameters, ); self.box2d.as_mut().unwrap().sync( - &mut self.physics.bodies, - &mut self.physics.colliders, + &mut self.harness.physics.bodies, + &mut self.harness.physics.colliders, ); } } @@ -558,12 +522,12 @@ impl Testbed { { // println!("Step"); self.physx.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut self.harness.physics.pipeline.counters, + &physics.integration_parameters, ); self.physx.as_mut().unwrap().sync( - &mut self.physics.bodies, - &mut self.physics.colliders, + &mut self.harness.physics.bodies, + &mut self.harness.physics.colliders, ); } } @@ -572,12 +536,12 @@ impl Testbed { { if self.state.selected_backend == NPHYSICS_BACKEND { self.nphysics.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut self.harness.physics.pipeline.counters, + &physics.integration_parameters, ); self.nphysics.as_mut().unwrap().sync( - &mut self.physics.bodies, - &mut self.physics.colliders, + &mut self.harness.physics.bodies, + &mut self.harness.physics.colliders, ); } } @@ -585,7 +549,7 @@ impl Testbed { // Skip the first update. if k > 0 { - timings.push(self.physics.pipeline.counters.step_time.time()); + timings.push(self.harness.physics.pipeline.counters.step_time.time()); } } results.push(timings); @@ -639,8 +603,7 @@ impl Testbed { .set(TestbedActionFlags::EXAMPLE_CHANGED, true), WindowEvent::Key(Key::C, Action::Release, _) => { // Delete 1 collider of 10% of the remaining dynamic bodies. - let mut colliders: Vec<_> = self - .physics + let mut colliders: Vec<_> = self.harness.physics .bodies .iter() .filter(|e| e.1.is_dynamic()) @@ -651,14 +614,17 @@ impl Testbed { let num_to_delete = (colliders.len() / 10).max(1); for to_delete in &colliders[..num_to_delete] { - self.physics + self + .harness + .physics .colliders - .remove(to_delete[0], &mut self.physics.bodies, true); + .remove(to_delete[0], &mut self.harness.physics.bodies, true); } } WindowEvent::Key(Key::D, Action::Release, _) => { // Delete 10% of the remaining dynamic bodies. let dynamic_bodies: Vec<_> = self + .harness .physics .bodies .iter() @@ -667,21 +633,23 @@ impl Testbed { .collect(); let num_to_delete = (dynamic_bodies.len() / 10).max(1); for to_delete in &dynamic_bodies[..num_to_delete] { - self.physics.bodies.remove( + self.harness.physics.bodies.remove( *to_delete, - &mut self.physics.colliders, - &mut self.physics.joints, + &mut self.harness.physics.colliders, + &mut self.harness.physics.joints, ); } } WindowEvent::Key(Key::J, Action::Release, _) => { // Delete 10% of the remaining joints. - let joints: Vec<_> = self.physics.joints.iter().map(|e| e.0).collect(); + let joints: Vec<_> = self.harness.physics.joints.iter().map(|e| e.0).collect(); let num_to_delete = (joints.len() / 10).max(1); for to_delete in &joints[..num_to_delete] { - self.physics + self + .harness + .physics .joints - .remove(*to_delete, &mut self.physics.bodies, true); + .remove(*to_delete, &mut self.harness.physics.bodies, true); } } WindowEvent::CursorPos(x, y, _) => { @@ -1028,15 +996,16 @@ impl Testbed { .camera() .unproject(&self.cursor_pos, &na::convert(size)); let ray = Ray::new(pos, dir); - let hit = self.physics.query_pipeline.cast_ray( - &self.physics.colliders, + let physics = &self.harness.physics; + let hit = physics.query_pipeline.cast_ray( + &physics.colliders, &ray, f32::MAX, InteractionGroups::all(), ); if let Some((_, collider, _)) = hit { - if self.physics.bodies[collider.parent()].is_dynamic() { + if physics.bodies[collider.parent()].is_dynamic() { self.state.highlighted_body = Some(collider.parent()); for node in self.graphics.body_nodes_mut(collider.parent()).unwrap() { node.select() @@ -1075,11 +1044,13 @@ impl State for Testbed { if let Some(ui) = &mut self.ui { ui.update( window, - &mut self.physics.integration_parameters, + &mut self.harness.physics.integration_parameters, &mut self.state, ); } + // let physics = &self.harness.physics; + // Handle UI actions. { let backend_changed = self @@ -1123,7 +1094,7 @@ impl State for Testbed { self.clear(window); if self.state.selected_example != prev_example { - self.physics.integration_parameters = IntegrationParameters::default(); + self.harness.physics.integration_parameters = IntegrationParameters::default(); } self.builders[self.state.selected_example].1(self); @@ -1141,11 +1112,11 @@ impl State for Testbed { .set(TestbedActionFlags::TAKE_SNAPSHOT, false); self.state.snapshot = PhysicsSnapshot::new( self.state.timestep_id, - &self.physics.broad_phase, - &self.physics.narrow_phase, - &self.physics.bodies, - &self.physics.colliders, - &self.physics.joints, + &self.harness.physics.broad_phase, + &self.harness.physics.narrow_phase, + &self.harness.physics.bodies, + &self.harness.physics.colliders, + &self.harness.physics.joints, ) .ok(); @@ -1154,6 +1125,7 @@ impl State for Testbed { } } + if self .state .action_flags @@ -1172,8 +1144,8 @@ impl State for Testbed { } self.set_world(w.3, w.4, w.5); - self.physics.broad_phase = w.1; - self.physics.narrow_phase = w.2; + self.harness.physics.broad_phase = w.1; + self.harness.physics.narrow_phase = w.2; self.state.timestep_id = w.0; } } @@ -1187,12 +1159,12 @@ impl State for Testbed { self.state .action_flags .set(TestbedActionFlags::RESET_WORLD_GRAPHICS, false); - for (handle, _) in self.physics.bodies.iter() { + for (handle, _) in self.harness.physics.bodies.iter() { self.graphics.add( window, handle, - &self.physics.bodies, - &self.physics.colliders, + &self.harness.physics.bodies, + &self.harness.physics.colliders, ); } @@ -1207,7 +1179,7 @@ impl State for Testbed { != self.state.flags.contains(TestbedStateFlags::WIREFRAME) { self.graphics.toggle_wireframe_mode( - &self.physics.colliders, + &self.harness.physics.colliders, self.state.flags.contains(TestbedStateFlags::WIREFRAME), ) } @@ -1216,11 +1188,11 @@ impl State for Testbed { != self.state.flags.contains(TestbedStateFlags::SLEEP) { if self.state.flags.contains(TestbedStateFlags::SLEEP) { - for (_, mut body) in self.physics.bodies.iter_mut() { + for (_, mut body) in self.harness.physics.bodies.iter_mut() { body.activation.threshold = ActivationStatus::default_threshold(); } } else { - for (_, mut body) in self.physics.bodies.iter_mut() { + for (_, mut body) in self.harness.physics.bodies.iter_mut() { body.wake_up(true); body.activation.threshold = -1.0; } @@ -1233,7 +1205,7 @@ impl State for Testbed { .contains(TestbedStateFlags::SUB_STEPPING) != self.state.flags.contains(TestbedStateFlags::SUB_STEPPING) { - self.physics.integration_parameters.return_after_ccd_substep = + self.harness.physics.integration_parameters.return_after_ccd_substep = self.state.flags.contains(TestbedStateFlags::SUB_STEPPING); } @@ -1285,46 +1257,11 @@ impl State for Testbed { for _ in 0..self.nsteps { self.state.timestep_id += 1; if self.state.selected_backend == RAPIER_BACKEND { - #[cfg(feature = "parallel")] - { - let physics = &mut self.physics; - let event_handler = &self.event_handler; - self.state.thread_pool.install(|| { - physics.pipeline.step( - &physics.gravity, - &physics.integration_parameters, - &mut physics.broad_phase, - &mut physics.narrow_phase, - &mut physics.bodies, - &mut physics.colliders, - &mut physics.joints, - None, - None, - event_handler, - ); - }); - } - - #[cfg(not(feature = "parallel"))] - self.physics.pipeline.step( - &self.physics.gravity, - &self.physics.integration_parameters, - &mut self.physics.broad_phase, - &mut self.physics.narrow_phase, - &mut self.physics.bodies, - &mut self.physics.colliders, - &mut self.physics.joints, - None, - None, - &self.event_handler, - ); - - self.physics - .query_pipeline - .update(&self.physics.bodies, &self.physics.colliders); + self.harness.step(); + //FIXME: this should probably be delegated to harness plugins for plugin in &mut self.plugins { - plugin.step(&mut self.physics) + plugin.step(&mut self.harness.physics) } } @@ -1332,13 +1269,13 @@ impl State for Testbed { { if self.state.selected_backend == BOX2D_BACKEND { self.box2d.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut physics.pipeline.counters, + &physics.integration_parameters, ); self.box2d .as_mut() .unwrap() - .sync(&mut self.physics.bodies, &mut self.physics.colliders); + .sync(&mut physics.bodies, &mut physics.colliders); } } @@ -1349,13 +1286,13 @@ impl State for Testbed { { // println!("Step"); self.physx.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut physics.pipeline.counters, + &physics.integration_parameters, ); self.physx .as_mut() .unwrap() - .sync(&mut self.physics.bodies, &mut self.physics.colliders); + .sync(&mut physics.bodies, &mut physics.colliders); } } @@ -1363,28 +1300,21 @@ impl State for Testbed { { if self.state.selected_backend == NPHYSICS_BACKEND { self.nphysics.as_mut().unwrap().step( - &mut self.physics.pipeline.counters, - &self.physics.integration_parameters, + &mut physics.pipeline.counters, + &physics.integration_parameters, ); self.nphysics .as_mut() .unwrap() - .sync(&mut self.physics.bodies, &mut self.physics.colliders); + .sync(&mut physics.bodies, &mut physics.colliders); } } - for f in &mut self.callbacks { - f( - window, - &mut self.physics, - &self.events, - &mut self.graphics, - self.time, - ) - } - + // FIXME: should this be handled by harness plugins? for plugin in &mut self.plugins { - plugin.run_callbacks(window, &mut self.physics, self.time) + { + plugin.run_callbacks(window, &mut self.harness.physics, self.time); + } } self.events.poll_all(); @@ -1396,20 +1326,22 @@ impl State for Testbed { // #[cfg(feature = "log")] // debug!("{}", self.world.counters); // } - self.time += self.physics.integration_parameters.dt(); + self.time += self.harness.physics.integration_parameters.dt(); } } - self.highlight_hovered_body(window); + // FIXME: this is causing errors, but should be put back in some how + // self.highlight_hovered_body(window); + let physics = &self.harness.physics; self.graphics - .draw(&self.physics.bodies, &self.physics.colliders, window); + .draw(&physics.bodies, &physics.colliders, window); for plugin in &mut self.plugins { plugin.draw(); } if self.state.flags.contains(TestbedStateFlags::CONTACT_POINTS) { - draw_contacts(window, &self.physics.narrow_phase, &self.physics.colliders); + draw_contacts(window, &physics.narrow_phase, &physics.colliders); } if self.state.running == RunMode::Step { @@ -1421,7 +1353,7 @@ impl State for Testbed { } let color = Point3::new(0.0, 0.0, 0.0); - let counters = self.physics.pipeline.counters; + let counters = physics.pipeline.counters; let mut profile = String::new(); if self.state.flags.contains(TestbedStateFlags::PROFILE) { @@ -1472,11 +1404,12 @@ CCD: {:.2}ms if self.state.flags.contains(TestbedStateFlags::DEBUG) { let t = instant::now(); - let bf = bincode::serialize(&self.physics.broad_phase).unwrap(); - let nf = bincode::serialize(&self.physics.narrow_phase).unwrap(); - let bs = bincode::serialize(&self.physics.bodies).unwrap(); - let cs = bincode::serialize(&self.physics.colliders).unwrap(); - let js = bincode::serialize(&self.physics.joints).unwrap(); + let physics = &self.harness.physics; + let bf = bincode::serialize(&physics.broad_phase).unwrap(); + let nf = bincode::serialize(&physics.narrow_phase).unwrap(); + let bs = bincode::serialize(&physics.bodies).unwrap(); + let cs = bincode::serialize(&physics.colliders).unwrap(); + let js = bincode::serialize(&physics.joints).unwrap(); let serialization_time = instant::now() - t; let hash_bf = md5::compute(&bf); let hash_nf = md5::compute(&nf); From fbde6847df10e0a5a9a5eac0912abaa641106f88 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 18:02:19 +1100 Subject: [PATCH 02/31] remove unused physics var --- src_testbed/testbed.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index ddfb770..778dd6e 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -21,11 +21,11 @@ use na::{self, Point2, Point3, Vector3}; use rapier::dynamics::{ ActivationStatus, IntegrationParameters, JointSet, RigidBodyHandle, RigidBodySet, }; -use rapier::geometry::{BroadPhase, ColliderHandle, ColliderSet, NarrowPhase}; +use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase}; #[cfg(feature = "dim3")] use rapier::geometry::{InteractionGroups, Ray}; use rapier::math::Vector; -use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; +use rapier::pipeline::{ChannelEventCollector}; #[cfg(all(feature = "dim2", feature = "other-backends"))] use crate::box2d_backend::Box2dWorld; @@ -303,8 +303,6 @@ impl Testbed { self.state.timestep_id = 0; self.state.highlighted_body = None; - let physics = &self.harness.physics; - #[cfg(all(feature = "dim2", feature = "other-backends"))] { if self.state.selected_backend == BOX2D_BACKEND { From 0a0c79a36bb28f282fb4ca6d057e7de3e3e7e34c Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 18:25:35 +1100 Subject: [PATCH 03/31] remove time & timestep_id from testbed side of things remove events code --- src_testbed/testbed.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 778dd6e..f89346b 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -117,9 +117,6 @@ pub struct TestbedState { pub physx_use_two_friction_directions: bool, pub num_threads: usize, pub snapshot: Option, - // #[cfg(feature = "parallel")] - // pub thread_pool: rapier::rayon::ThreadPool, - pub timestep_id: usize, } pub struct Testbed { @@ -129,13 +126,10 @@ pub struct Testbed { camera_locked: bool, // Used so that the camera can remain the same before and after we change backend or press the restart button. callbacks: Callbacks, plugins: Vec>, - time: f32, hide_counters: bool, // persistant_contacts: HashMap, font: Rc, cursor_pos: Point2, - events: PhysicsEvents, - event_handler: ChannelEventCollector, ui: Option, state: TestbedState, harness: Harness, @@ -194,7 +188,6 @@ impl Testbed { backend_names, example_names: Vec::new(), selected_example: 0, - timestep_id: 0, selected_backend: RAPIER_BACKEND, physx_use_two_friction_directions: true, num_threads, @@ -219,14 +212,11 @@ impl Testbed { graphics, nsteps: 1, camera_locked: false, - time: 0.0, hide_counters: true, // persistant_contacts: HashMap::new(), font: Font::default(), cursor_pos: Point2::new(0.0f32, 0.0), ui, - event_handler, - events, state, harness, #[cfg(all(feature = "dim2", feature = "other-backends"))] @@ -298,9 +288,6 @@ impl Testbed { .action_flags .set(TestbedActionFlags::RESET_WORLD_GRAPHICS, true); - //FIXME: remove time & state.timestep_id if appropriate - self.time = 0.0; - self.state.timestep_id = 0; self.state.highlighted_body = None; #[cfg(all(feature = "dim2", feature = "other-backends"))] @@ -462,6 +449,7 @@ impl Testbed { } } + //FIXME: move this to dedicated benchmarking code if benchmark_mode { use std::fs::File; use std::io::{BufWriter, Write}; @@ -494,7 +482,6 @@ impl Testbed { let mut timings = Vec::new(); for k in 0..=NUM_ITERS { { - // FIXME: code duplicated from self.step() if self.state.selected_backend == RAPIER_BACKEND { self.harness.step(); } @@ -1109,7 +1096,7 @@ impl State for Testbed { .action_flags .set(TestbedActionFlags::TAKE_SNAPSHOT, false); self.state.snapshot = PhysicsSnapshot::new( - self.state.timestep_id, + self.harness.state.timestep_id, &self.harness.physics.broad_phase, &self.harness.physics.narrow_phase, &self.harness.physics.bodies, @@ -1144,7 +1131,8 @@ impl State for Testbed { self.set_world(w.3, w.4, w.5); self.harness.physics.broad_phase = w.1; self.harness.physics.narrow_phase = w.2; - self.state.timestep_id = w.0; + //FIXME: not completely sure this is valid + self.harness.state.timestep_id = w.0; } } } @@ -1253,7 +1241,6 @@ impl State for Testbed { if self.state.running != RunMode::Stop { for _ in 0..self.nsteps { - self.state.timestep_id += 1; if self.state.selected_backend == RAPIER_BACKEND { self.harness.step(); @@ -1311,12 +1298,10 @@ impl State for Testbed { // FIXME: should this be handled by harness plugins? for plugin in &mut self.plugins { { - plugin.run_callbacks(window, &mut self.harness.physics, self.time); + plugin.run_callbacks(window, &mut self.harness.physics, self.harness.state.time); } } - self.events.poll_all(); - // if true { // // !self.hide_counters { // #[cfg(not(feature = "log"))] @@ -1324,7 +1309,6 @@ impl State for Testbed { // #[cfg(feature = "log")] // debug!("{}", self.world.counters); // } - self.time += self.harness.physics.integration_parameters.dt(); } } @@ -1425,7 +1409,7 @@ Hashes at frame: {} |_ Joints [{:.1}KB]: {:?}"#, profile, serialization_time, - self.state.timestep_id, + self.harness.state.timestep_id, bf.len() as f32 / 1000.0, hash_bf, nf.len() as f32 / 1000.0, From 2de41ee5e30df5e67c98427100304a5b4ded085f Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 18:31:59 +1100 Subject: [PATCH 04/31] change HarnessPlugin trait to add run_state to the step trait method --- src_testbed/harness/mod.rs | 2 +- src_testbed/harness/plugin.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 6467db5..dd49fe6 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -166,7 +166,7 @@ impl Harness { .update(&self.physics.bodies, &self.physics.colliders); for plugin in &mut self.plugins { - plugin.step(&mut self.physics) + plugin.step(&mut self.physics, &self.state) } for f in &mut self.callbacks { diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index 702efe2..ef0ebc4 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -10,6 +10,6 @@ pub trait HarnessPlugin { harness_state: &RunState, t: f32, ); - fn step(&mut self, physics: &mut PhysicsState); + fn step(&mut self, physics: &mut PhysicsState, run_state: &RunState); fn profiling_string(&self) -> String; } From bd6e46cdd928eca854248e95fe04efdc41010248 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 18:32:07 +1100 Subject: [PATCH 05/31] remove event code --- src_testbed/testbed.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index f89346b..6ae1351 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -197,14 +197,6 @@ impl Testbed { let harness = Harness::new_empty(); - let contact_channel = crossbeam::channel::unbounded(); - let proximity_channel = crossbeam::channel::unbounded(); - let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); - let events = PhysicsEvents { - contact_events: contact_channel.1, - proximity_events: proximity_channel.1, - }; - Testbed { builders: Vec::new(), callbacks: Vec::new(), From cbe6baced5c36c39a0bd3cb8977c6097249ec5cb Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 19:15:34 +1100 Subject: [PATCH 06/31] remove fixme comments --- src_testbed/testbed.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 6ae1351..20a9e0a 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -396,6 +396,7 @@ impl Testbed { } fn clear(&mut self, window: &mut Window) { + //FIXME: do we need to do this still, after moving to harness code? self.callbacks.clear(); // self.persistant_contacts.clear(); // self.state.grabbed_object = None; @@ -1236,7 +1237,6 @@ impl State for Testbed { if self.state.selected_backend == RAPIER_BACKEND { self.harness.step(); - //FIXME: this should probably be delegated to harness plugins for plugin in &mut self.plugins { plugin.step(&mut self.harness.physics) } @@ -1287,7 +1287,6 @@ impl State for Testbed { } } - // FIXME: should this be handled by harness plugins? for plugin in &mut self.plugins { { plugin.run_callbacks(window, &mut self.harness.physics, self.harness.state.time); From baccfff4cde4e70f79bef1d061a4bdea9a845546 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 19:17:06 +1100 Subject: [PATCH 07/31] reenable self.highlight_hovered_body --- src_testbed/testbed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 20a9e0a..46cb64f 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1303,8 +1303,8 @@ impl State for Testbed { } } - // FIXME: this is causing errors, but should be put back in some how - // self.highlight_hovered_body(window); + self.highlight_hovered_body(window); + let physics = &self.harness.physics; self.graphics .draw(&physics.bodies, &physics.colliders, window); From b1d0dc006d39faeb4a3ef85acc62f2981d26a85b Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 20:16:11 +1100 Subject: [PATCH 08/31] cargo fmt --- src_testbed/harness/mod.rs | 20 +++++++---- src_testbed/testbed.rs | 72 +++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index dd49fe6..c6f682c 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -20,7 +20,7 @@ impl RunState { #[cfg(feature = "parallel")] thread_pool: rapier::rayon::ThreadPool, timestep_id: 0, - time: 0.0 + time: 0.0, } } } @@ -117,9 +117,7 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState, f32) + 'static, - >( + pub fn add_callback( &mut self, callback: F, ) { @@ -170,11 +168,21 @@ impl Harness { } for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state, self.state.time) + f( + &mut self.physics, + &self.events, + &self.state, + self.state.time, + ) } for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.state.time) + plugin.run_callbacks( + &mut self.physics, + &self.events, + &self.state, + self.state.time, + ) } self.events.poll_all(); diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 46cb64f..b7a6974 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -25,15 +25,15 @@ use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase}; #[cfg(feature = "dim3")] use rapier::geometry::{InteractionGroups, Ray}; use rapier::math::Vector; -use rapier::pipeline::{ChannelEventCollector}; +use rapier::pipeline::ChannelEventCollector; #[cfg(all(feature = "dim2", feature = "other-backends"))] use crate::box2d_backend::Box2dWorld; +use crate::harness::{Harness, RunState}; #[cfg(feature = "other-backends")] use crate::nphysics_backend::NPhysicsWorld; #[cfg(all(feature = "dim3", feature = "other-backends"))] use crate::physx_backend::PhysxWorld; -use crate::harness::{Harness, RunState}; const RAPIER_BACKEND: usize = 0; const NPHYSICS_BACKEND: usize = 1; @@ -141,8 +141,9 @@ pub struct Testbed { nphysics: Option, } -type Callbacks = - Vec>; +type Callbacks = Vec< + Box, +>; impl Testbed { pub fn new_empty() -> Testbed { @@ -271,10 +272,10 @@ impl Testbed { joints: JointSet, gravity: Vector, ) { - println!("Num bodies: {}", bodies.len()); println!("Num joints: {}", joints.len()); - self.harness.set_world_with_gravity(bodies, colliders, joints, gravity); + self.harness + .set_world_with_gravity(bodies, colliders, joints, gravity); self.state .action_flags @@ -416,7 +417,8 @@ impl Testbed { } pub fn add_callback< - F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, &RunState) + 'static, + F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, &RunState) + + 'static, >( &mut self, callback: F, @@ -463,11 +465,23 @@ impl Testbed { && (backend_id == PHYSX_BACKEND_PATCH_FRICTION || backend_id == PHYSX_BACKEND_TWO_FRICTION_DIR) { - self.harness.physics.integration_parameters.max_velocity_iterations = 1; - self.harness.physics.integration_parameters.max_position_iterations = 4; + self.harness + .physics + .integration_parameters + .max_velocity_iterations = 1; + self.harness + .physics + .integration_parameters + .max_position_iterations = 4; } else { - self.harness.physics.integration_parameters.max_velocity_iterations = 4; - self.harness.physics.integration_parameters.max_position_iterations = 1; + self.harness + .physics + .integration_parameters + .max_velocity_iterations = 4; + self.harness + .physics + .integration_parameters + .max_position_iterations = 1; } // Init world. (builder.1)(&mut self); @@ -581,7 +595,9 @@ impl Testbed { .set(TestbedActionFlags::EXAMPLE_CHANGED, true), WindowEvent::Key(Key::C, Action::Release, _) => { // Delete 1 collider of 10% of the remaining dynamic bodies. - let mut colliders: Vec<_> = self.harness.physics + let mut colliders: Vec<_> = self + .harness + .physics .bodies .iter() .filter(|e| e.1.is_dynamic()) @@ -592,11 +608,11 @@ impl Testbed { let num_to_delete = (colliders.len() / 10).max(1); for to_delete in &colliders[..num_to_delete] { - self - .harness - .physics - .colliders - .remove(to_delete[0], &mut self.harness.physics.bodies, true); + self.harness.physics.colliders.remove( + to_delete[0], + &mut self.harness.physics.bodies, + true, + ); } } WindowEvent::Key(Key::D, Action::Release, _) => { @@ -623,11 +639,11 @@ impl Testbed { let joints: Vec<_> = self.harness.physics.joints.iter().map(|e| e.0).collect(); let num_to_delete = (joints.len() / 10).max(1); for to_delete in &joints[..num_to_delete] { - self - .harness - .physics - .joints - .remove(*to_delete, &mut self.harness.physics.bodies, true); + self.harness.physics.joints.remove( + *to_delete, + &mut self.harness.physics.bodies, + true, + ); } } WindowEvent::CursorPos(x, y, _) => { @@ -1103,7 +1119,6 @@ impl State for Testbed { } } - if self .state .action_flags @@ -1184,7 +1199,10 @@ impl State for Testbed { .contains(TestbedStateFlags::SUB_STEPPING) != self.state.flags.contains(TestbedStateFlags::SUB_STEPPING) { - self.harness.physics.integration_parameters.return_after_ccd_substep = + self.harness + .physics + .integration_parameters + .return_after_ccd_substep = self.state.flags.contains(TestbedStateFlags::SUB_STEPPING); } @@ -1289,7 +1307,11 @@ impl State for Testbed { for plugin in &mut self.plugins { { - plugin.run_callbacks(window, &mut self.harness.physics, self.harness.state.time); + plugin.run_callbacks( + window, + &mut self.harness.physics, + self.harness.state.time, + ); } } From 5ffacf0a14cda067c7e362e33303bcde8b76e68e Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 20:56:11 +1100 Subject: [PATCH 09/31] pass run_state instead of time to TestbedPlugin::run_callbacks --- src_testbed/plugin.rs | 3 ++- src_testbed/testbed.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src_testbed/plugin.rs b/src_testbed/plugin.rs index 872cdf5..a807dde 100644 --- a/src_testbed/plugin.rs +++ b/src_testbed/plugin.rs @@ -1,11 +1,12 @@ use crate::physics::PhysicsState; use kiss3d::window::Window; use na::Point3; +use crate::harness::RunState; pub trait TestbedPlugin { fn init_graphics(&mut self, window: &mut Window, gen_color: &mut dyn FnMut() -> Point3); fn clear_graphics(&mut self, window: &mut Window); - fn run_callbacks(&mut self, window: &mut Window, physics: &mut PhysicsState, t: f32); + fn run_callbacks(&mut self, window: &mut Window, physics: &mut PhysicsState, run_state: &RunState); fn step(&mut self, physics: &mut PhysicsState); fn draw(&mut self); fn profiling_string(&self) -> String; diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index b7a6974..d87c565 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1310,7 +1310,7 @@ impl State for Testbed { plugin.run_callbacks( window, &mut self.harness.physics, - self.harness.state.time, + &self.harness.state, ); } } From 70d05cc63f24945d5d59c6e14aa6f36c36304a84 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:24:04 +1100 Subject: [PATCH 10/31] fix typo with creating threadpool in RunState --- src_testbed/harness/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index c6f682c..379a42c 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -18,7 +18,10 @@ impl RunState { pub fn new() -> Self { Self { #[cfg(feature = "parallel")] - thread_pool: rapier::rayon::ThreadPool, + thread_pool: rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(), timestep_id: 0, time: 0.0, } From 975f0d149f84068e46c7bb8bd6514378aa6aeaf4 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:25:49 +1100 Subject: [PATCH 11/31] add num_threads back in too --- src_testbed/harness/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 379a42c..228ffe0 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -16,12 +16,18 @@ pub struct RunState { impl RunState { pub fn new() -> Self { + #[cfg(feature = "parallel")] + let num_threads = num_cpus::get_physical(); + + #[cfg(feature = "parallel")] + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + Self { #[cfg(feature = "parallel")] - thread_pool: rapier::rayon::ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .unwrap(), + thread_pool: thread_pool, timestep_id: 0, time: 0.0, } From 31032ab96978a32811319bb911b4e8e905918fe7 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:27:57 +1100 Subject: [PATCH 12/31] remove thread code completely from testbed --- src_testbed/testbed.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index d87c565..bb663e7 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -162,17 +162,6 @@ impl Testbed { #[cfg(all(feature = "dim3", feature = "other-backends"))] backend_names.push("physx (two friction dir)"); - #[cfg(feature = "parallel")] - let num_threads = num_cpus::get_physical(); - #[cfg(not(feature = "parallel"))] - let num_threads = 1; - - #[cfg(feature = "parallel")] - let thread_pool = rapier::rayon::ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .unwrap(); - let state = TestbedState { running: RunMode::Running, draw_colls: false, @@ -192,8 +181,6 @@ impl Testbed { selected_backend: RAPIER_BACKEND, physx_use_two_friction_directions: true, num_threads, - #[cfg(feature = "parallel")] - thread_pool, }; let harness = Harness::new_empty(); From 5fce09cb52f7c24662650a998d279433a324e517 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:42:46 +1100 Subject: [PATCH 13/31] rework some threading code with the ui --- src_testbed/harness/mod.rs | 4 ++++ src_testbed/testbed.rs | 3 +-- src_testbed/ui.rs | 12 +++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 228ffe0..916823e 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -10,6 +10,8 @@ pub mod plugin; pub struct RunState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, + #[cfg(feature = "parallel")] + pub num_threads: usize, pub timestep_id: usize, pub time: f32, } @@ -28,6 +30,8 @@ impl RunState { Self { #[cfg(feature = "parallel")] thread_pool: thread_pool, + #[cfg(feature = "parallel")] + num_threads, timestep_id: 0, time: 0.0, } diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index bb663e7..949afe7 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -115,7 +115,6 @@ pub struct TestbedState { pub selected_example: usize, pub selected_backend: usize, pub physx_use_two_friction_directions: bool, - pub num_threads: usize, pub snapshot: Option, } @@ -180,7 +179,6 @@ impl Testbed { selected_example: 0, selected_backend: RAPIER_BACKEND, physx_use_two_friction_directions: true, - num_threads, }; let harness = Harness::new_empty(); @@ -1027,6 +1025,7 @@ impl State for Testbed { window, &mut self.harness.physics.integration_parameters, &mut self.state, + &mut self.harness.state ); } diff --git a/src_testbed/ui.rs b/src_testbed/ui.rs index f88db9d..f72ca73 100644 --- a/src_testbed/ui.rs +++ b/src_testbed/ui.rs @@ -3,6 +3,7 @@ use kiss3d::window::Window; use rapier::dynamics::IntegrationParameters; use crate::testbed::{RunMode, TestbedActionFlags, TestbedState, TestbedStateFlags}; +use crate::harness::RunState; const SIDEBAR_W: f64 = 200.0; const ELEMENT_W: f64 = SIDEBAR_W - 20.0; @@ -98,6 +99,7 @@ impl TestbedUi { window: &mut Window, integration_parameters: &mut IntegrationParameters, state: &mut TestbedState, + run_state: &mut RunState, ) { let ui_root = window.conrod_ui().window; let mut ui = window.conrod_ui_mut().set_widgets(); @@ -254,7 +256,7 @@ impl TestbedUi { let curr_vel_iters = integration_parameters.max_velocity_iterations; let curr_pos_iters = integration_parameters.max_position_iterations; #[cfg(feature = "parallel")] - let curr_num_threads = state.num_threads; + let curr_num_threads = run_state.num_threads; let curr_max_ccd_substeps = integration_parameters.max_ccd_substeps; let curr_min_island_size = integration_parameters.min_island_size; let curr_warmstart_coeff = integration_parameters.warmstart_coeff; @@ -305,10 +307,10 @@ impl TestbedUi { .w_h(ELEMENT_W, ELEMENT_H) .set(self.ids.slider_num_threads, &mut ui) { - if state.num_threads != val as usize { - state.num_threads = val as usize; - state.thread_pool = rapier::rayon::ThreadPoolBuilder::new() - .num_threads(state.num_threads) + if run_state.num_threads != val as usize { + run_state.num_threads = val as usize; + run_state.thread_pool = rapier::rayon::ThreadPoolBuilder::new() + .num_threads(run_state.num_threads) .build() .unwrap(); } From caf9d71bc7ae069d1d80194164f54b1132e3b2ad Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:45:00 +1100 Subject: [PATCH 14/31] cargo fmt --- src_testbed/harness/mod.rs | 4 ++-- src_testbed/plugin.rs | 9 +++++++-- src_testbed/testbed.rs | 2 +- src_testbed/ui.rs | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 916823e..45e52dc 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -19,10 +19,10 @@ pub struct RunState { impl RunState { pub fn new() -> Self { #[cfg(feature = "parallel")] - let num_threads = num_cpus::get_physical(); + let num_threads = num_cpus::get_physical(); #[cfg(feature = "parallel")] - let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() .num_threads(num_threads) .build() .unwrap(); diff --git a/src_testbed/plugin.rs b/src_testbed/plugin.rs index a807dde..877e7f6 100644 --- a/src_testbed/plugin.rs +++ b/src_testbed/plugin.rs @@ -1,12 +1,17 @@ +use crate::harness::RunState; use crate::physics::PhysicsState; use kiss3d::window::Window; use na::Point3; -use crate::harness::RunState; pub trait TestbedPlugin { fn init_graphics(&mut self, window: &mut Window, gen_color: &mut dyn FnMut() -> Point3); fn clear_graphics(&mut self, window: &mut Window); - fn run_callbacks(&mut self, window: &mut Window, physics: &mut PhysicsState, run_state: &RunState); + fn run_callbacks( + &mut self, + window: &mut Window, + physics: &mut PhysicsState, + run_state: &RunState, + ); fn step(&mut self, physics: &mut PhysicsState); fn draw(&mut self); fn profiling_string(&self) -> String; diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 949afe7..f02380c 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1025,7 +1025,7 @@ impl State for Testbed { window, &mut self.harness.physics.integration_parameters, &mut self.state, - &mut self.harness.state + &mut self.harness.state, ); } diff --git a/src_testbed/ui.rs b/src_testbed/ui.rs index f72ca73..1b9084c 100644 --- a/src_testbed/ui.rs +++ b/src_testbed/ui.rs @@ -2,8 +2,8 @@ use kiss3d::conrod::{self, Borderable, Colorable, Labelable, Positionable, Sizea use kiss3d::window::Window; use rapier::dynamics::IntegrationParameters; -use crate::testbed::{RunMode, TestbedActionFlags, TestbedState, TestbedStateFlags}; use crate::harness::RunState; +use crate::testbed::{RunMode, TestbedActionFlags, TestbedState, TestbedStateFlags}; const SIDEBAR_W: f64 = 200.0; const ELEMENT_W: f64 = SIDEBAR_W - 20.0; From 75c80bff5fe44752eeaa8f4aaa4ade2ee54b4bd3 Mon Sep 17 00:00:00 2001 From: rezural <69941255+rezural@users.noreply.github.com> Date: Thu, 31 Dec 2020 11:47:02 +1100 Subject: [PATCH 15/31] TODO comment update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src_testbed/testbed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index f02380c..c16e62e 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -429,7 +429,7 @@ impl Testbed { } } - //FIXME: move this to dedicated benchmarking code + // TODO: move this to dedicated benchmarking code if benchmark_mode { use std::fs::File; use std::io::{BufWriter, Write}; From d75d325b49c2acfd996fb2c625cda790ff1189bc Mon Sep 17 00:00:00 2001 From: rezural <69941255+rezural@users.noreply.github.com> Date: Thu, 31 Dec 2020 11:52:24 +1100 Subject: [PATCH 16/31] remove comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src_testbed/testbed.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index c16e62e..62f02c1 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1125,7 +1125,6 @@ impl State for Testbed { self.set_world(w.3, w.4, w.5); self.harness.physics.broad_phase = w.1; self.harness.physics.narrow_phase = w.2; - //FIXME: not completely sure this is valid self.harness.state.timestep_id = w.0; } } From 47e0ad44257d386e3647564c5b2120042c207992 Mon Sep 17 00:00:00 2001 From: rezural <69941255+rezural@users.noreply.github.com> Date: Thu, 31 Dec 2020 12:00:33 +1100 Subject: [PATCH 17/31] Update src_testbed/testbed.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src_testbed/testbed.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 62f02c1..c169a85 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -1291,13 +1291,11 @@ impl State for Testbed { } for plugin in &mut self.plugins { - { - plugin.run_callbacks( - window, - &mut self.harness.physics, - &self.harness.state, - ); - } + plugin.run_callbacks( + window, + &mut self.harness.physics, + &self.harness.state, + ); } // if true { From d51008903dc77be6bf7ed1eebcf592f587b3f448 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 12:14:38 +1100 Subject: [PATCH 18/31] remove plugin callback related code from testbed --- src_testbed/testbed.rs | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index c169a85..db9f32d 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -123,7 +123,6 @@ pub struct Testbed { graphics: GraphicsManager, nsteps: usize, camera_locked: bool, // Used so that the camera can remain the same before and after we change backend or press the restart button. - callbacks: Callbacks, plugins: Vec>, hide_counters: bool, // persistant_contacts: HashMap, @@ -140,10 +139,6 @@ pub struct Testbed { nphysics: Option, } -type Callbacks = Vec< - Box, ->; - impl Testbed { pub fn new_empty() -> Testbed { let graphics = GraphicsManager::new(); @@ -185,7 +180,6 @@ impl Testbed { Testbed { builders: Vec::new(), - callbacks: Vec::new(), plugins: Vec::new(), graphics, nsteps: 1, @@ -382,8 +376,6 @@ impl Testbed { } fn clear(&mut self, window: &mut Window) { - //FIXME: do we need to do this still, after moving to harness code? - self.callbacks.clear(); // self.persistant_contacts.clear(); // self.state.grabbed_object = None; // self.state.grabbed_object_constraint = None; @@ -401,16 +393,6 @@ impl Testbed { self.plugins.push(Box::new(plugin)); } - pub fn add_callback< - F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, &RunState) - + 'static, - >( - &mut self, - callback: F, - ) { - self.callbacks.push(Box::new(callback)); - } - pub fn run(mut self) { let mut args = env::args(); let mut benchmark_mode = false; @@ -1029,8 +1011,6 @@ impl State for Testbed { ); } - // let physics = &self.harness.physics; - // Handle UI actions. { let backend_changed = self @@ -1291,11 +1271,7 @@ impl State for Testbed { } for plugin in &mut self.plugins { - plugin.run_callbacks( - window, - &mut self.harness.physics, - &self.harness.state, - ); + plugin.run_callbacks(window, &mut self.harness.physics, &self.harness.state); } // if true { From aaf2872e5c8cd444d34b3503dd3380b1dcd074f2 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:06:13 +1100 Subject: [PATCH 19/31] leading _ for run_state to squash warning --- src_testbed/ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_testbed/ui.rs b/src_testbed/ui.rs index 1b9084c..4c511f2 100644 --- a/src_testbed/ui.rs +++ b/src_testbed/ui.rs @@ -99,7 +99,7 @@ impl TestbedUi { window: &mut Window, integration_parameters: &mut IntegrationParameters, state: &mut TestbedState, - run_state: &mut RunState, + _run_state: &mut RunState, ) { let ui_root = window.conrod_ui().window; let mut ui = window.conrod_ui_mut().set_widgets(); From d992ebc4885737511d9b5fafd1846e7601963e55 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:06:29 +1100 Subject: [PATCH 20/31] remove unused use --- src_testbed/testbed.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index db9f32d..d723e59 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -25,7 +25,6 @@ use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase}; #[cfg(feature = "dim3")] use rapier::geometry::{InteractionGroups, Ray}; use rapier::math::Vector; -use rapier::pipeline::ChannelEventCollector; #[cfg(all(feature = "dim2", feature = "other-backends"))] use crate::box2d_backend::Box2dWorld; From f7820139471178fd273c7698eba17cb4ee0cf00d Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:24:29 +1100 Subject: [PATCH 21/31] make examples compile, code that accessed window & graphics via the callback is currently disabled, until that is added back in --- examples2d/add_remove2.rs | 10 +++++++--- examples2d/platform2.rs | 2 +- examples2d/sensor2.rs | 8 +++++--- examples3d/debug_add_remove_collider3.rs | 6 ++++-- examples3d/debug_dynamic_collider_add3.rs | 9 ++++++--- examples3d/debug_rollback3.rs | 2 +- examples3d/fountain3.rs | 9 ++++++--- examples3d/platform3.rs | 2 +- examples3d/sensor3.rs | 8 +++++--- 9 files changed, 36 insertions(+), 20 deletions(-) diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a3d223d..7ace00f 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -19,7 +19,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, handle, &physics.bodies, &physics.colliders); + + // TODO: need a way to access graphics & window + // graphics.add(window, handle, &physics.bodies, &physics.colliders); let to_remove: Vec<_> = physics .bodies @@ -31,7 +33,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - graphics.remove_body_nodes(window, handle); + + // TODO: need a way to access graphics & window + // graphics.remove_body_nodes(window, handle); } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 3b543db..5b84a18 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,7 +60,7 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.add_callback(move |_, physics, _, _, run_state| { + testbed.harness_mut().add_callback(move |physics, _, run_state, _| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 959ecbf..c9edea0 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,7 +69,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, physics, events, graphics, _| { + testbed.harness_mut().add_callback(move |physics, events, _, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -80,10 +80,12 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - graphics.set_body_color(parent_handle1, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); } if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - graphics.set_body_color(parent_handle2, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); } } }); diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index a42dc97..1c59fcf 100644 --- a/examples3d/debug_add_remove_collider3.rs +++ b/examples3d/debug_add_remove_collider3.rs @@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); colliders.insert(collider, ball_handle, &mut bodies); - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders @@ -43,7 +43,9 @@ pub fn init_world(testbed: &mut Testbed) { ground_collider_handle = physics .colliders .insert(coll, ground_handle, &mut physics.bodies); - graphics.add_collider(window, ground_collider_handle, &physics.colliders); + + // TODO: need a way to access graphics & window + // graphics.add_collider(window, ground_collider_handle, &physics.colliders); }); /* diff --git a/examples3d/debug_dynamic_collider_add3.rs b/examples3d/debug_dynamic_collider_add3.rs index a71e95e..da33494 100644 --- a/examples3d/debug_dynamic_collider_add3.rs +++ b/examples3d/debug_dynamic_collider_add3.rs @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut extra_colliders = Vec::new(); let snapped_frame = 51; - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { step += 1; // Add a bigger ball collider @@ -56,7 +56,8 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, ball_handle, &mut physics.bodies); - graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); + // TODO: need a way to access graphics & window + // graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); extra_colliders.push(new_ball_collider_handle); // Snap the ball velocity or restore it. @@ -95,7 +96,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(coll, ground_handle, &mut physics.bodies); - graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); + + // TODO: need a way to access graphics & window + // graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); extra_colliders.push(new_ground_collider_handle); }); diff --git a/examples3d/debug_rollback3.rs b/examples3d/debug_rollback3.rs index 19f9474..c3eb684 100644 --- a/examples3d/debug_rollback3.rs +++ b/examples3d/debug_rollback3.rs @@ -44,7 +44,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut step = 0; let snapped_frame = 51; - testbed.add_callback(move |_, physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { step += 1; // Snap the ball velocity or restore it. diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index fafa988..8dd12ed 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut k = 0; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, physics, _, graphics, _| { + testbed.harness_mut().add_callback(move |physics, _, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) @@ -41,7 +41,8 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, handle, &physics.bodies, &physics.colliders); + // TODO: need a way to access graphics & window + // graphics.add(window, handle, &physics.bodies, &physics.colliders); if physics.bodies.len() > MAX_NUMBER_OF_BODIES { let mut to_remove: Vec<_> = physics @@ -67,7 +68,9 @@ pub fn init_world(testbed: &mut Testbed) { physics .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - graphics.remove_body_nodes(window, *handle); + + // TODO: need a way to access graphics & window + // graphics.remove_body_nodes(window, *handle); } } }); diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 1e8d330..82ea8f4 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -65,7 +65,7 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a callback to control the platform. */ let mut count = 0; - testbed.add_callback(move |_, physics, _, _, run_state| { + testbed.harness_mut().add_callback(move |physics, _, run_state, _| { count += 1; if count % 100 > 50 { return; diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index 7b218fe..44c8d25 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,7 +73,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, physics, events, graphics, _| { + testbed.harness_mut().add_callback(move |physics, events, _, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -84,10 +84,12 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - graphics.set_body_color(parent_handle1, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); } if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - graphics.set_body_color(parent_handle2, color); + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); } } }); From 26af08e03c4e54f7a00603804ae9a4ff3e2ab3ea Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:25:05 +1100 Subject: [PATCH 22/31] add harness_mut() to testbed --- src_testbed/testbed.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index d723e59..8b9f2eb 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -239,6 +239,10 @@ impl Testbed { &mut self.harness.physics } + pub fn harness_mut(&mut self) -> &mut Harness { + &mut self.harness + } + pub fn set_world(&mut self, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) { self.set_world_with_gravity(bodies, colliders, joints, Vector::y() * -9.81) } From e11ace383164de39dfaadcb00f258497b132cf1d Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:31:30 +1100 Subject: [PATCH 23/31] cargo fmt --- examples2d/add_remove2.rs | 4 ++-- examples2d/platform2.rs | 30 +++++++++++++------------- examples2d/sensor2.rs | 36 +++++++++++++++++--------------- examples3d/fountain3.rs | 2 +- examples3d/platform3.rs | 44 ++++++++++++++++++++------------------- examples3d/sensor3.rs | 36 +++++++++++++++++--------------- 6 files changed, 80 insertions(+), 72 deletions(-) diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index 7ace00f..a351a8f 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -19,7 +19,7 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - + // TODO: need a way to access graphics & window // graphics.add(window, handle, &physics.bodies, &physics.colliders); @@ -33,7 +33,7 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - + // TODO: need a way to access graphics & window // graphics.remove_body_nodes(window, handle); } diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 5b84a18..769161f 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,23 +60,25 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.harness_mut().add_callback(move |physics, _, run_state, _| { - let platform = physics.bodies.get_mut(platform_handle).unwrap(); - let mut next_pos = *platform.position(); + testbed + .harness_mut() + .add_callback(move |physics, _, run_state, _| { + let platform = physics.bodies.get_mut(platform_handle).unwrap(); + let mut next_pos = *platform.position(); - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; - if next_pos.translation.vector.x >= rad * 10.0 { - next_pos.translation.vector.x -= dt; - } - if next_pos.translation.vector.x <= -rad * 10.0 { - next_pos.translation.vector.x += dt; - } + if next_pos.translation.vector.x >= rad * 10.0 { + next_pos.translation.vector.x -= dt; + } + if next_pos.translation.vector.x <= -rad * 10.0 { + next_pos.translation.vector.x += dt; + } - platform.set_next_kinematic_position(next_pos); - }); + platform.set_next_kinematic_position(next_pos); + }); /* * Run the simulation. diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index c9edea0..123f633 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,26 +69,28 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, events, _, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; + testbed + .harness_mut() + .add_callback(move |physics, events, _, _| { + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); + } } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); - } - } - }); + }); /* * Set up the testbed. diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index 8dd12ed..5dcf2d8 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -68,7 +68,7 @@ pub fn init_world(testbed: &mut Testbed) { physics .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - + // TODO: need a way to access graphics & window // graphics.remove_body_nodes(window, *handle); } diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 82ea8f4..b71d307 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -65,29 +65,31 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a callback to control the platform. */ let mut count = 0; - testbed.harness_mut().add_callback(move |physics, _, run_state, _| { - count += 1; - if count % 100 > 50 { - return; - } - - if let Some(platform) = physics.bodies.get_mut(platform_handle) { - let mut next_pos = *platform.position(); - - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; - - if next_pos.translation.vector.z >= rad * 10.0 { - next_pos.translation.vector.z -= dt; - } - if next_pos.translation.vector.z <= -rad * 10.0 { - next_pos.translation.vector.z += dt; + testbed + .harness_mut() + .add_callback(move |physics, _, run_state, _| { + count += 1; + if count % 100 > 50 { + return; } - platform.set_next_kinematic_position(next_pos); - } - }); + if let Some(platform) = physics.bodies.get_mut(platform_handle) { + let mut next_pos = *platform.position(); + + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; + + if next_pos.translation.vector.z >= rad * 10.0 { + next_pos.translation.vector.z -= dt; + } + if next_pos.translation.vector.z <= -rad * 10.0 { + next_pos.translation.vector.z += dt; + } + + platform.set_next_kinematic_position(next_pos); + } + }); /* * Run the simulation. diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index 44c8d25..e734258 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,26 +73,28 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, events, _, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; + testbed + .harness_mut() + .add_callback(move |physics, events, _, _| { + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // TODO: need a way to access graphics & window + // graphics.set_body_color(parent_handle2, color); + } } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); - } - } - }); + }); /* * Set up the testbed. From 1ac2d03fea4f51164df0b662fe990c27aedaf0ab Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 13:56:37 +1100 Subject: [PATCH 24/31] Revert "leading _ for run_state to squash warning" This reverts commit aaf2872e5c8cd444d34b3503dd3380b1dcd074f2. --- src_testbed/ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_testbed/ui.rs b/src_testbed/ui.rs index 4c511f2..1b9084c 100644 --- a/src_testbed/ui.rs +++ b/src_testbed/ui.rs @@ -99,7 +99,7 @@ impl TestbedUi { window: &mut Window, integration_parameters: &mut IntegrationParameters, state: &mut TestbedState, - _run_state: &mut RunState, + run_state: &mut RunState, ) { let ui_root = window.conrod_ui().window; let mut ui = window.conrod_ui_mut().set_widgets(); From 6f508e5d04e5652991f9feaad09231af84542ac1 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:23:25 +1100 Subject: [PATCH 25/31] remove redundant time :f32 from harness callbacks. it can be access via run_state.time --- examples2d/add_remove2.rs | 2 +- examples2d/platform2.rs | 2 +- examples2d/sensor2.rs | 2 +- examples3d/debug_add_remove_collider3.rs | 2 +- examples3d/debug_dynamic_collider_add3.rs | 2 +- examples3d/debug_rollback3.rs | 2 +- examples3d/fountain3.rs | 2 +- examples3d/platform3.rs | 2 +- examples3d/sensor3.rs | 2 +- src_testbed/harness/mod.rs | 5 ++--- 10 files changed, 11 insertions(+), 12 deletions(-) diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a351a8f..614829b 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 769161f..88f05f2 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -62,7 +62,7 @@ pub fn init_world(testbed: &mut Testbed) { */ testbed .harness_mut() - .add_callback(move |physics, _, run_state, _| { + .add_callback(move |physics, _, run_state| { let platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = *platform.position(); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 123f633..382581e 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -71,7 +71,7 @@ pub fn init_world(testbed: &mut Testbed) { // Callback that will be executed on the main loop to handle proximities. testbed .harness_mut() - .add_callback(move |physics, events, _, _| { + .add_callback(move |physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index 1c59fcf..437a27d 100644 --- a/examples3d/debug_add_remove_collider3.rs +++ b/examples3d/debug_add_remove_collider3.rs @@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); colliders.insert(collider, ball_handle, &mut bodies); - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders diff --git a/examples3d/debug_dynamic_collider_add3.rs b/examples3d/debug_dynamic_collider_add3.rs index da33494..92ae209 100644 --- a/examples3d/debug_dynamic_collider_add3.rs +++ b/examples3d/debug_dynamic_collider_add3.rs @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut extra_colliders = Vec::new(); let snapped_frame = 51; - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { step += 1; // Add a bigger ball collider diff --git a/examples3d/debug_rollback3.rs b/examples3d/debug_rollback3.rs index c3eb684..aa0b0ff 100644 --- a/examples3d/debug_rollback3.rs +++ b/examples3d/debug_rollback3.rs @@ -44,7 +44,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut step = 0; let snapped_frame = 51; - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { step += 1; // Snap the ball velocity or restore it. diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index 5dcf2d8..0909a0c 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut k = 0; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _, _| { + testbed.harness_mut().add_callback(move |physics, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index b71d307..944adae 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -67,7 +67,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut count = 0; testbed .harness_mut() - .add_callback(move |physics, _, run_state, _| { + .add_callback(move |physics, _, run_state| { count += 1; if count % 100 > 50 { return; diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index e734258..db01265 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -75,7 +75,7 @@ pub fn init_world(testbed: &mut Testbed) { // Callback that will be executed on the main loop to handle proximities. testbed .harness_mut() - .add_callback(move |physics, events, _, _| { + .add_callback(move |physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 45e52dc..638c4ed 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -48,7 +48,7 @@ pub struct Harness { pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { @@ -130,7 +130,7 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback( + pub fn add_callback( &mut self, callback: F, ) { @@ -185,7 +185,6 @@ impl Harness { &mut self.physics, &self.events, &self.state, - self.state.time, ) } From c300ce760c9151d7cfc9391600750ca9bc6ad600 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:26:11 +1100 Subject: [PATCH 26/31] cargo fmt --- src_testbed/harness/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 638c4ed..aa73afd 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -181,11 +181,7 @@ impl Harness { } for f in &mut self.callbacks { - f( - &mut self.physics, - &self.events, - &self.state, - ) + f(&mut self.physics, &self.events, &self.state) } for plugin in &mut self.plugins { From 6d5b6d778da11decd8303ac23260fc7e5dd3080e Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:29:09 +1100 Subject: [PATCH 27/31] remove time field from HarnessPlugin trait --- src_testbed/harness/plugin.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index ef0ebc4..ac20945 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -8,7 +8,6 @@ pub trait HarnessPlugin { physics: &mut PhysicsState, events: &PhysicsEvents, harness_state: &RunState, - t: f32, ); fn step(&mut self, physics: &mut PhysicsState, run_state: &RunState); fn profiling_string(&self) -> String; From 5fb9304f4cf7c7efdffae3f4c9b83a88ee2df048 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:33:33 +1100 Subject: [PATCH 28/31] remove time from plugin.run_callbacks --- src_testbed/harness/mod.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index aa73afd..86cee09 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -185,12 +185,7 @@ impl Harness { } for plugin in &mut self.plugins { - plugin.run_callbacks( - &mut self.physics, - &self.events, - &self.state, - self.state.time, - ) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state) } self.events.poll_all(); From ed76291fbfdc93290754cb0be500775fb9c5f48c Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:39:44 +1100 Subject: [PATCH 29/31] remove some unused imports --- src_testbed/testbed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index 8b9f2eb..bb3d8ee 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -4,7 +4,7 @@ use std::path::Path; use std::rc::Rc; use crate::engine::GraphicsManager; -use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState}; +use crate::physics::{PhysicsSnapshot, PhysicsState}; use crate::plugin::TestbedPlugin; use crate::ui::TestbedUi; @@ -28,7 +28,7 @@ use rapier::math::Vector; #[cfg(all(feature = "dim2", feature = "other-backends"))] use crate::box2d_backend::Box2dWorld; -use crate::harness::{Harness, RunState}; +use crate::harness::Harness; #[cfg(feature = "other-backends")] use crate::nphysics_backend::NPhysicsWorld; #[cfg(all(feature = "dim3", feature = "other-backends"))] From 34e79e9afc21ff0202d8a0338d0e8e038402a159 Mon Sep 17 00:00:00 2001 From: rezural Date: Sat, 2 Jan 2021 16:45:55 +1100 Subject: [PATCH 30/31] unify callbacks with & without graphics & window --- examples2d/add_remove2.rs | 15 ++++-- examples2d/platform2.rs | 30 ++++++------ examples2d/sensor2.rs | 36 +++++++------- examples3d/debug_add_remove_collider3.rs | 10 +++- examples3d/debug_dynamic_collider_add3.rs | 19 ++++++-- examples3d/debug_rollback3.rs | 2 +- examples3d/fountain3.rs | 15 ++++-- examples3d/platform3.rs | 44 ++++++++--------- examples3d/sensor3.rs | 38 ++++++++------- src_testbed/harness/mod.rs | 58 +++++++++++++++++++++-- src_testbed/testbed.rs | 23 +++++++-- 11 files changed, 188 insertions(+), 102 deletions(-) diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index 614829b..cb71025 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |window, graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -20,8 +20,11 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.add(window, handle, &physics.bodies, &physics.colliders); + if graphics.is_some() { + graphics + .unwrap() + .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + } let to_remove: Vec<_> = physics .bodies @@ -34,8 +37,10 @@ pub fn init_world(testbed: &mut Testbed) { .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - // TODO: need a way to access graphics & window - // graphics.remove_body_nodes(window, handle); + // FIXME: need a way to access graphics & window in a loop + // if graphics.is_some() { + // graphics.unwrap().remove_body_nodes(window.unwrap(), handle); + // } } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 88f05f2..afd4d64 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -60,25 +60,23 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed - .harness_mut() - .add_callback(move |physics, _, run_state| { - let platform = physics.bodies.get_mut(platform_handle).unwrap(); - let mut next_pos = *platform.position(); + testbed.add_callback(move |_, _, physics, _, run_state| { + let platform = physics.bodies.get_mut(platform_handle).unwrap(); + let mut next_pos = *platform.position(); - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; - if next_pos.translation.vector.x >= rad * 10.0 { - next_pos.translation.vector.x -= dt; - } - if next_pos.translation.vector.x <= -rad * 10.0 { - next_pos.translation.vector.x += dt; - } + if next_pos.translation.vector.x >= rad * 10.0 { + next_pos.translation.vector.x -= dt; + } + if next_pos.translation.vector.x <= -rad * 10.0 { + next_pos.translation.vector.x += dt; + } - platform.set_next_kinematic_position(next_pos); - }); + platform.set_next_kinematic_position(next_pos); + }); /* * Run the simulation. diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 382581e..9fdbf14 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,28 +69,26 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed - .harness_mut() - .add_callback(move |physics, events, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; + testbed.add_callback(move |_, _, physics, events, _| { + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); - } + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // FIXME: need a way to access graphics & window in a loop + // graphics.set_body_color(parent_handle1, color); } - }); + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // FIXME: need a way to access graphics & window in a loop + // graphics.set_body_color(parent_handle2, color); + } + } + }); /* * Set up the testbed. diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index 437a27d..8a3d672 100644 --- a/examples3d/debug_add_remove_collider3.rs +++ b/examples3d/debug_add_remove_collider3.rs @@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); colliders.insert(collider, ball_handle, &mut bodies); - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |window, graphics, physics, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders @@ -45,7 +45,13 @@ pub fn init_world(testbed: &mut Testbed) { .insert(coll, ground_handle, &mut physics.bodies); // TODO: need a way to access graphics & window - // graphics.add_collider(window, ground_collider_handle, &physics.colliders); + if graphics.is_some() { + graphics.unwrap().add_collider( + window.unwrap(), + ground_collider_handle, + &physics.colliders, + ); + } }); /* diff --git a/examples3d/debug_dynamic_collider_add3.rs b/examples3d/debug_dynamic_collider_add3.rs index 92ae209..cefe030 100644 --- a/examples3d/debug_dynamic_collider_add3.rs +++ b/examples3d/debug_dynamic_collider_add3.rs @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut extra_colliders = Vec::new(); let snapped_frame = 51; - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |window, graphics, physics, _, _| { step += 1; // Add a bigger ball collider @@ -56,8 +56,14 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, ball_handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); + + if graphics.is_some() { + graphics.unwrap().add_collider( + window.unwrap(), + new_ball_collider_handle, + &physics.colliders, + ); + } extra_colliders.push(new_ball_collider_handle); // Snap the ball velocity or restore it. @@ -97,8 +103,11 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(coll, ground_handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); + //FIXME: This causes an error + // if graphics.is_some() { + // graphics.unwrap().add_collider(window.unwrap(), new_ground_collider_handle, &physics.colliders); + // } + extra_colliders.push(new_ground_collider_handle); }); diff --git a/examples3d/debug_rollback3.rs b/examples3d/debug_rollback3.rs index aa0b0ff..27b27cf 100644 --- a/examples3d/debug_rollback3.rs +++ b/examples3d/debug_rollback3.rs @@ -44,7 +44,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut step = 0; let snapped_frame = 51; - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |_, _, physics, _, _| { step += 1; // Snap the ball velocity or restore it. diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index 0909a0c..e42f71a 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut k = 0; // Callback that will be executed on the main loop to handle proximities. - testbed.harness_mut().add_callback(move |physics, _, _| { + testbed.add_callback(move |window, graphics, physics, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) @@ -41,8 +41,12 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.add(window, handle, &physics.bodies, &physics.colliders); + + if graphics.is_some() { + graphics + .unwrap() + .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + } if physics.bodies.len() > MAX_NUMBER_OF_BODIES { let mut to_remove: Vec<_> = physics @@ -69,8 +73,9 @@ pub fn init_world(testbed: &mut Testbed) { .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - // TODO: need a way to access graphics & window - // graphics.remove_body_nodes(window, *handle); + // if graphics.is_some() { + // graphics.unwrap().remove_body_nodes(window.unwrap(), *handle); + // } } } }); diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 944adae..7b5c986 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -65,31 +65,29 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a callback to control the platform. */ let mut count = 0; - testbed - .harness_mut() - .add_callback(move |physics, _, run_state| { - count += 1; - if count % 100 > 50 { - return; + testbed.add_callback(move |_, _, physics, _, run_state| { + count += 1; + if count % 100 > 50 { + return; + } + + if let Some(platform) = physics.bodies.get_mut(platform_handle) { + let mut next_pos = *platform.position(); + + let dt = 0.016; + next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; + next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; + + if next_pos.translation.vector.z >= rad * 10.0 { + next_pos.translation.vector.z -= dt; + } + if next_pos.translation.vector.z <= -rad * 10.0 { + next_pos.translation.vector.z += dt; } - if let Some(platform) = physics.bodies.get_mut(platform_handle) { - let mut next_pos = *platform.position(); - - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt; - - if next_pos.translation.vector.z >= rad * 10.0 { - next_pos.translation.vector.z -= dt; - } - if next_pos.translation.vector.z <= -rad * 10.0 { - next_pos.translation.vector.z += dt; - } - - platform.set_next_kinematic_position(next_pos); - } - }); + platform.set_next_kinematic_position(next_pos); + } + }); /* * Run the simulation. diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index db01265..37a8f5a 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,28 +73,30 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed - .harness_mut() - .add_callback(move |physics, events, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), - }; + testbed.add_callback(move |_, graphics, physics, events, _| { + let graphics = &graphics; + while let Ok(prox) = events.proximity_events.try_recv() { + let color = match prox.new_status { + Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), + Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); + let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // TODO: need a way to access graphics & window - // graphics.set_body_color(parent_handle2, color); + match graphics { + Some(graphics) => { + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + // graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + // graphics.set_body_color(parent_handle2, color); + } } + None => {} } - }); + } + }); /* * Set up the testbed. diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 86cee09..51bf315 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,4 +1,8 @@ -use crate::physics::{PhysicsEvents, PhysicsState}; +use crate::{ + physics::{PhysicsEvents, PhysicsState}, + GraphicsManager, +}; +use kiss3d::window::Window; use plugin::HarnessPlugin; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; @@ -48,7 +52,17 @@ pub struct Harness { pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec< + Box< + dyn FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ), + >, +>; #[allow(dead_code)] impl Harness { @@ -130,7 +144,15 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback( + pub fn add_callback< + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, + >( &mut self, callback: F, ) { @@ -138,6 +160,14 @@ impl Harness { } pub fn step(&mut self) { + self.step_with_graphics(None, None); + } + + pub fn step_with_graphics( + &mut self, + window: Option<&mut Window>, + graphics: Option<&mut GraphicsManager>, + ) { #[cfg(feature = "parallel")] { let physics = &mut self.physics; @@ -180,8 +210,26 @@ impl Harness { plugin.step(&mut self.physics, &self.state) } - for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state) + // FIXME: This assumes either window & graphics are Some, or they are all None + // this is required as we cannot pass Option<&mut Window> & Option<&mut GraphicsManager directly in a loop + // there must be a better way of doing this? + match (window, graphics) { + (Some(window), Some(graphics)) => { + for f in &mut self.callbacks { + f( + Some(window), + Some(graphics), + &mut self.physics, + &self.events, + &self.state, + ); + } + } + _ => { + for f in &mut self.callbacks { + f(None, None, &mut self.physics, &self.events, &self.state); + } + } } for plugin in &mut self.plugins { diff --git a/src_testbed/testbed.rs b/src_testbed/testbed.rs index bb3d8ee..fd6b9a0 100644 --- a/src_testbed/testbed.rs +++ b/src_testbed/testbed.rs @@ -3,10 +3,10 @@ use std::mem; use std::path::Path; use std::rc::Rc; -use crate::engine::GraphicsManager; -use crate::physics::{PhysicsSnapshot, PhysicsState}; +use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState}; use crate::plugin::TestbedPlugin; use crate::ui::TestbedUi; +use crate::{engine::GraphicsManager, harness::RunState}; use kiss3d::camera::Camera; use kiss3d::event::Event; @@ -392,6 +392,21 @@ impl Testbed { self.plugins.clear(); } + pub fn add_callback< + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, + >( + &mut self, + callback: F, + ) { + self.harness.add_callback(callback); + } + pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { self.plugins.push(Box::new(plugin)); } @@ -1221,7 +1236,9 @@ impl State for Testbed { if self.state.running != RunMode::Stop { for _ in 0..self.nsteps { if self.state.selected_backend == RAPIER_BACKEND { - self.harness.step(); + let graphics = &mut self.graphics; + self.harness + .step_with_graphics(Some(window), Some(graphics)); for plugin in &mut self.plugins { plugin.step(&mut self.harness.physics) From 5ca82eeaee5c45d31cdbb5f963d0f93b19196ea8 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 3 Jan 2021 19:54:56 +1100 Subject: [PATCH 31/31] enable graphics and windows related code in examples --- examples2d/add_remove2.rs | 15 ++++++--------- examples2d/sensor2.rs | 17 ++++++++--------- examples3d/debug_add_remove_collider3.rs | 11 +++-------- examples3d/debug_dynamic_collider_add3.rs | 18 +++++++----------- examples3d/fountain3.rs | 14 ++++++-------- examples3d/sensor3.rs | 18 +++++++----------- 6 files changed, 37 insertions(+), 56 deletions(-) diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index cb71025..0aeffbe 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) { let rad = 0.5; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, graphics, physics, _, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -20,10 +20,8 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, handle, &mut physics.bodies); - if graphics.is_some() { - graphics - .unwrap() - .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add(*window, handle, &physics.bodies, &physics.colliders); } let to_remove: Vec<_> = physics @@ -37,10 +35,9 @@ pub fn init_world(testbed: &mut Testbed) { .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - // FIXME: need a way to access graphics & window in a loop - // if graphics.is_some() { - // graphics.unwrap().remove_body_nodes(window.unwrap(), handle); - // } + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(*window, handle); + } } }); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 9fdbf14..3d5976d 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,7 +69,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, _, physics, events, _| { + testbed.add_callback(move |_, mut graphics, physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -78,14 +78,13 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // FIXME: need a way to access graphics & window in a loop - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // FIXME: need a way to access graphics & window in a loop - // graphics.set_body_color(parent_handle2, color); + if let Some(graphics) = &mut graphics { + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + graphics.set_body_color(parent_handle2, color); + } } } }); diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index 8a3d672..f353162 100644 --- a/examples3d/debug_add_remove_collider3.rs +++ b/examples3d/debug_add_remove_collider3.rs @@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); colliders.insert(collider, ball_handle, &mut bodies); - testbed.add_callback(move |window, graphics, physics, _, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders @@ -44,13 +44,8 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(coll, ground_handle, &mut physics.bodies); - // TODO: need a way to access graphics & window - if graphics.is_some() { - graphics.unwrap().add_collider( - window.unwrap(), - ground_collider_handle, - &physics.colliders, - ); + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, ground_collider_handle, &physics.colliders); } }); diff --git a/examples3d/debug_dynamic_collider_add3.rs b/examples3d/debug_dynamic_collider_add3.rs index cefe030..bd7205e 100644 --- a/examples3d/debug_dynamic_collider_add3.rs +++ b/examples3d/debug_dynamic_collider_add3.rs @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut extra_colliders = Vec::new(); let snapped_frame = 51; - testbed.add_callback(move |window, graphics, physics, _, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { step += 1; // Add a bigger ball collider @@ -57,13 +57,10 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, ball_handle, &mut physics.bodies); - if graphics.is_some() { - graphics.unwrap().add_collider( - window.unwrap(), - new_ball_collider_handle, - &physics.colliders, - ); + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, new_ball_collider_handle, &physics.colliders); } + extra_colliders.push(new_ball_collider_handle); // Snap the ball velocity or restore it. @@ -103,10 +100,9 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(coll, ground_handle, &mut physics.bodies); - //FIXME: This causes an error - // if graphics.is_some() { - // graphics.unwrap().add_collider(window.unwrap(), new_ground_collider_handle, &physics.colliders); - // } + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); + } extra_colliders.push(new_ground_collider_handle); }); diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs index e42f71a..5acc2e8 100644 --- a/examples3d/fountain3.rs +++ b/examples3d/fountain3.rs @@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) { let mut k = 0; // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |window, graphics, physics, _, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) @@ -42,10 +42,8 @@ pub fn init_world(testbed: &mut Testbed) { .colliders .insert(collider, handle, &mut physics.bodies); - if graphics.is_some() { - graphics - .unwrap() - .add(window.unwrap(), handle, &physics.bodies, &physics.colliders); + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.add(window, handle, &physics.bodies, &physics.colliders); } if physics.bodies.len() > MAX_NUMBER_OF_BODIES { @@ -73,9 +71,9 @@ pub fn init_world(testbed: &mut Testbed) { .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - // if graphics.is_some() { - // graphics.unwrap().remove_body_nodes(window.unwrap(), *handle); - // } + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(window, *handle); + } } } }); diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index 37a8f5a..4160248 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,8 +73,7 @@ pub fn init_world(testbed: &mut Testbed) { testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); // Callback that will be executed on the main loop to handle proximities. - testbed.add_callback(move |_, graphics, physics, events, _| { - let graphics = &graphics; + testbed.add_callback(move |_, mut graphics, physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), @@ -84,16 +83,13 @@ pub fn init_world(testbed: &mut Testbed) { let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); - match graphics { - Some(graphics) => { - if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { - // graphics.set_body_color(parent_handle1, color); - } - if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { - // graphics.set_body_color(parent_handle2, color); - } + if let Some(graphics) = &mut graphics { + if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { + graphics.set_body_color(parent_handle1, color); + } + if parent_handle2 != ground_handle && parent_handle2 != sensor_handle { + graphics.set_body_color(parent_handle2, color); } - None => {} } } });