refactor testbed to use harness

This commit is contained in:
rezural
2020-12-24 17:58:37 +11:00
parent fd3b4801b6
commit c56ebcc663
5 changed files with 147 additions and 209 deletions

View File

@@ -60,13 +60,13 @@ pub fn init_world(testbed: &mut Testbed) {
/* /*
* Setup a callback to control the platform. * 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 platform = physics.bodies.get_mut(platform_handle).unwrap();
let mut next_pos = *platform.position(); let mut next_pos = *platform.position();
let dt = 0.016; let dt = 0.016;
next_pos.translation.vector.y += (time * 5.0).sin() * dt; next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt;
next_pos.translation.vector.x += time.sin() * 5.0 * dt; next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt;
if next_pos.translation.vector.x >= rad * 10.0 { if next_pos.translation.vector.x >= rad * 10.0 {
next_pos.translation.vector.x -= dt; next_pos.translation.vector.x -= dt;

View File

@@ -65,7 +65,7 @@ pub fn init_world(testbed: &mut Testbed) {
* Setup a callback to control the platform. * Setup a callback to control the platform.
*/ */
let mut count = 0; let mut count = 0;
testbed.add_callback(move |_, physics, _, _, time| { testbed.add_callback(move |_, physics, _, _, run_state| {
count += 1; count += 1;
if count % 100 > 50 { if count % 100 > 50 {
return; return;
@@ -75,8 +75,8 @@ pub fn init_world(testbed: &mut Testbed) {
let mut next_pos = *platform.position(); let mut next_pos = *platform.position();
let dt = 0.016; let dt = 0.016;
next_pos.translation.vector.y += (time * 5.0).sin() * dt; next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt;
next_pos.translation.vector.z += time.sin() * 5.0 * dt; next_pos.translation.vector.z += run_state.time.sin() * 5.0 * dt;
if next_pos.translation.vector.z >= rad * 10.0 { if next_pos.translation.vector.z >= rad * 10.0 {
next_pos.translation.vector.z -= dt; next_pos.translation.vector.z -= dt;

View File

@@ -7,24 +7,35 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline};
pub mod plugin; pub mod plugin;
pub struct HarnessState { pub struct RunState {
#[cfg(feature = "parallel")] #[cfg(feature = "parallel")]
pub thread_pool: rapier::rayon::ThreadPool, pub thread_pool: rapier::rayon::ThreadPool,
pub timestep_id: usize, 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 { pub struct Harness {
physics: PhysicsState, pub physics: PhysicsState,
max_steps: usize, max_steps: usize,
callbacks: Callbacks, callbacks: Callbacks,
plugins: Vec<Box<dyn HarnessPlugin>>, plugins: Vec<Box<dyn HarnessPlugin>>,
time: f32,
events: PhysicsEvents, events: PhysicsEvents,
event_handler: ChannelEventCollector, 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(&mut PhysicsState, &PhysicsEvents, &RunState, f32)>>;
#[allow(dead_code)] #[allow(dead_code)]
impl Harness { impl Harness {
@@ -46,18 +57,13 @@ impl Harness {
proximity_events: proximity_channel.1, proximity_events: proximity_channel.1,
}; };
let physics = PhysicsState::new(); let physics = PhysicsState::new();
let state = HarnessState { let state = RunState::new();
#[cfg(feature = "parallel")]
thread_pool,
timestep_id: 0,
};
Self { Self {
physics, physics,
max_steps: 1000, max_steps: 1000,
callbacks: Vec::new(), callbacks: Vec::new(),
plugins: Vec::new(), plugins: Vec::new(),
time: 0.0,
events, events,
event_handler, event_handler,
state, state,
@@ -101,7 +107,6 @@ impl Harness {
self.physics.joints = joints; self.physics.joints = joints;
self.physics.broad_phase = BroadPhase::new(); self.physics.broad_phase = BroadPhase::new();
self.physics.narrow_phase = NarrowPhase::new(); self.physics.narrow_phase = NarrowPhase::new();
self.time = 0.0;
self.state.timestep_id = 0; self.state.timestep_id = 0;
self.physics.query_pipeline = QueryPipeline::new(); self.physics.query_pipeline = QueryPipeline::new();
self.physics.pipeline = PhysicsPipeline::new(); self.physics.pipeline = PhysicsPipeline::new();
@@ -113,7 +118,7 @@ impl Harness {
} }
pub fn add_callback< pub fn add_callback<
F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState, f32) + 'static,
>( >(
&mut self, &mut self,
callback: F, callback: F,
@@ -165,22 +170,22 @@ impl Harness {
} }
for f in &mut self.callbacks { 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 { 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.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) { pub fn run(&mut self) {
for _ in 0..self.max_steps { for _ in 0..self.max_steps {
self.step(); self.step();
self.state.timestep_id += 1;
} }
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::harness::HarnessState; use crate::harness::RunState;
use crate::physics::PhysicsEvents; use crate::physics::PhysicsEvents;
use crate::PhysicsState; use crate::PhysicsState;
@@ -7,7 +7,7 @@ pub trait HarnessPlugin {
&mut self, &mut self,
physics: &mut PhysicsState, physics: &mut PhysicsState,
events: &PhysicsEvents, events: &PhysicsEvents,
harness_state: &HarnessState, harness_state: &RunState,
t: f32, t: f32,
); );
fn step(&mut self, physics: &mut PhysicsState); fn step(&mut self, physics: &mut PhysicsState);

View File

@@ -33,6 +33,7 @@ use crate::box2d_backend::Box2dWorld;
use crate::nphysics_backend::NPhysicsWorld; use crate::nphysics_backend::NPhysicsWorld;
#[cfg(all(feature = "dim3", feature = "other-backends"))] #[cfg(all(feature = "dim3", feature = "other-backends"))]
use crate::physx_backend::PhysxWorld; use crate::physx_backend::PhysxWorld;
use crate::harness::{Harness, RunState};
const RAPIER_BACKEND: usize = 0; const RAPIER_BACKEND: usize = 0;
const NPHYSICS_BACKEND: usize = 1; const NPHYSICS_BACKEND: usize = 1;
@@ -116,14 +117,13 @@ pub struct TestbedState {
pub physx_use_two_friction_directions: bool, pub physx_use_two_friction_directions: bool,
pub num_threads: usize, pub num_threads: usize,
pub snapshot: Option<PhysicsSnapshot>, pub snapshot: Option<PhysicsSnapshot>,
#[cfg(feature = "parallel")] // #[cfg(feature = "parallel")]
pub thread_pool: rapier::rayon::ThreadPool, // pub thread_pool: rapier::rayon::ThreadPool,
pub timestep_id: usize, pub timestep_id: usize,
} }
pub struct Testbed { pub struct Testbed {
builders: Vec<(&'static str, fn(&mut Testbed))>, builders: Vec<(&'static str, fn(&mut Testbed))>,
physics: PhysicsState,
graphics: GraphicsManager, graphics: GraphicsManager,
nsteps: usize, 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. 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, event_handler: ChannelEventCollector,
ui: Option<TestbedUi>, ui: Option<TestbedUi>,
state: TestbedState, state: TestbedState,
harness: Harness,
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
box2d: Option<Box2dWorld>, box2d: Option<Box2dWorld>,
#[cfg(all(feature = "dim3", feature = "other-backends"))] #[cfg(all(feature = "dim3", feature = "other-backends"))]
@@ -147,7 +148,7 @@ pub struct Testbed {
} }
type Callbacks = type Callbacks =
Vec<Box<dyn FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, f32)>>; Vec<Box<dyn FnMut(&mut Window, &mut PhysicsState, &PhysicsEvents, &mut GraphicsManager, &RunState)>>;
impl Testbed { impl Testbed {
pub fn new_empty() -> Testbed { pub fn new_empty() -> Testbed {
@@ -201,6 +202,8 @@ impl Testbed {
thread_pool, thread_pool,
}; };
let harness = Harness::new_empty();
let contact_channel = crossbeam::channel::unbounded(); let contact_channel = crossbeam::channel::unbounded();
let proximity_channel = crossbeam::channel::unbounded(); let proximity_channel = crossbeam::channel::unbounded();
let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0);
@@ -208,11 +211,9 @@ impl Testbed {
contact_events: contact_channel.1, contact_events: contact_channel.1,
proximity_events: proximity_channel.1, proximity_events: proximity_channel.1,
}; };
let physics = PhysicsState::new();
Testbed { Testbed {
builders: Vec::new(), builders: Vec::new(),
physics,
callbacks: Vec::new(), callbacks: Vec::new(),
plugins: Vec::new(), plugins: Vec::new(),
graphics, graphics,
@@ -227,6 +228,7 @@ impl Testbed {
event_handler, event_handler,
events, events,
state, state,
harness,
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
box2d: None, box2d: None,
#[cfg(all(feature = "dim3", feature = "other-backends"))] #[cfg(all(feature = "dim3", feature = "other-backends"))]
@@ -238,7 +240,7 @@ impl Testbed {
pub fn new(bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) -> Self { pub fn new(bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) -> Self {
let mut res = Self::new_empty(); let mut res = Self::new_empty();
res.set_world(bodies, colliders, joints); res.harness.set_world(bodies, colliders, joints);
res res
} }
@@ -269,11 +271,11 @@ impl Testbed {
} }
pub fn integration_parameters_mut(&mut self) -> &mut IntegrationParameters { 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 { 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) { pub fn set_world(&mut self, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) {
@@ -287,32 +289,30 @@ impl Testbed {
joints: JointSet, joints: JointSet,
gravity: Vector<f32>, gravity: Vector<f32>,
) { ) {
println!("Num bodies: {}", bodies.len()); println!("Num bodies: {}", bodies.len());
println!("Num joints: {}", joints.len()); println!("Num joints: {}", joints.len());
self.physics.gravity = gravity; self.harness.set_world_with_gravity(bodies, colliders, joints, 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.state self.state
.action_flags .action_flags
.set(TestbedActionFlags::RESET_WORLD_GRAPHICS, true); .set(TestbedActionFlags::RESET_WORLD_GRAPHICS, true);
//FIXME: remove time & state.timestep_id if appropriate
self.time = 0.0; self.time = 0.0;
self.state.timestep_id = 0; self.state.timestep_id = 0;
self.state.highlighted_body = None; self.state.highlighted_body = None;
self.physics.query_pipeline = QueryPipeline::new();
self.physics.pipeline = PhysicsPipeline::new(); let physics = &self.harness.physics;
self.physics.pipeline.counters.enable();
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
{ {
if self.state.selected_backend == BOX2D_BACKEND { if self.state.selected_backend == BOX2D_BACKEND {
self.box2d = Some(Box2dWorld::from_rapier( self.box2d = Some(Box2dWorld::from_rapier(
self.physics.gravity, physics.gravity,
&self.physics.bodies, &physics.bodies,
&self.physics.colliders, &physics.colliders,
&self.physics.joints, &physics.joints,
)); ));
} }
} }
@@ -323,13 +323,13 @@ impl Testbed {
|| self.state.selected_backend == PHYSX_BACKEND_TWO_FRICTION_DIR || self.state.selected_backend == PHYSX_BACKEND_TWO_FRICTION_DIR
{ {
self.physx = Some(PhysxWorld::from_rapier( self.physx = Some(PhysxWorld::from_rapier(
self.physics.gravity, physics.gravity,
&self.physics.integration_parameters, &physics.integration_parameters,
&self.physics.bodies, &physics.bodies,
&self.physics.colliders, &physics.colliders,
&self.physics.joints, &physics.joints,
self.state.selected_backend == PHYSX_BACKEND_TWO_FRICTION_DIR, 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 { if self.state.selected_backend == NPHYSICS_BACKEND {
self.nphysics = Some(NPhysicsWorld::from_rapier( self.nphysics = Some(NPhysicsWorld::from_rapier(
self.physics.gravity, physics.gravity,
&self.physics.bodies, &physics.bodies,
&self.physics.colliders, &physics.colliders,
&self.physics.joints, &physics.joints,
)); ));
} }
} }
@@ -438,7 +438,7 @@ impl Testbed {
} }
pub fn add_callback< 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, &mut self,
callback: F, callback: F,
@@ -484,11 +484,11 @@ impl Testbed {
&& (backend_id == PHYSX_BACKEND_PATCH_FRICTION && (backend_id == PHYSX_BACKEND_PATCH_FRICTION
|| backend_id == PHYSX_BACKEND_TWO_FRICTION_DIR) || backend_id == PHYSX_BACKEND_TWO_FRICTION_DIR)
{ {
self.physics.integration_parameters.max_velocity_iterations = 1; self.harness.physics.integration_parameters.max_velocity_iterations = 1;
self.physics.integration_parameters.max_position_iterations = 4; self.harness.physics.integration_parameters.max_position_iterations = 4;
} else { } else {
self.physics.integration_parameters.max_velocity_iterations = 4; self.harness.physics.integration_parameters.max_velocity_iterations = 4;
self.physics.integration_parameters.max_position_iterations = 1; self.harness.physics.integration_parameters.max_position_iterations = 1;
} }
// Init world. // Init world.
(builder.1)(&mut self); (builder.1)(&mut self);
@@ -498,55 +498,19 @@ impl Testbed {
{ {
// FIXME: code duplicated from self.step() // FIXME: code duplicated from self.step()
if self.state.selected_backend == RAPIER_BACKEND { if self.state.selected_backend == RAPIER_BACKEND {
#[cfg(feature = "parallel")] self.harness.step();
{
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);
} }
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
{ {
if self.state.selected_backend == BOX2D_BACKEND { if self.state.selected_backend == BOX2D_BACKEND {
self.box2d.as_mut().unwrap().step( self.box2d.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut self.harness.physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.box2d.as_mut().unwrap().sync( self.box2d.as_mut().unwrap().sync(
&mut self.physics.bodies, &mut self.harness.physics.bodies,
&mut self.physics.colliders, &mut self.harness.physics.colliders,
); );
} }
} }
@@ -558,12 +522,12 @@ impl Testbed {
{ {
// println!("Step"); // println!("Step");
self.physx.as_mut().unwrap().step( self.physx.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut self.harness.physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.physx.as_mut().unwrap().sync( self.physx.as_mut().unwrap().sync(
&mut self.physics.bodies, &mut self.harness.physics.bodies,
&mut self.physics.colliders, &mut self.harness.physics.colliders,
); );
} }
} }
@@ -572,12 +536,12 @@ impl Testbed {
{ {
if self.state.selected_backend == NPHYSICS_BACKEND { if self.state.selected_backend == NPHYSICS_BACKEND {
self.nphysics.as_mut().unwrap().step( self.nphysics.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut self.harness.physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.nphysics.as_mut().unwrap().sync( self.nphysics.as_mut().unwrap().sync(
&mut self.physics.bodies, &mut self.harness.physics.bodies,
&mut self.physics.colliders, &mut self.harness.physics.colliders,
); );
} }
} }
@@ -585,7 +549,7 @@ impl Testbed {
// Skip the first update. // Skip the first update.
if k > 0 { 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); results.push(timings);
@@ -639,8 +603,7 @@ impl Testbed {
.set(TestbedActionFlags::EXAMPLE_CHANGED, true), .set(TestbedActionFlags::EXAMPLE_CHANGED, true),
WindowEvent::Key(Key::C, Action::Release, _) => { WindowEvent::Key(Key::C, Action::Release, _) => {
// Delete 1 collider of 10% of the remaining dynamic bodies. // Delete 1 collider of 10% of the remaining dynamic bodies.
let mut colliders: Vec<_> = self let mut colliders: Vec<_> = self.harness.physics
.physics
.bodies .bodies
.iter() .iter()
.filter(|e| e.1.is_dynamic()) .filter(|e| e.1.is_dynamic())
@@ -651,14 +614,17 @@ impl Testbed {
let num_to_delete = (colliders.len() / 10).max(1); let num_to_delete = (colliders.len() / 10).max(1);
for to_delete in &colliders[..num_to_delete] { for to_delete in &colliders[..num_to_delete] {
self.physics self
.harness
.physics
.colliders .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, _) => { WindowEvent::Key(Key::D, Action::Release, _) => {
// Delete 10% of the remaining dynamic bodies. // Delete 10% of the remaining dynamic bodies.
let dynamic_bodies: Vec<_> = self let dynamic_bodies: Vec<_> = self
.harness
.physics .physics
.bodies .bodies
.iter() .iter()
@@ -667,21 +633,23 @@ impl Testbed {
.collect(); .collect();
let num_to_delete = (dynamic_bodies.len() / 10).max(1); let num_to_delete = (dynamic_bodies.len() / 10).max(1);
for to_delete in &dynamic_bodies[..num_to_delete] { for to_delete in &dynamic_bodies[..num_to_delete] {
self.physics.bodies.remove( self.harness.physics.bodies.remove(
*to_delete, *to_delete,
&mut self.physics.colliders, &mut self.harness.physics.colliders,
&mut self.physics.joints, &mut self.harness.physics.joints,
); );
} }
} }
WindowEvent::Key(Key::J, Action::Release, _) => { WindowEvent::Key(Key::J, Action::Release, _) => {
// Delete 10% of the remaining joints. // 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); let num_to_delete = (joints.len() / 10).max(1);
for to_delete in &joints[..num_to_delete] { for to_delete in &joints[..num_to_delete] {
self.physics self
.harness
.physics
.joints .joints
.remove(*to_delete, &mut self.physics.bodies, true); .remove(*to_delete, &mut self.harness.physics.bodies, true);
} }
} }
WindowEvent::CursorPos(x, y, _) => { WindowEvent::CursorPos(x, y, _) => {
@@ -1028,15 +996,16 @@ impl Testbed {
.camera() .camera()
.unproject(&self.cursor_pos, &na::convert(size)); .unproject(&self.cursor_pos, &na::convert(size));
let ray = Ray::new(pos, dir); let ray = Ray::new(pos, dir);
let hit = self.physics.query_pipeline.cast_ray( let physics = &self.harness.physics;
&self.physics.colliders, let hit = physics.query_pipeline.cast_ray(
&physics.colliders,
&ray, &ray,
f32::MAX, f32::MAX,
InteractionGroups::all(), InteractionGroups::all(),
); );
if let Some((_, collider, _)) = hit { 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()); self.state.highlighted_body = Some(collider.parent());
for node in self.graphics.body_nodes_mut(collider.parent()).unwrap() { for node in self.graphics.body_nodes_mut(collider.parent()).unwrap() {
node.select() node.select()
@@ -1075,11 +1044,13 @@ impl State for Testbed {
if let Some(ui) = &mut self.ui { if let Some(ui) = &mut self.ui {
ui.update( ui.update(
window, window,
&mut self.physics.integration_parameters, &mut self.harness.physics.integration_parameters,
&mut self.state, &mut self.state,
); );
} }
// let physics = &self.harness.physics;
// Handle UI actions. // Handle UI actions.
{ {
let backend_changed = self let backend_changed = self
@@ -1123,7 +1094,7 @@ impl State for Testbed {
self.clear(window); self.clear(window);
if self.state.selected_example != prev_example { 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); self.builders[self.state.selected_example].1(self);
@@ -1141,11 +1112,11 @@ impl State for Testbed {
.set(TestbedActionFlags::TAKE_SNAPSHOT, false); .set(TestbedActionFlags::TAKE_SNAPSHOT, false);
self.state.snapshot = PhysicsSnapshot::new( self.state.snapshot = PhysicsSnapshot::new(
self.state.timestep_id, self.state.timestep_id,
&self.physics.broad_phase, &self.harness.physics.broad_phase,
&self.physics.narrow_phase, &self.harness.physics.narrow_phase,
&self.physics.bodies, &self.harness.physics.bodies,
&self.physics.colliders, &self.harness.physics.colliders,
&self.physics.joints, &self.harness.physics.joints,
) )
.ok(); .ok();
@@ -1154,6 +1125,7 @@ impl State for Testbed {
} }
} }
if self if self
.state .state
.action_flags .action_flags
@@ -1172,8 +1144,8 @@ impl State for Testbed {
} }
self.set_world(w.3, w.4, w.5); self.set_world(w.3, w.4, w.5);
self.physics.broad_phase = w.1; self.harness.physics.broad_phase = w.1;
self.physics.narrow_phase = w.2; self.harness.physics.narrow_phase = w.2;
self.state.timestep_id = w.0; self.state.timestep_id = w.0;
} }
} }
@@ -1187,12 +1159,12 @@ impl State for Testbed {
self.state self.state
.action_flags .action_flags
.set(TestbedActionFlags::RESET_WORLD_GRAPHICS, false); .set(TestbedActionFlags::RESET_WORLD_GRAPHICS, false);
for (handle, _) in self.physics.bodies.iter() { for (handle, _) in self.harness.physics.bodies.iter() {
self.graphics.add( self.graphics.add(
window, window,
handle, handle,
&self.physics.bodies, &self.harness.physics.bodies,
&self.physics.colliders, &self.harness.physics.colliders,
); );
} }
@@ -1207,7 +1179,7 @@ impl State for Testbed {
!= self.state.flags.contains(TestbedStateFlags::WIREFRAME) != self.state.flags.contains(TestbedStateFlags::WIREFRAME)
{ {
self.graphics.toggle_wireframe_mode( self.graphics.toggle_wireframe_mode(
&self.physics.colliders, &self.harness.physics.colliders,
self.state.flags.contains(TestbedStateFlags::WIREFRAME), self.state.flags.contains(TestbedStateFlags::WIREFRAME),
) )
} }
@@ -1216,11 +1188,11 @@ impl State for Testbed {
!= self.state.flags.contains(TestbedStateFlags::SLEEP) != self.state.flags.contains(TestbedStateFlags::SLEEP)
{ {
if 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(); body.activation.threshold = ActivationStatus::default_threshold();
} }
} else { } 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.wake_up(true);
body.activation.threshold = -1.0; body.activation.threshold = -1.0;
} }
@@ -1233,7 +1205,7 @@ impl State for Testbed {
.contains(TestbedStateFlags::SUB_STEPPING) .contains(TestbedStateFlags::SUB_STEPPING)
!= self.state.flags.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); self.state.flags.contains(TestbedStateFlags::SUB_STEPPING);
} }
@@ -1285,46 +1257,11 @@ impl State for Testbed {
for _ in 0..self.nsteps { for _ in 0..self.nsteps {
self.state.timestep_id += 1; self.state.timestep_id += 1;
if self.state.selected_backend == RAPIER_BACKEND { if self.state.selected_backend == RAPIER_BACKEND {
#[cfg(feature = "parallel")] self.harness.step();
{
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);
//FIXME: this should probably be delegated to harness plugins
for plugin in &mut self.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 { if self.state.selected_backend == BOX2D_BACKEND {
self.box2d.as_mut().unwrap().step( self.box2d.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.box2d self.box2d
.as_mut() .as_mut()
.unwrap() .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"); // println!("Step");
self.physx.as_mut().unwrap().step( self.physx.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.physx self.physx
.as_mut() .as_mut()
.unwrap() .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 { if self.state.selected_backend == NPHYSICS_BACKEND {
self.nphysics.as_mut().unwrap().step( self.nphysics.as_mut().unwrap().step(
&mut self.physics.pipeline.counters, &mut physics.pipeline.counters,
&self.physics.integration_parameters, &physics.integration_parameters,
); );
self.nphysics self.nphysics
.as_mut() .as_mut()
.unwrap() .unwrap()
.sync(&mut self.physics.bodies, &mut self.physics.colliders); .sync(&mut physics.bodies, &mut physics.colliders);
} }
} }
for f in &mut self.callbacks { // FIXME: should this be handled by harness plugins?
f(
window,
&mut self.physics,
&self.events,
&mut self.graphics,
self.time,
)
}
for plugin in &mut self.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(); self.events.poll_all();
@@ -1396,20 +1326,22 @@ impl State for Testbed {
// #[cfg(feature = "log")] // #[cfg(feature = "log")]
// debug!("{}", self.world.counters); // 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 self.graphics
.draw(&self.physics.bodies, &self.physics.colliders, window); .draw(&physics.bodies, &physics.colliders, window);
for plugin in &mut self.plugins { for plugin in &mut self.plugins {
plugin.draw(); plugin.draw();
} }
if self.state.flags.contains(TestbedStateFlags::CONTACT_POINTS) { 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 { 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 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(); let mut profile = String::new();
if self.state.flags.contains(TestbedStateFlags::PROFILE) { if self.state.flags.contains(TestbedStateFlags::PROFILE) {
@@ -1472,11 +1404,12 @@ CCD: {:.2}ms
if self.state.flags.contains(TestbedStateFlags::DEBUG) { if self.state.flags.contains(TestbedStateFlags::DEBUG) {
let t = instant::now(); let t = instant::now();
let bf = bincode::serialize(&self.physics.broad_phase).unwrap(); let physics = &self.harness.physics;
let nf = bincode::serialize(&self.physics.narrow_phase).unwrap(); let bf = bincode::serialize(&physics.broad_phase).unwrap();
let bs = bincode::serialize(&self.physics.bodies).unwrap(); let nf = bincode::serialize(&physics.narrow_phase).unwrap();
let cs = bincode::serialize(&self.physics.colliders).unwrap(); let bs = bincode::serialize(&physics.bodies).unwrap();
let js = bincode::serialize(&self.physics.joints).unwrap(); let cs = bincode::serialize(&physics.colliders).unwrap();
let js = bincode::serialize(&physics.joints).unwrap();
let serialization_time = instant::now() - t; let serialization_time = instant::now() - t;
let hash_bf = md5::compute(&bf); let hash_bf = md5::compute(&bf);
let hash_nf = md5::compute(&nf); let hash_nf = md5::compute(&nf);