Skip to content

Commit

Permalink
Merge pull request #1 from Vrixyz/131-testbed-world-owner
Browse files Browse the repository at this point in the history
131 testbed world owner
  • Loading branch information
Vrixyz authored Aug 23, 2018
2 parents 73c93cf + 13bcce9 commit 098927c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
5 changes: 2 additions & 3 deletions examples2d/balls2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use nphysics2d::object::{BodyHandle, Material};
use nphysics2d::volumetric::Volumetric;
use nphysics2d::world::World;
use nphysics_testbed2d::Testbed;
use std::sync::Arc;
use std::sync::Mutex;
use nphysics_testbed2d::WorldOwnerExclusive;

const COLLIDER_MARGIN: f32 = 0.01;

Expand Down Expand Up @@ -82,7 +81,7 @@ fn main() {
/*
* Set up the testbed.
*/
let mut testbed = Testbed::new(Arc::new(Mutex::new(world)));
let mut testbed = Testbed::new(Box::new(WorldOwnerExclusive::new(world)));
testbed.look_at(Point2::new(0.0, -2.5), 95.0);
testbed.run();
}
3 changes: 3 additions & 0 deletions nphysics_testbed2d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ extern crate time;

pub use engine::GraphicsManager;
pub use testbed::Testbed;
pub use testbed::WorldOwner;
pub use testbed::WorldOwnerShared;
pub use testbed::WorldOwnerExclusive;

mod engine;
pub mod objects;
Expand Down
75 changes: 57 additions & 18 deletions nphysics_testbed2d/src/testbed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;
use std::ops::DerefMut;

#[derive(PartialEq)]
enum RunMode {
Expand Down Expand Up @@ -51,8 +52,45 @@ fn usage(exe_name: &str) {
println!(" b - draw the bounding boxes.");
}

pub struct Testbed {
/// This trait is designed to allow choosing implementation of underlying storing of World: shared between threads or owned only by WorldOwner.
pub trait WorldOwner {
fn get_mut<'a: 'b, 'b>(&'a mut self) -> Box<DerefMut<Target = World<f32>> + 'b>;
}

#[derive(Clone)]
pub struct WorldOwnerShared {
world: Arc<Mutex<World<f32>>>,
}

impl WorldOwnerShared {
pub fn new(w: World<f32>) -> WorldOwnerShared {
WorldOwnerShared {world: Arc::new(Mutex::new(w))}
}
}

impl WorldOwner for WorldOwnerShared {
fn get_mut<'a: 'b, 'b>(&'a mut self) -> Box<DerefMut<Target = World<f32>> + 'b> {
Box::new(self.world.lock().unwrap())
}
}

pub struct WorldOwnerExclusive {
world: World<f32>
}

impl WorldOwnerExclusive {
pub fn new(world: World<f32>) -> WorldOwnerExclusive {
WorldOwnerExclusive {world}
}
}

impl WorldOwner for WorldOwnerExclusive {
fn get_mut<'a: 'b, 'b>(&'a mut self) -> Box<DerefMut<Target = World<f32>> + 'b> {
Box::new(&mut self.world)
}
}

pub struct Testbed {
window: Option<Box<Window>>,
graphics: GraphicsManager,
nsteps: usize,
Expand All @@ -67,6 +105,7 @@ pub struct Testbed {
cursor_pos: Point2<f32>,
grabbed_object: Option<BodyHandle>,
grabbed_object_constraint: Option<ConstraintHandle>,
world: Box<WorldOwner>,
}

impl Testbed {
Expand All @@ -79,7 +118,7 @@ impl Testbed {
window.set_framerate_limit(Some(60));

Testbed {
world: Arc::new(Mutex::new(world)),
world: Box::new(WorldOwnerShared::new(world)),
callbacks: Vec::new(),
window: Some(window),
graphics: graphics,
Expand All @@ -97,7 +136,7 @@ impl Testbed {
}
}

pub fn new(world: Arc<Mutex<World<f32>>>) -> Testbed {
pub fn new(world: Box<WorldOwner>) -> Testbed {
let mut res = Testbed::new_empty();

res.set_world(world);
Expand All @@ -117,9 +156,9 @@ impl Testbed {
self.hide_counters = false;
}

pub fn set_world(&mut self, world: Arc<Mutex<World<f32>>>) {
pub fn set_world(&mut self, world: Box<WorldOwner>) {
self.world = world;
let mut world = self.world.lock().unwrap();
let mut world = self.world.get_mut();
world.enable_performance_counters();

self.graphics.clear(self.window.as_mut().unwrap());
Expand All @@ -142,9 +181,9 @@ impl Testbed {
self.graphics.set_collider_color(collider, color);
}

/*pub fn world(&self) -> &World<f32> {
&self.world.borrow_mut()
}*/
pub fn world(&self) -> &Box<WorldOwner> {
&self.world
}

pub fn graphics_mut(&mut self) -> &mut GraphicsManager {
&mut self.graphics
Expand Down Expand Up @@ -237,7 +276,7 @@ impl State for Testbed {
// graphics.add(window, WorldObject::RigidBody(body));
// },
WindowEvent::MouseButton(_, Action::Press, modifier) => {
let mut physics_world = &mut self.world.lock().unwrap();
let mut physics_world = &mut self.world.get_mut();
let mapped_point = self
.graphics
.camera()
Expand Down Expand Up @@ -310,7 +349,7 @@ impl State for Testbed {
}
}
WindowEvent::MouseButton(_, Action::Release, _) => {
let mut physics_world = &mut self.world.lock().unwrap();
let mut physics_world = &mut self.world.get_mut();
if let Some(body) = self.grabbed_object {
for n in self
.graphics
Expand All @@ -330,7 +369,7 @@ impl State for Testbed {
self.grabbed_object_constraint = None;
}
WindowEvent::CursorPos(x, y, modifiers) => {
let mut physics_world = &mut self.world.lock().unwrap();
let mut physics_world = &mut self.world.get_mut();
self.cursor_pos.x = x as f32;
self.cursor_pos.y = y as f32;

Expand Down Expand Up @@ -371,7 +410,7 @@ impl State for Testbed {
// // }
// },
WindowEvent::Key(Key::Space, Action::Release, _) => {
let mut physics_world = &mut self.world.lock().unwrap();
let mut physics_world = &mut self.world.get_mut();
self.draw_colls = !self.draw_colls;
for co in physics_world.colliders() {
// FIXME: ugly clone.
Expand Down Expand Up @@ -443,13 +482,13 @@ impl State for Testbed {
for f in &self.callbacks {
f(&mut self.graphics, self.time)
}
self.world.lock().unwrap().step();
self.world.get_mut().step();
if !self.hide_counters {
println!("{}", self.world.lock().unwrap().performance_counters());
println!("{}", self.world.get_mut().performance_counters());
}
self.time += self.world.lock().unwrap().timestep();
self.time += self.world.get_mut().timestep();
}
let mut physics_world = &mut self.world.lock().unwrap();
let physics_world = &self.world.get_mut();

for co in physics_world.colliders() {
if self.graphics.body_nodes_mut(physics_world, co.data().body()).is_none() {
Expand All @@ -463,7 +502,7 @@ impl State for Testbed {
if self.draw_colls {
draw_collisions(
window,
&mut self.world.lock().unwrap(),
&mut self.world.get_mut(),
&mut self.persistant_contacts,
self.running != RunMode::Stop,
);
Expand All @@ -481,7 +520,7 @@ impl State for Testbed {
&format!(
"Simulation time: {:.*}sec.",
4,
self.world.lock().unwrap().performance_counters().step_time(),
self.world.get_mut().performance_counters().step_time(),
)[..],
&Point2::origin(),
60.0,
Expand Down

0 comments on commit 098927c

Please sign in to comment.