From 5ce36065829cdc23334bbb6ca6c0d83f7de1ece8 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 19 Nov 2020 18:09:55 +0100 Subject: Add explicit wake_up parameter to method setting the position and velocity of a rigid-body. --- examples3d/add_remove3.rs | 67 +++++++++++++++++++++++++++++++++++++---------- examples3d/damping3.rs | 2 +- examples3d/platform3.rs | 2 +- 3 files changed, 55 insertions(+), 16 deletions(-) (limited to 'examples3d') diff --git a/examples3d/add_remove3.rs b/examples3d/add_remove3.rs index 6b58adf..77350e5 100644 --- a/examples3d/add_remove3.rs +++ b/examples3d/add_remove3.rs @@ -3,43 +3,82 @@ use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; use rapier3d::geometry::{ColliderBuilder, ColliderSet}; use rapier_testbed3d::Testbed; +const MAX_NUMBER_OF_BODIES: usize = 400; + pub fn init_world(testbed: &mut Testbed) { - let bodies = RigidBodySet::new(); - let colliders = ColliderSet::new(); + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); let joints = JointSet::new(); let rad = 0.5; + /* + * Ground + */ + let ground_size = 100.1; + let ground_height = 2.1; + + let rigid_body = RigidBodyBuilder::new_static() + .translation(0.0, -ground_height, 0.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size).build(); + colliders.insert(collider, handle, &mut bodies); + let mut k = 0; + // Callback that will be executed on the main loop to handle proximities. testbed.add_callback(move |window, physics, _, graphics, _| { + k += 1; 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).build(); + let collider = match k % 3 { + 0 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(), + 1 => ColliderBuilder::cone(rad, rad).build(), + _ => ColliderBuilder::cuboid(rad, rad, rad).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 + if physics.bodies.len() > MAX_NUMBER_OF_BODIES { + let mut to_remove: Vec<_> = physics .bodies - .remove(handle, &mut physics.colliders, &mut physics.joints); - graphics.remove_body_nodes(window, handle); + .iter() + .filter(|e| e.1.is_dynamic()) + .map(|e| (e.0, e.1.position().translation.vector)) + .collect(); + + to_remove.sort_by(|a, b| { + (a.1.x.abs() + a.1.z.abs()) + .partial_cmp(&(b.1.x.abs() + b.1.z.abs())) + .unwrap() + .reverse() + }); + + let num_to_remove = to_remove.len() - MAX_NUMBER_OF_BODIES; + for (handle, _) in &to_remove[..num_to_remove] { + physics + .bodies + .remove(*handle, &mut physics.colliders, &mut physics.joints); + physics.broad_phase.maintain(&mut physics.colliders); + physics + .narrow_phase + .maintain(&mut physics.colliders, &mut physics.bodies); + graphics.remove_body_nodes(window, *handle); + } } + + println!("Num bodies: {}", physics.bodies.len()); }); /* * 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)); + testbed.look_at(Point3::new(-30.0, 4.0, -30.0), Point3::new(0.0, 1.0, 0.0)); } fn main() { diff --git a/examples3d/damping3.rs b/examples3d/damping3.rs index e055d8e..8c68d3b 100644 --- a/examples3d/damping3.rs +++ b/examples3d/damping3.rs @@ -12,7 +12,7 @@ pub fn init_world(testbed: &mut Testbed) { let joints = JointSet::new(); /* - * Create the balls + * Create the cubes */ let num = 10; let rad = 0.2; diff --git a/examples3d/platform3.rs b/examples3d/platform3.rs index 6c3483e..0843300 100644 --- a/examples3d/platform3.rs +++ b/examples3d/platform3.rs @@ -72,7 +72,7 @@ pub fn init_world(testbed: &mut Testbed) { } if let Some(mut platform) = physics.bodies.get_mut(platform_handle) { - let mut next_pos = platform.position; + let mut next_pos = *platform.position(); let dt = 0.016; next_pos.translation.vector.y += (time * 5.0).sin() * dt; -- cgit