aboutsummaryrefslogtreecommitdiff
path: root/examples3d/trimesh3.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples3d/trimesh3.rs')
-rw-r--r--examples3d/trimesh3.rs64
1 files changed, 39 insertions, 25 deletions
diff --git a/examples3d/trimesh3.rs b/examples3d/trimesh3.rs
index 0cee465..55002d9 100644
--- a/examples3d/trimesh3.rs
+++ b/examples3d/trimesh3.rs
@@ -1,6 +1,6 @@
-use na::{ComplexField, Point3};
+use na::{ComplexField, DMatrix, Isometry3, Point3, Vector3};
use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
-use rapier3d::geometry::{ColliderBuilder, ColliderSet};
+use rapier3d::geometry::{ColliderBuilder, ColliderSet, HeightField, SharedShape};
use rapier_testbed3d::Testbed;
pub fn init_world(testbed: &mut Testbed) {
@@ -14,31 +14,27 @@ pub fn init_world(testbed: &mut Testbed) {
/*
* Ground
*/
- let ground_size = 100.0f32;
- let ground_height = 1.0;
+ let ground_size = Vector3::new(100.0, 1.0, 100.0);
let nsubdivs = 20;
- let quad = rapier3d::ncollide::procedural::quad(ground_size, ground_size, nsubdivs, nsubdivs);
- let indices = quad
- .flat_indices()
- .chunks(3)
- .map(|is| Point3::new(is[0], is[2], is[1]))
- .collect();
- let mut vertices = quad.coords;
+ let heights = DMatrix::from_fn(nsubdivs + 1, nsubdivs + 1, |i, j| {
+ if i == 0 || i == nsubdivs || j == 0 || j == nsubdivs {
+ 10.0
+ } else {
+ let x = i as f32 * ground_size.x / (nsubdivs as f32);
+ let z = j as f32 * ground_size.z / (nsubdivs as f32);
- // ncollide generates a quad with `z` as the normal.
- // so we switch z and y here and set a random altitude at each point.
- for p in vertices.iter_mut() {
- p.z = p.y;
- // 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.
- p.y = (<f32 as ComplexField>::sin(p.x) + <f32 as ComplexField>::cos(p.z)) * ground_height;
-
- if p.x.abs() == ground_size / 2.0 || p.z.abs() == ground_size / 2.0 {
- p.y = 10.0;
+ // 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.
+ <f32 as ComplexField>::sin(x) + <f32 as ComplexField>::cos(z)
}
- }
+ });
+
+ // Here we will build our trimesh from the mesh representation of an
+ // heightfield.
+ let heightfield = HeightField::new(heights, ground_size);
+ let (vertices, indices) = heightfield.to_trimesh();
let rigid_body = RigidBodyBuilder::new_static().build();
let handle = bodies.insert(rigid_body);
@@ -67,14 +63,32 @@ pub fn init_world(testbed: &mut Testbed) {
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build();
let handle = bodies.insert(rigid_body);
- let collider = match j % 5 {
+ let collider = match j % 6 {
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(),
+ 4 => ColliderBuilder::capsule_y(rad, rad).build(),
+ _ => {
+ let shapes = vec![
+ (
+ Isometry3::identity(),
+ SharedShape::cuboid(rad, rad / 2.0, rad / 2.0),
+ ),
+ (
+ Isometry3::translation(rad, 0.0, 0.0),
+ SharedShape::cuboid(rad / 2.0, rad, rad / 2.0),
+ ),
+ (
+ Isometry3::translation(-rad, 0.0, 0.0),
+ SharedShape::cuboid(rad / 2.0, rad, rad / 2.0),
+ ),
+ ];
+
+ ColliderBuilder::compound(shapes).build()
+ }
};
colliders.insert(collider, handle, &mut bodies);