diff options
Diffstat (limited to 'examples3d')
| -rw-r--r-- | examples3d/trimesh3.rs | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/examples3d/trimesh3.rs b/examples3d/trimesh3.rs index 0cee465..d753988 100644 --- a/examples3d/trimesh3.rs +++ b/examples3d/trimesh3.rs @@ -1,6 +1,6 @@ -use na::{ComplexField, Point3}; +use na::{ComplexField, DMatrix, Point3, Vector3}; use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier3d::geometry::{ColliderBuilder, ColliderSet}; +use rapier3d::geometry::{ColliderBuilder, ColliderSet, HeightField}; 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); |
