diff options
Diffstat (limited to 'examples3d')
| -rw-r--r-- | examples3d/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples3d/all_examples3.rs | 4 | ||||
| -rw-r--r-- | examples3d/collision_groups3.rs | 102 | ||||
| -rw-r--r-- | examples3d/debug_boxes3.rs | 32 | ||||
| -rw-r--r-- | examples3d/debug_cylinder3.rs | 65 | ||||
| -rw-r--r-- | examples3d/heightfield3.rs | 18 | ||||
| -rw-r--r-- | examples3d/primitives3.rs | 27 | ||||
| -rw-r--r-- | examples3d/trimesh3.rs | 18 |
8 files changed, 228 insertions, 40 deletions
diff --git a/examples3d/Cargo.toml b/examples3d/Cargo.toml index efc3cce..5362554 100644 --- a/examples3d/Cargo.toml +++ b/examples3d/Cargo.toml @@ -14,7 +14,7 @@ enhanced-determinism = [ "rapier3d/enhanced-determinism" ] [dependencies] rand = "0.7" Inflector = "0.11" -nalgebra = "0.22" +nalgebra = "0.23" [dependencies.rapier_testbed3d] path = "../build/rapier_testbed3d" diff --git a/examples3d/all_examples3.rs b/examples3d/all_examples3.rs index 64eb5b6..eeb9736 100644 --- a/examples3d/all_examples3.rs +++ b/examples3d/all_examples3.rs @@ -11,8 +11,10 @@ use rapier_testbed3d::Testbed; use std::cmp::Ordering; mod add_remove3; +mod collision_groups3; mod compound3; mod debug_boxes3; +mod debug_cylinder3; mod debug_triangle3; mod debug_trimesh3; mod domino3; @@ -64,6 +66,7 @@ pub fn main() { let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ ("Add remove", add_remove3::init_world), ("Primitives", primitives3::init_world), + ("Collision groups", collision_groups3::init_world), ("Compound", compound3::init_world), ("Domino", domino3::init_world), ("Heightfield", heightfield3::init_world), @@ -76,6 +79,7 @@ pub fn main() { ("(Debug) boxes", debug_boxes3::init_world), ("(Debug) triangle", debug_triangle3::init_world), ("(Debug) trimesh", debug_trimesh3::init_world), + ("(Debug) cylinder", debug_cylinder3::init_world), ]; // Lexicographic sort, with stress tests moved at the end of the list. diff --git a/examples3d/collision_groups3.rs b/examples3d/collision_groups3.rs new file mode 100644 index 0000000..625bc50 --- /dev/null +++ b/examples3d/collision_groups3.rs @@ -0,0 +1,102 @@ +use na::Point3; +use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier3d::geometry::{ColliderBuilder, ColliderSet, InteractionGroups}; +use rapier_testbed3d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = 5.0; + let ground_height = 0.1; + + let rigid_body = RigidBodyBuilder::new_static() + .translation(0.0, -ground_height, 0.0) + .build(); + let floor_handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size).build(); + colliders.insert(collider, floor_handle, &mut bodies); + + /* + * Setup groups + */ + const GREEN_GROUP: InteractionGroups = InteractionGroups::new(0b01, 0b01); + const BLUE_GROUP: InteractionGroups = InteractionGroups::new(0b10, 0b10); + + /* + * A green floor that will collide with the GREEN group only. + */ + let green_floor = ColliderBuilder::cuboid(1.0, 0.1, 1.0) + .translation(0.0, 1.0, 0.0) + .collision_groups(GREEN_GROUP) + .build(); + let green_collider_handle = colliders.insert(green_floor, floor_handle, &mut bodies); + + testbed.set_collider_initial_color(green_collider_handle, Point3::new(0.0, 1.0, 0.0)); + + /* + * A blue floor that will collide with the BLUE group only. + */ + let blue_floor = ColliderBuilder::cuboid(1.0, 0.1, 1.0) + .translation(0.0, 2.0, 0.0) + .collision_groups(BLUE_GROUP) + .build(); + let blue_collider_handle = colliders.insert(blue_floor, floor_handle, &mut bodies); + + testbed.set_collider_initial_color(blue_collider_handle, Point3::new(0.0, 0.0, 1.0)); + + /* + * Create the cubes + */ + let num = 8; + let rad = 0.1; + + let shift = rad * 2.0; + let centerx = shift * (num / 2) as f32; + let centery = 2.5; + let centerz = shift * (num / 2) as f32; + + for j in 0usize..4 { + for i in 0..num { + for k in 0usize..num { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery; + let z = k as f32 * shift - centerz; + + // Alternate between the green and blue groups. + let (group, color) = if k % 2 == 0 { + (GREEN_GROUP, Point3::new(0.0, 1.0, 0.0)) + } else { + (BLUE_GROUP, Point3::new(0.0, 0.0, 1.0)) + }; + + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(rad, rad, rad) + .collision_groups(group) + .build(); + colliders.insert(collider, handle, &mut bodies); + + testbed.set_body_color(handle, color); + } + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point3::new(10.0, 10.0, 10.0), Point3::origin()); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]); + testbed.run() +} diff --git a/examples3d/debug_boxes3.rs b/examples3d/debug_boxes3.rs index 7237fd9..919cdd6 100644 --- a/examples3d/debug_boxes3.rs +++ b/examples3d/debug_boxes3.rs @@ -17,22 +17,26 @@ pub fn init_world(testbed: &mut Testbed) { let ground_size = 100.1; let ground_height = 0.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); + for _ in 0..6 { + 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); + } // Build the dynamic box rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(1.1, 0.0, 0.0) - .rotation(Vector3::new(0.8, 0.2, 0.1)) - .can_sleep(false) - .build(); - let handle = bodies.insert(rigid_body); - let collider = ColliderBuilder::cuboid(2.0, 0.1, 1.0).build(); - colliders.insert(collider, handle, &mut bodies); + for _ in 0..6 { + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(1.1, 0.0, 0.0) + .rotation(Vector3::new(0.8, 0.2, 0.1)) + .can_sleep(false) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(2.0, 0.1, 1.0).build(); + colliders.insert(collider, handle, &mut bodies); + } /* * Set up the testbed. diff --git a/examples3d/debug_cylinder3.rs b/examples3d/debug_cylinder3.rs new file mode 100644 index 0000000..0717c67 --- /dev/null +++ b/examples3d/debug_cylinder3.rs @@ -0,0 +1,65 @@ +use na::Point3; +use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier3d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed3d::Testbed; + +// This shows a bug when a cylinder is in contact with a very large +// but very thin cuboid. In this case the EPA returns an incorrect +// contact normal, resulting in the cylinder falling through the floor. +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let joints = JointSet::new(); + + /* + * Ground + */ + let ground_size = 100.1; + let ground_height = 0.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); + + /* + * Create the cubes + */ + let num = 1; + let rad = 1.0; + + let shiftx = rad * 2.0 + rad; + let shifty = rad * 2.0 + rad; + let shiftz = rad * 2.0 + rad; + let centerx = shiftx * (num / 2) as f32; + let centery = shifty / 2.0; + let centerz = shiftz * (num / 2) as f32; + + let offset = -(num as f32) * (rad * 2.0 + rad) * 0.5; + + let x = -centerx + offset; + let y = centery + 3.0; + let z = -centerz + offset; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cylinder(rad, rad).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point3::new(100.0, 100.0, 100.0), Point3::origin()); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]); + testbed.run() +} diff --git a/examples3d/heightfield3.rs b/examples3d/heightfield3.rs index 09df815..6af93c7 100644 --- a/examples3d/heightfield3.rs +++ b/examples3d/heightfield3.rs @@ -54,13 +54,17 @@ pub fn init_world(testbed: &mut Testbed) { let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); let handle = bodies.insert(rigid_body); - if j % 2 == 0 { - let collider = ColliderBuilder::cuboid(rad, rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); - } else { - let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, handle, &mut bodies); - } + let collider = match j % 5 { + 0 => ColliderBuilder::cuboid(rad, rad, rad).build(), + 1 => ColliderBuilder::ball(rad).build(), + // Rounded cylinders are much more efficient that cylinder, even if the + // rounding margin is small. + 2 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(), + 3 => ColliderBuilder::cone(rad, rad).build(), + _ => ColliderBuilder::capsule_y(rad, rad).build(), + }; + + colliders.insert(collider, handle, &mut bodies); } } } diff --git a/examples3d/primitives3.rs b/examples3d/primitives3.rs index cf678b9..db15341 100644 --- a/examples3d/primitives3.rs +++ b/examples3d/primitives3.rs @@ -15,7 +15,7 @@ pub fn init_world(testbed: &mut Testbed) { * Ground */ let ground_size = 100.1; - let ground_height = 0.1; + let ground_height = 2.1; let rigid_body = RigidBodyBuilder::new_static() .translation(0.0, -ground_height, 0.0) @@ -30,29 +30,34 @@ pub fn init_world(testbed: &mut Testbed) { let num = 8; let rad = 1.0; - let shift = rad * 2.0 + rad; - let centerx = shift * (num / 2) as f32; - let centery = shift / 2.0; - let centerz = shift * (num / 2) as f32; + let shiftx = rad * 2.0 + rad; + let shifty = rad * 2.0 + rad; + let shiftz = rad * 2.0 + rad; + let centerx = shiftx * (num / 2) as f32; + let centery = shifty / 2.0; + let centerz = shiftz * (num / 2) as f32; let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5; for j in 0usize..20 { for i in 0..num { for k in 0usize..num { - let x = i as f32 * shift - centerx + offset; - let y = j as f32 * shift + centery + 3.0; - let z = k as f32 * shift - centerz + offset; + let x = i as f32 * shiftx - centerx + offset; + let y = j as f32 * shifty + centery + 3.0; + let z = k as f32 * shiftz - centerz + offset; // Build the rigid body. let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); let handle = bodies.insert(rigid_body); - let collider = match j % 2 { + let collider = match j % 5 { 0 => ColliderBuilder::cuboid(rad, rad, rad).build(), 1 => ColliderBuilder::ball(rad).build(), - // 2 => ColliderBuilder::capsule_y(rad, rad).build(), - _ => unreachable!(), + // Rounded cylinders are much more efficient that cylinder, even if the + // rounding margin is small. + 2 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(), + 3 => ColliderBuilder::cone(rad, rad).build(), + _ => ColliderBuilder::capsule_y(rad, rad).build(), }; colliders.insert(collider, handle, &mut bodies); diff --git a/examples3d/trimesh3.rs b/examples3d/trimesh3.rs index d022a1f..2a96bda 100644 --- a/examples3d/trimesh3.rs +++ b/examples3d/trimesh3.rs @@ -64,13 +64,17 @@ pub fn init_world(testbed: &mut Testbed) { let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); let handle = bodies.insert(rigid_body); - if j % 2 == 0 { - let collider = ColliderBuilder::cuboid(rad, rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); - } else { - let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, handle, &mut bodies); - } + let collider = match j % 5 { + 0 => ColliderBuilder::cuboid(rad, rad, rad).build(), + 1 => ColliderBuilder::ball(rad).build(), + // Rounded cylinders are much more efficient that cylinder, even if the + // rounding margin is small. + 2 => ColliderBuilder::round_cylinder(rad, rad, rad / 10.0).build(), + 3 => ColliderBuilder::cone(rad, rad).build(), + _ => ColliderBuilder::capsule_y(rad, rad).build(), + }; + + colliders.insert(collider, handle, &mut bodies); } } } |
