unify callbacks with & without graphics & window

This commit is contained in:
rezural
2021-01-02 16:45:55 +11:00
parent ed76291fbf
commit 34e79e9afc
11 changed files with 188 additions and 102 deletions

View File

@@ -10,7 +10,7 @@ pub fn init_world(testbed: &mut Testbed) {
let rad = 0.5; let rad = 0.5;
// Callback that will be executed on the main loop to handle proximities. // 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() let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 10.0) .translation(0.0, 10.0)
.build(); .build();
@@ -20,8 +20,11 @@ pub fn init_world(testbed: &mut Testbed) {
.colliders .colliders
.insert(collider, handle, &mut physics.bodies); .insert(collider, handle, &mut physics.bodies);
// TODO: need a way to access graphics & window if graphics.is_some() {
// graphics.add(window, handle, &physics.bodies, &physics.colliders); graphics
.unwrap()
.add(window.unwrap(), handle, &physics.bodies, &physics.colliders);
}
let to_remove: Vec<_> = physics let to_remove: Vec<_> = physics
.bodies .bodies
@@ -34,8 +37,10 @@ pub fn init_world(testbed: &mut Testbed) {
.bodies .bodies
.remove(handle, &mut physics.colliders, &mut physics.joints); .remove(handle, &mut physics.colliders, &mut physics.joints);
// TODO: need a way to access graphics & window // FIXME: need a way to access graphics & window in a loop
// graphics.remove_body_nodes(window, handle); // if graphics.is_some() {
// graphics.unwrap().remove_body_nodes(window.unwrap(), handle);
// }
} }
}); });

View File

@@ -60,25 +60,23 @@ pub fn init_world(testbed: &mut Testbed) {
/* /*
* Setup a callback to control the platform. * Setup a callback to control the platform.
*/ */
testbed testbed.add_callback(move |_, _, physics, _, run_state| {
.harness_mut() let platform = physics.bodies.get_mut(platform_handle).unwrap();
.add_callback(move |physics, _, run_state| { let mut next_pos = *platform.position();
let platform = physics.bodies.get_mut(platform_handle).unwrap();
let mut next_pos = *platform.position();
let dt = 0.016; let dt = 0.016;
next_pos.translation.vector.y += (run_state.time * 5.0).sin() * 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; 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;
} }
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;
} }
platform.set_next_kinematic_position(next_pos); platform.set_next_kinematic_position(next_pos);
}); });
/* /*
* Run the simulation. * Run the simulation.

View File

@@ -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)); 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. // Callback that will be executed on the main loop to handle proximities.
testbed testbed.add_callback(move |_, _, physics, events, _| {
.harness_mut() while let Ok(prox) = events.proximity_events.try_recv() {
.add_callback(move |physics, events, _| { let color = match prox.new_status {
while let Ok(prox) = events.proximity_events.try_recv() { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0),
let color = match prox.new_status { Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0),
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_handle1 = physics.colliders.get(prox.collider1).unwrap().parent();
let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent();
if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { if parent_handle1 != ground_handle && parent_handle1 != sensor_handle {
// TODO: need a way to access graphics & window // FIXME: need a way to access graphics & window in a loop
// graphics.set_body_color(parent_handle1, color); // 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 {
// FIXME: need a way to access graphics & window in a loop
// graphics.set_body_color(parent_handle2, color);
}
}
});
/* /*
* Set up the testbed. * Set up the testbed.

View File

@@ -34,7 +34,7 @@ pub fn init_world(testbed: &mut Testbed) {
let collider = ColliderBuilder::ball(ball_rad).density(100.0).build(); let collider = ColliderBuilder::ball(ball_rad).density(100.0).build();
colliders.insert(collider, ball_handle, &mut bodies); 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. // Remove then re-add the ground collider.
let coll = physics let coll = physics
.colliders .colliders
@@ -45,7 +45,13 @@ pub fn init_world(testbed: &mut Testbed) {
.insert(coll, ground_handle, &mut physics.bodies); .insert(coll, ground_handle, &mut physics.bodies);
// TODO: need a way to access graphics & window // 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,
);
}
}); });
/* /*

View File

@@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) {
let mut extra_colliders = Vec::new(); let mut extra_colliders = Vec::new();
let snapped_frame = 51; let snapped_frame = 51;
testbed.harness_mut().add_callback(move |physics, _, _| { testbed.add_callback(move |window, graphics, physics, _, _| {
step += 1; step += 1;
// Add a bigger ball collider // Add a bigger ball collider
@@ -56,8 +56,14 @@ pub fn init_world(testbed: &mut Testbed) {
physics physics
.colliders .colliders
.insert(collider, ball_handle, &mut physics.bodies); .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); extra_colliders.push(new_ball_collider_handle);
// Snap the ball velocity or restore it. // Snap the ball velocity or restore it.
@@ -97,8 +103,11 @@ pub fn init_world(testbed: &mut Testbed) {
.colliders .colliders
.insert(coll, ground_handle, &mut physics.bodies); .insert(coll, ground_handle, &mut physics.bodies);
// TODO: need a way to access graphics & window //FIXME: This causes an error
// graphics.add_collider(window, new_ground_collider_handle, &physics.colliders); // if graphics.is_some() {
// graphics.unwrap().add_collider(window.unwrap(), new_ground_collider_handle, &physics.colliders);
// }
extra_colliders.push(new_ground_collider_handle); extra_colliders.push(new_ground_collider_handle);
}); });

View File

@@ -44,7 +44,7 @@ pub fn init_world(testbed: &mut Testbed) {
let mut step = 0; let mut step = 0;
let snapped_frame = 51; let snapped_frame = 51;
testbed.harness_mut().add_callback(move |physics, _, _| { testbed.add_callback(move |_, _, physics, _, _| {
step += 1; step += 1;
// Snap the ball velocity or restore it. // Snap the ball velocity or restore it.

View File

@@ -26,7 +26,7 @@ pub fn init_world(testbed: &mut Testbed) {
let mut k = 0; let mut k = 0;
// Callback that will be executed on the main loop to handle proximities. // 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; k += 1;
let rigid_body = RigidBodyBuilder::new_dynamic() let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(0.0, 10.0, 0.0) .translation(0.0, 10.0, 0.0)
@@ -41,8 +41,12 @@ pub fn init_world(testbed: &mut Testbed) {
physics physics
.colliders .colliders
.insert(collider, handle, &mut physics.bodies); .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 { if physics.bodies.len() > MAX_NUMBER_OF_BODIES {
let mut to_remove: Vec<_> = physics let mut to_remove: Vec<_> = physics
@@ -69,8 +73,9 @@ pub fn init_world(testbed: &mut Testbed) {
.narrow_phase .narrow_phase
.maintain(&mut physics.colliders, &mut physics.bodies); .maintain(&mut physics.colliders, &mut physics.bodies);
// TODO: need a way to access graphics & window // if graphics.is_some() {
// graphics.remove_body_nodes(window, *handle); // graphics.unwrap().remove_body_nodes(window.unwrap(), *handle);
// }
} }
} }
}); });

View File

@@ -65,31 +65,29 @@ 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 testbed.add_callback(move |_, _, physics, _, run_state| {
.harness_mut() count += 1;
.add_callback(move |physics, _, run_state| { if count % 100 > 50 {
count += 1; return;
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) { platform.set_next_kinematic_position(next_pos);
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. * Run the simulation.

View File

@@ -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)); 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. // Callback that will be executed on the main loop to handle proximities.
testbed testbed.add_callback(move |_, graphics, physics, events, _| {
.harness_mut() let graphics = &graphics;
.add_callback(move |physics, events, _| { while let Ok(prox) = events.proximity_events.try_recv() {
while let Ok(prox) = events.proximity_events.try_recv() { let color = match prox.new_status {
let color = match prox.new_status { Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0),
Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0),
Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), };
};
let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent();
let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent();
if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { match graphics {
// TODO: need a way to access graphics & window Some(graphics) => {
// graphics.set_body_color(parent_handle1, color); 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 { }
// TODO: need a way to access graphics & window if parent_handle2 != ground_handle && parent_handle2 != sensor_handle {
// graphics.set_body_color(parent_handle2, color); // graphics.set_body_color(parent_handle2, color);
}
} }
None => {}
} }
}); }
});
/* /*
* Set up the testbed. * Set up the testbed.

View File

@@ -1,4 +1,8 @@
use crate::physics::{PhysicsEvents, PhysicsState}; use crate::{
physics::{PhysicsEvents, PhysicsState},
GraphicsManager,
};
use kiss3d::window::Window;
use plugin::HarnessPlugin; use plugin::HarnessPlugin;
use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase};
@@ -48,7 +52,17 @@ pub struct Harness {
pub state: RunState, pub state: RunState,
} }
type Callbacks = Vec<Box<dyn FnMut(&mut PhysicsState, &PhysicsEvents, &RunState)>>; type Callbacks = Vec<
Box<
dyn FnMut(
Option<&mut Window>,
Option<&mut GraphicsManager>,
&mut PhysicsState,
&PhysicsEvents,
&RunState,
),
>,
>;
#[allow(dead_code)] #[allow(dead_code)]
impl Harness { impl Harness {
@@ -130,7 +144,15 @@ impl Harness {
self.plugins.push(Box::new(plugin)); self.plugins.push(Box::new(plugin));
} }
pub fn add_callback<F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState) + 'static>( pub fn add_callback<
F: FnMut(
Option<&mut Window>,
Option<&mut GraphicsManager>,
&mut PhysicsState,
&PhysicsEvents,
&RunState,
) + 'static,
>(
&mut self, &mut self,
callback: F, callback: F,
) { ) {
@@ -138,6 +160,14 @@ impl Harness {
} }
pub fn step(&mut self) { 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")] #[cfg(feature = "parallel")]
{ {
let physics = &mut self.physics; let physics = &mut self.physics;
@@ -180,8 +210,26 @@ impl Harness {
plugin.step(&mut self.physics, &self.state) plugin.step(&mut self.physics, &self.state)
} }
for f in &mut self.callbacks { // FIXME: This assumes either window & graphics are Some, or they are all None
f(&mut self.physics, &self.events, &self.state) // 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 { for plugin in &mut self.plugins {

View File

@@ -3,10 +3,10 @@ use std::mem;
use std::path::Path; use std::path::Path;
use std::rc::Rc; 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::plugin::TestbedPlugin;
use crate::ui::TestbedUi; use crate::ui::TestbedUi;
use crate::{engine::GraphicsManager, harness::RunState};
use kiss3d::camera::Camera; use kiss3d::camera::Camera;
use kiss3d::event::Event; use kiss3d::event::Event;
@@ -392,6 +392,21 @@ impl Testbed {
self.plugins.clear(); 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) { pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) {
self.plugins.push(Box::new(plugin)); self.plugins.push(Box::new(plugin));
} }
@@ -1221,7 +1236,9 @@ impl State for Testbed {
if self.state.running != RunMode::Stop { if self.state.running != RunMode::Stop {
for _ in 0..self.nsteps { for _ in 0..self.nsteps {
if self.state.selected_backend == RAPIER_BACKEND { 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 { for plugin in &mut self.plugins {
plugin.step(&mut self.harness.physics) plugin.step(&mut self.harness.physics)