diff options
| author | Sébastien Crozet <developer@crozet.re> | 2021-01-08 18:26:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-08 18:26:30 +0100 |
| commit | 0f3d68d518dc445cb94901d09c681f9a299c2078 (patch) | |
| tree | 3a0287f6314c0e7256b1a95ba3ed4c61e85a64a0 | |
| parent | fd3b4801b63fd56369ff37bdc2e5189db159e8ff (diff) | |
| parent | 5ca82eeaee5c45d31cdbb5f963d0f93b19196ea8 (diff) | |
| download | rapier-0f3d68d518dc445cb94901d09c681f9a299c2078.tar.gz rapier-0f3d68d518dc445cb94901d09c681f9a299c2078.tar.bz2 rapier-0f3d68d518dc445cb94901d09c681f9a299c2078.zip | |
Merge pull request #83 from rezural/harness-testbed-integration
Harness testbed integration
| -rw-r--r-- | examples2d/add_remove2.rs | 12 | ||||
| -rw-r--r-- | examples2d/platform2.rs | 6 | ||||
| -rw-r--r-- | examples2d/sensor2.rs | 15 | ||||
| -rw-r--r-- | examples3d/debug_add_remove_collider3.rs | 7 | ||||
| -rw-r--r-- | examples3d/debug_dynamic_collider_add3.rs | 14 | ||||
| -rw-r--r-- | examples3d/debug_rollback3.rs | 2 | ||||
| -rw-r--r-- | examples3d/fountain3.rs | 12 | ||||
| -rw-r--r-- | examples3d/platform3.rs | 6 | ||||
| -rw-r--r-- | examples3d/sensor3.rs | 14 | ||||
| -rw-r--r-- | src_testbed/harness/mod.rs | 104 | ||||
| -rw-r--r-- | src_testbed/harness/plugin.rs | 7 | ||||
| -rw-r--r-- | src_testbed/plugin.rs | 8 | ||||
| -rw-r--r-- | src_testbed/testbed.rs | 377 | ||||
| -rw-r--r-- | src_testbed/ui.rs | 12 |
14 files changed, 300 insertions, 296 deletions
diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index a3d223d..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, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0) .build(); @@ -19,7 +19,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, 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 .bodies @@ -31,7 +34,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .bodies .remove(handle, &mut physics.colliders, &mut physics.joints); - graphics.remove_body_nodes(window, handle); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(*window, handle); + } } }); diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 20b2e59..afd4d64 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/examples2d/sensor2.rs b/examples2d/sensor2.rs index 959ecbf..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, 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), @@ -78,12 +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 { - 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); + } } } }); diff --git a/examples3d/debug_add_remove_collider3.rs b/examples3d/debug_add_remove_collider3.rs index a42dc97..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, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { // Remove then re-add the ground collider. let coll = physics .colliders @@ -43,7 +43,10 @@ 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); + + 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 a71e95e..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, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { step += 1; // Add a bigger ball collider @@ -56,7 +56,11 @@ 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); + + 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. @@ -95,7 +99,11 @@ 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); + + 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/debug_rollback3.rs b/examples3d/debug_rollback3.rs index 19f9474..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.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 fafa988..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, physics, _, graphics, _| { + testbed.add_callback(move |mut window, mut graphics, physics, _, _| { k += 1; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(0.0, 10.0, 0.0) @@ -41,7 +41,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .colliders .insert(collider, handle, &mut physics.bodies); - graphics.add(window, 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 { let mut to_remove: Vec<_> = physics @@ -67,7 +70,10 @@ pub fn init_world(testbed: &mut Testbed) { physics .narrow_phase .maintain(&mut physics.colliders, &mut physics.bodies); - graphics.remove_body_nodes(window, *handle); + + if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) { + graphics.remove_body_nodes(window, *handle); + } } } }); diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 0975f39..7b5c986 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/examples3d/sensor3.rs b/examples3d/sensor3.rs index 7b218fe..4160248 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.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), @@ -83,11 +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(); - 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); + } } } }); diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index fa6c4c6..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}; @@ -7,24 +11,58 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; pub mod plugin; -pub struct HarnessState { +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, +} + +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: thread_pool, + #[cfg(feature = "parallel")] + num_threads, + timestep_id: 0, + time: 0.0, + } + } } pub struct Harness { - physics: PhysicsState, + pub physics: PhysicsState, max_steps: usize, callbacks: Callbacks, plugins: Vec<Box<dyn HarnessPlugin>>, - time: f32, events: PhysicsEvents, event_handler: ChannelEventCollector, - pub state: HarnessState, + pub state: RunState, } -type Callbacks = Vec<Box<dyn FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32)>>; +type Callbacks = Vec< + Box< + dyn FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ), + >, +>; #[allow(dead_code)] impl Harness { @@ -46,18 +84,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 +134,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 +145,13 @@ impl Harness { } pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, >( &mut self, callback: F, @@ -122,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; @@ -161,26 +207,44 @@ 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 { - f(&mut self.physics, &self.events, &self.state, self.time) + // 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 { - plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.time) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state) } 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..ac20945 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,9 +7,8 @@ pub trait HarnessPlugin { &mut self, physics: &mut PhysicsState, events: &PhysicsEvents, - harness_state: &HarnessState, - t: f32, + harness_state: &RunState, ); - fn step(&mut self, physics: &mut PhysicsState); + fn step(&mut self, physics: &mut PhysicsState, run_state: &RunState); fn profiling_string(&self) -> String; } diff --git a/src_testbed/plugin.rs b/src_testbed/plugin.rs index 872cdf5..877e7f6 100644 --- a/src_testbed/plugin.rs +++ b/src_testbed/plugin.rs @@ -1,3 +1,4 @@ +use crate::harness::RunState; use crate::physics::PhysicsState; use kiss3d::window::Window; use na::Point3; @@ -5,7 +6,12 @@ use na::Point3; pub trait TestbedPlugin { fn init_graphics(&mut self, window: &mut Window, gen_color: &mut dyn FnMut() -> Point3<f32>); 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 4fac8e1..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::{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; @@ -21,14 +21,14 @@ 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}; #[cfg(all(feature = "dim2", feature = "other-backends"))] use crate::box2d_backend::Box2dWorld; +use crate::harness::Harness; #[cfg(feature = "other-backends")] use crate::nphysics_backend::NPhysicsWorld; #[cfg(all(feature = "dim3", feature = "other-backends"))] @@ -114,30 +114,22 @@ 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<PhysicsSnapshot>, - #[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. - callbacks: Callbacks, plugins: Vec<Box<dyn TestbedPlugin>>, - time: f32, hide_counters: bool, // persistant_contacts: HashMap<ContactId, bool>, font: Rc<Font>, cursor_pos: Point2<f32>, - events: PhysicsEvents, - event_handler: ChannelEventCollector, ui: Option<TestbedUi>, state: TestbedState, + harness: Harness, #[cfg(all(feature = "dim2", feature = "other-backends"))] box2d: Option<Box2dWorld>, #[cfg(all(feature = "dim3", feature = "other-backends"))] @@ -146,9 +138,6 @@ pub struct Testbed { nphysics: Option<NPhysicsWorld>, } -type Callbacks = - Vec<Box<dyn FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, f32)>>; - impl Testbed { pub fn new_empty() -> Testbed { let graphics = GraphicsManager::new(); @@ -166,17 +155,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, @@ -193,40 +171,25 @@ 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, - #[cfg(feature = "parallel")] - thread_pool, }; - 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, - }; - let physics = PhysicsState::new(); + let harness = Harness::new_empty(); Testbed { builders: Vec::new(), - physics, - callbacks: Vec::new(), plugins: Vec::new(), 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"))] box2d: None, #[cfg(all(feature = "dim3", feature = "other-backends"))] @@ -238,7 +201,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 +232,15 @@ 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 harness_mut(&mut self) -> &mut Harness { + &mut self.harness } pub fn set_world(&mut self, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) { @@ -289,30 +256,23 @@ impl Testbed { ) { 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); - 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(); #[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 +283,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 +298,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, )); } } @@ -419,7 +379,6 @@ impl Testbed { } fn clear(&mut self, window: &mut Window) { - self.callbacks.clear(); // self.persistant_contacts.clear(); // self.state.grabbed_object = None; // self.state.grabbed_object_constraint = None; @@ -433,17 +392,23 @@ impl Testbed { self.plugins.clear(); } - pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { - self.plugins.push(Box::new(plugin)); - } - pub fn add_callback< - F: FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, f32) + 'static, + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, >( &mut self, callback: F, ) { - self.callbacks.push(Box::new(callback)); + self.harness.add_callback(callback); + } + + pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { + self.plugins.push(Box::new(plugin)); } pub fn run(mut self) { @@ -464,6 +429,7 @@ impl Testbed { } } + // TODO: move this to dedicated benchmarking code if benchmark_mode { use std::fs::File; use std::io::{BufWriter, Write}; @@ -484,11 +450,23 @@ 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); @@ -496,57 +474,20 @@ 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 { - #[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, |
