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. --- examples2d/add_remove2.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ examples2d/all_examples2.rs | 2 ++ examples2d/kinematic2.rs | 4 ++-- examples2d/sensor2.rs | 6 ++--- 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 examples2d/add_remove2.rs (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs new file mode 100644 index 0000000..49da436 --- /dev/null +++ b/examples2d/add_remove2.rs @@ -0,0 +1,53 @@ +use na::Point2; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::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) + .build(); + let handle = physics.bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(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(Point2::new(0.0, 0.0), 20.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Add-remove", init_world)]); + testbed.run() +} diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs index 7b8bf09..836dcbc 100644 --- a/examples2d/all_examples2.rs +++ b/examples2d/all_examples2.rs @@ -10,6 +10,7 @@ use inflector::Inflector; use rapier_testbed2d::Testbed; use std::cmp::Ordering; +mod add_remove2; mod balls2; mod boxes2; mod capsules2; @@ -56,6 +57,7 @@ pub fn main() { .to_camel_case(); let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ + ("Add remove", add_remove2::init_world), ("Balls", balls2::init_world), ("Boxes", boxes2::init_world), ("Capsules", capsules2::init_world), diff --git a/examples2d/kinematic2.rs b/examples2d/kinematic2.rs index d979cd0..c40134b 100644 --- a/examples2d/kinematic2.rs +++ b/examples2d/kinematic2.rs @@ -62,8 +62,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/examples2d/sensor2.rs b/examples2d/sensor2.rs index 75634a8..c7e0205 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -69,15 +69,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