aboutsummaryrefslogtreecommitdiff
path: root/examples3d/fountain3.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2020-11-24 16:54:15 +0100
committerGitHub <noreply@github.com>2020-11-24 16:54:15 +0100
commitbdf2e15fdcff4c4757b4875354b2d6e8b9c6939d (patch)
tree097166c76d92921b269b28a1e115b3cef89d820b /examples3d/fountain3.rs
parentc641114f016c47f6b22acc084610847f88ff5a66 (diff)
parentfcafcac66f1792ea155925e3de5055ef50910fb0 (diff)
downloadrapier-bdf2e15fdcff4c4757b4875354b2d6e8b9c6939d.tar.gz
rapier-bdf2e15fdcff4c4757b4875354b2d6e8b9c6939d.tar.bz2
rapier-bdf2e15fdcff4c4757b4875354b2d6e8b9c6939d.zip
Merge pull request #68 from dimforge/read_contacts
Allow access to contact information
Diffstat (limited to 'examples3d/fountain3.rs')
-rw-r--r--examples3d/fountain3.rs85
1 files changed, 85 insertions, 0 deletions
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()
+}