From 3b000f90bfb0762df23f78a8a9b9a9cf5ad6c08a Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Thu, 27 Aug 2020 09:01:32 +0200 Subject: Fix BroadPhase proxy handle recycling causing a crash. --- examples3d/add_remove3.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ examples3d/all_examples3.rs | 2 ++ examples3d/kinematic3.rs | 4 ++-- examples3d/sensor3.rs | 6 ++--- 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 examples3d/add_remove3.rs (limited to 'examples3d') diff --git a/examples3d/add_remove3.rs b/examples3d/add_remove3.rs new file mode 100644 index 0000000..cf565e0 --- /dev/null +++ b/examples3d/add_remove3.rs @@ -0,0 +1,53 @@ +use na::Point3; +use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier3d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed3d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + let bodies = RigidBodySet::new(); + let colliders = ColliderSet::new(); + let joints = JointSet::new(); + let rad = 0.5; + + // Callback that will be executed on the main loop to handle proximities. + testbed.add_callback(move |window, physics, _, graphics, _| { + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(0.0, 10.0, 0.0) + .build(); + let handle = physics.bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad, rad).density(1.0).build(); + physics + .colliders + .insert(collider, handle, &mut physics.bodies); + graphics.add(window, handle, &physics.bodies, &physics.colliders); + + let to_remove: Vec<_> = physics + .bodies + .iter() + .filter(|(_, b)| b.position.translation.vector.y < -10.0) + .map(|e| e.0) + .collect(); + for handle in to_remove { + physics.pipeline.remove_rigid_body( + handle, + &mut physics.broad_phase, + &mut physics.narrow_phase, + &mut physics.bodies, + &mut physics.colliders, + &mut physics.joints, + ); + graphics.remove_body_nodes(window, handle); + } + }); + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point3::new(-30.0, -4.0, -30.0), Point3::new(0.0, 1.0, 0.0)); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Add-remove", init_world)]); + testbed.run() +} diff --git a/examples3d/all_examples3.rs b/examples3d/all_examples3.rs index 10b8f33..04b9b36 100644 --- a/examples3d/all_examples3.rs +++ b/examples3d/all_examples3.rs @@ -10,6 +10,7 @@ use inflector::Inflector; use rapier_testbed3d::Testbed; use std::cmp::Ordering; +mod add_remove3; mod balls3; mod boxes3; mod capsules3; @@ -66,6 +67,7 @@ pub fn main() { .to_camel_case(); let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ + ("Add remove", add_remove3::init_world), ("Balls", balls3::init_world), ("Boxes", boxes3::init_world), ("Capsules", capsules3::init_world), diff --git a/examples3d/kinematic3.rs b/examples3d/kinematic3.rs index d6f2014..a9adc8c 100644 --- a/examples3d/kinematic3.rs +++ b/examples3d/kinematic3.rs @@ -66,8 +66,8 @@ pub fn init_world(testbed: &mut Testbed) { /* * Setup a callback to control the platform. */ - testbed.add_callback(move |bodies, _, _, _, time| { - let mut platform = bodies.get_mut(platform_handle).unwrap(); + testbed.add_callback(move |_, physics, _, _, time| { + let mut platform = physics.bodies.get_mut(platform_handle).unwrap(); let mut next_pos = platform.position; let dt = 0.016; diff --git a/examples3d/sensor3.rs b/examples3d/sensor3.rs index 50c051f..598b0ad 100644 --- a/examples3d/sensor3.rs +++ b/examples3d/sensor3.rs @@ -73,15 +73,15 @@ 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 |_, colliders, events, graphics, _| { + testbed.add_callback(move |_, physics, events, 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 = colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = 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 { graphics.set_body_color(parent_handle1, color); -- cgit