From 3379094f5af20187d7ee0d50717810956fa32254 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 24 Nov 2020 15:02:39 +0100 Subject: Minor demos changes. --- examples3d/add_remove3.rs | 87 --------------------------------------------- examples3d/all_examples3.rs | 4 +-- examples3d/fountain3.rs | 85 +++++++++++++++++++++++++++++++++++++++++++ examples3d/heightfield3.rs | 2 +- examples3d/joints3.rs | 10 ++---- 5 files changed, 90 insertions(+), 98 deletions(-) delete mode 100644 examples3d/add_remove3.rs create mode 100644 examples3d/fountain3.rs (limited to 'examples3d') diff --git a/examples3d/add_remove3.rs b/examples3d/add_remove3.rs deleted file mode 100644 index 77350e5..0000000 --- a/examples3d/add_remove3.rs +++ /dev/null @@ -1,87 +0,0 @@ -use na::Point3; -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 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 = 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); - - if physics.bodies.len() > MAX_NUMBER_OF_BODIES { - let mut to_remove: Vec<_> = physics - .bodies - .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)); -} - -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 d0efa0e..88f3f8f 100644 --- a/examples3d/all_examples3.rs +++ b/examples3d/all_examples3.rs @@ -10,7 +10,6 @@ use inflector::Inflector; use rapier_testbed3d::Testbed; use std::cmp::Ordering; -mod add_remove3; mod collision_groups3; mod compound3; mod damping3; @@ -20,6 +19,7 @@ mod debug_infinite_fall3; mod debug_triangle3; mod debug_trimesh3; mod domino3; +mod fountain3; mod heightfield3; mod joints3; mod keva3; @@ -67,7 +67,7 @@ pub fn main() { .to_camel_case(); let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ - ("Add remove", add_remove3::init_world), + ("Fountain", fountain3::init_world), ("Primitives", primitives3::init_world), ("Collision groups", collision_groups3::init_world), ("Compound", compound3::init_world), diff --git a/examples3d/fountain3.rs b/examples3d/fountain3.rs new file mode 100644 index 0000000..fafa988 --- /dev/null +++ b/examples3d/fountain3.rs @@ -0,0 +1,85 @@ +use na::Point3; +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 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 = 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); + + if physics.bodies.len() > MAX_NUMBER_OF_BODIES { + let mut to_remove: Vec<_> = physics + .bodies + .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); + } + } + }); + + /* + * 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/heightfield3.rs b/examples3d/heightfield3.rs index 59380d5..6558614 100644 --- a/examples3d/heightfield3.rs +++ b/examples3d/heightfield3.rs @@ -27,7 +27,7 @@ pub fn init_world(testbed: &mut Testbed) { // NOTE: make sure we use the sin/cos from simba to ensure // cross-platform determinism of the example when the // enhanced_determinism feature is enabled. - (::sin(x) + ::cos(z)) + ::sin(x) + ::cos(z) } }); diff --git a/examples3d/joints3.rs b/examples3d/joints3.rs index d4a797a..dd56958 100644 --- a/examples3d/joints3.rs +++ b/examples3d/joints3.rs @@ -25,14 +25,11 @@ fn create_prismatic_joints( for i in 0..num { let z = origin.z + (i + 1) as f32 * shift; - let density = 1.0; let rigid_body = RigidBodyBuilder::new_dynamic() .translation(origin.x, origin.y, z) .build(); let curr_child = bodies.insert(rigid_body); - let collider = ColliderBuilder::cuboid(rad, rad, rad) - .density(density) - .build(); + let collider = ColliderBuilder::cuboid(rad, rad, rad).build(); colliders.insert(collider, curr_child, bodies); let axis = if i % 2 == 0 { @@ -88,14 +85,11 @@ fn create_revolute_joints( let mut handles = [curr_parent; 4]; for k in 0..4 { - let density = 1.0; let rigid_body = RigidBodyBuilder::new_dynamic() .position(positions[k]) .build(); handles[k] = bodies.insert(rigid_body); - let collider = ColliderBuilder::cuboid(rad, rad, rad) - .density(density) - .build(); + let collider = ColliderBuilder::cuboid(rad, rad, rad).build(); colliders.insert(collider, handles[k], bodies); } -- cgit