Fix BroadPhase proxy handle recycling causing a crash.

This commit is contained in:
Sébastien Crozet
2020-08-27 09:01:32 +02:00
parent cd3d4e0bff
commit 3b000f90bf
10 changed files with 308 additions and 148 deletions

53
examples2d/add_remove2.rs Normal file
View File

@@ -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()
}

View File

@@ -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),

View File

@@ -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;

View File

@@ -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);