diff options
| -rw-r--r-- | examples2d/all_examples2.rs | 4 | ||||
| -rw-r--r-- | examples2d/convex_polygons2.rs | 86 | ||||
| -rw-r--r-- | examples2d/polyline2.rs | 74 | ||||
| -rw-r--r-- | examples3d/all_examples3.rs | 2 | ||||
| -rw-r--r-- | examples3d/convex_polyhedron3.rs | 78 | ||||
| -rw-r--r-- | src/geometry/collider.rs | 5 | ||||
| -rw-r--r-- | src_testbed/engine.rs | 10 | ||||
| -rw-r--r-- | src_testbed/objects/mod.rs | 1 | ||||
| -rw-r--r-- | src_testbed/objects/node.rs | 23 | ||||
| -rw-r--r-- | src_testbed/objects/polyline.rs | 64 |
10 files changed, 294 insertions, 53 deletions
diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs index e0b6631..5040c8a 100644 --- a/examples2d/all_examples2.rs +++ b/examples2d/all_examples2.rs @@ -12,12 +12,14 @@ use std::cmp::Ordering; mod add_remove2; mod collision_groups2; +mod convex_polygons2; mod damping2; mod debug_box_ball2; mod heightfield2; mod joints2; mod locked_rotations2; mod platform2; +mod polyline2; mod pyramid2; mod restitution2; mod sensor2; @@ -58,11 +60,13 @@ pub fn main() { let mut builders: Vec<(_, fn(&mut Testbed))> = vec![ ("Add remove", add_remove2::init_world), ("Collision groups", collision_groups2::init_world), + ("Convex polygons", convex_polygons2::init_world), ("Damping", damping2::init_world), ("Heightfield", heightfield2::init_world), ("Joints", joints2::init_world), ("Locked rotations", locked_rotations2::init_world), ("Platform", platform2::init_world), + ("Polyline", polyline2::init_world), ("Pyramid", pyramid2::init_world), ("Restitution", restitution2::init_world), ("Sensor", sensor2::init_world), diff --git a/examples2d/convex_polygons2.rs b/examples2d/convex_polygons2.rs new file mode 100644 index 0000000..d936fb6 --- /dev/null +++ b/examples2d/convex_polygons2.rs @@ -0,0 +1,86 @@ +use na::Point2; +use rand::distributions::{Distribution, Standard}; +use rand::{rngs::StdRng, SeedableRng}; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::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 = 30.0; + + let rigid_body = RigidBodyBuilder::new_static().build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(ground_size, ground_size * 2.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + let rigid_body = RigidBodyBuilder::new_static() + .rotation(std::f32::consts::FRAC_PI_2) + .translation(-ground_size, ground_size * 2.0) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size * 2.0, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the convex polygons + */ + let num = 14; + let scale = 4.0; + let border_rad = 0.0; + + let shift = border_rad * 2.0 + scale; + let centerx = shift * (num as f32) / 2.0; + let centery = shift / 2.0; + + let mut rng = StdRng::seed_from_u64(0); + let distribution = Standard; + + for i in 0..num { + for j in 0usize..num * 4 { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift * 2.0 + centery + 2.0; + + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + + let mut points = Vec::new(); + + for _ in 0..10 { + let pt: Point2<f32> = distribution.sample(&mut rng); + points.push(pt * scale); + } + + let collider = ColliderBuilder::convex_hull(&points).unwrap().build(); + colliders.insert(collider, handle, &mut bodies); + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 50.0), 10.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} diff --git a/examples2d/polyline2.rs b/examples2d/polyline2.rs new file mode 100644 index 0000000..3aa4a47 --- /dev/null +++ b/examples2d/polyline2.rs @@ -0,0 +1,74 @@ +use na::{ComplexField, Point2}; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::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 = 50.0; + let nsubdivs = 2000; + let step_size = ground_size / (nsubdivs as f32); + let mut points = Vec::new(); + + points.push(Point2::new(-ground_size / 2.0, 40.0)); + for i in 1..nsubdivs - 1 { + let x = -ground_size / 2.0 + i as f32 * step_size; + let y = ComplexField::cos(i as f32 * step_size) * 2.0; + points.push(Point2::new(x, y)); + } + points.push(Point2::new(ground_size / 2.0, 40.0)); + + let rigid_body = RigidBodyBuilder::new_static().build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::polyline(points, None).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the cubes + */ + let num = 20; + let rad = 0.5; + + let shift = rad * 2.0; + let centerx = shift * (num / 2) as f32; + let centery = shift / 2.0; + + for i in 0..num { + for j in 0usize..num { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery + 3.0; + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let handle = bodies.insert(rigid_body); + + if j % 2 == 0 { + let collider = ColliderBuilder::cuboid(rad, rad).build(); + colliders.insert(collider, handle, &mut bodies); + } else { + let collider = ColliderBuilder::ball(rad).build(); + colliders.insert(collider, handle, &mut bodies); + } + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 0.0), 10.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Heightfield", init_world)]); + testbed.run() +} diff --git a/examples3d/all_examples3.rs b/examples3d/all_examples3.rs index ff5a3e8..9cbf4c3 100644 --- a/examples3d/all_examples3.rs +++ b/examples3d/all_examples3.rs @@ -13,6 +13,7 @@ use std::cmp::Ordering; mod collision_groups3; mod compound3; mod convex_decomposition3; +mod convex_polyhedron3; mod damping3; mod debug_add_remove_collider3; mod debug_boxes3; @@ -76,6 +77,7 @@ pub fn main() { ("Collision groups", collision_groups3::init_world), ("Compound", compound3::init_world), ("Convex decomposition", convex_decomposition3::init_world), + ("Convex polyhedron", convex_polyhedron3::init_world), ("Damping", damping3::init_world), ("Domino", domino3::init_world), ("Heightfield", heightfield3::init_world), diff --git a/examples3d/convex_polyhedron3.rs b/examples3d/convex_polyhedron3.rs new file mode 100644 index 0000000..3cbcbf1 --- /dev/null +++ b/examples3d/convex_polyhedron3.rs @@ -0,0 +1,78 @@ +use na::Point3; +use rand::distributions::{Distribution, Standard}; +use rand::{rngs::StdRng, SeedableRng}; +use rapier3d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier3d::geometry::{ColliderBuilder, ColliderSet}; +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 = 40.0; + 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 polyhedra + */ + let num = 5; + let scale = 2.0; + let border_rad = 0.1; + + let shift = border_rad * 2.0 + scale; + let centerx = shift * (num / 2) as f32; + let centery = shift / 2.0; + let centerz = shift * (num / 2) as f32; + + let mut rng = StdRng::seed_from_u64(0); + let distribution = Standard; + + for j in 0usize..25 { + for i in 0..num { + for k in 0usize..num { + let x = i as f32 * shift - centerx; + let y = j as f32 * shift + centery + 3.0; + let z = k as f32 * shift - centerz; + + let mut points = Vec::new(); + for _ in 0..10 { + let pt: Point3<f32> = distribution.sample(&mut rng); + points.push(pt * scale); + } + + // Build the rigid body. + let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::round_convex_hull(&points, border_rad) + .unwrap() + .build(); + colliders.insert(collider, handle, &mut bodies); + } + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point3::new(30.0, 30.0, 30.0), Point3::origin()); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]); + testbed.run() +} diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs index 138b9b8..8924a3d 100644 --- a/src/geometry/collider.rs +++ b/src/geometry/collider.rs @@ -299,6 +299,11 @@ impl ColliderBuilder { Self::new(SharedShape::round_triangle(a, b, c, border_radius)) } + /// Initializes a collider builder with a polyline shape defined by its vertex and index buffers. + pub fn polyline(vertices: Vec<Point<Real>>, indices: Option<Vec<[u32; 2]>>) -> Self { + Self::new(SharedShape::polyline(vertices, indices)) + } + /// Initializes a collider builder with a triangle mesh shape defined by its vertex and index buffers. pub fn trimesh(vertices: Vec<Point<Real>>, indices: Vec<[u32; 3]>) -> Self { Self::new(SharedShape::trimesh(vertices, indices)) diff --git a/src_testbed/engine.rs b/src_testbed/engine.rs index cef12d6..bcdbbca 100644 --- a/src_testbed/engine.rs +++ b/src_testbed/engine.rs @@ -26,6 +26,7 @@ use crate::objects::cone::Cone; #[cfg(feature = "dim3")] use crate::objects::cylinder::Cylinder; use crate::objects::mesh::Mesh; +use crate::objects::polyline::Polyline; use rand::{Rng, SeedableRng}; use rand_pcg::Pcg32; use std::collections::HashMap; @@ -352,6 +353,15 @@ impl GraphicsManager { ))) } + if let Some(polyline) = shape.as_polyline() { + out.push(Node::Polyline(Polyline::new( + handle, + polyline.vertices().to_vec(), + polyline.indices().to_vec(), + color, + ))) + } + if let Some(heightfield) = shape.as_heightfield() { out.push(Node::HeightField(HeightField::new( handle, diff --git a/src_testbed/objects/mod.rs b/src_testbed/objects/mod.rs index e4d7bc4..8e12dbe 100644 --- a/src_testbed/objects/mod.rs +++ b/src_testbed/objects/mod.rs @@ -7,6 +7,7 @@ pub mod cylinder; pub mod heightfield; pub mod mesh; pub mod node; +pub mod polyline; //pub mod plane; //#[cfg(feature = "dim2")] //pub mod polyline; diff --git a/src_testbed/objects/node.rs b/src_testbed/objects/node.rs index d1de799..64c8ad4 100644 --- a/src_testbed/objects/node.rs +++ b/src_testbed/objects/node.rs @@ -5,8 +5,7 @@ use crate::objects::convex::Convex; use crate::objects::heightfield::HeightField; use crate::objects::mesh::Mesh; //use crate::objects::plane::Plane; -//#[cfg(feature = "dim2")] -//use crate::objects::polyline::Polyline; +use crate::objects::polyline::Polyline; use kiss3d::window::Window; use na::Point3; @@ -26,8 +25,7 @@ pub enum Node { Box(Box), HeightField(HeightField), Capsule(Capsule), - // #[cfg(feature = "dim2")] - // Polyline(Polyline), + Polyline(Polyline), Mesh(Mesh), Convex(Convex), Cylinder(Cylinder), @@ -42,8 +40,7 @@ impl Node { Node::Box(ref mut n) => n.select(), Node::Capsule(ref mut n) => n.select(), Node::HeightField(ref mut n) => n.select(), - // #[cfg(feature = "dim2")] - // Node::Polyline(ref mut n) => n.select(), + Node::Polyline(ref mut n) => n.select(), Node::Mesh(ref mut n) => n.select(), Node::Convex(ref mut n) => n.select(), Node::Cylinder(ref mut n) => n.select(), @@ -58,8 +55,7 @@ impl Node { Node::Box(ref mut n) => n.unselect(), Node::Capsule(ref mut n) => n.unselect(), Node::HeightField(ref mut n) => n.unselect(), - // #[cfg(feature = "dim2")] - // Node::Polyline(ref mut n) => n.unselect(), + Node::Polyline(ref mut n) => n.unselect(), Node::Mesh(ref mut n) => n.unselect(), Node::Convex(ref mut n) => n.unselect(), Node::Cylinder(ref mut n) => n.unselect(), @@ -74,8 +70,7 @@ impl Node { Node::Box(ref mut n) => n.update(colliders), Node::Capsule(ref mut n) => n.update(colliders), Node::HeightField(ref mut n) => n.update(colliders), - // #[cfg(feature = "dim2")] - // Node::Polyline(ref mut n) => n.update(colliders), + Node::Polyline(ref mut n) => n.update(colliders), Node::Mesh(ref mut n) => n.update(colliders), Node::Convex(ref mut n) => n.update(colliders), Node::Cylinder(ref mut n) => n.update(colliders), @@ -86,7 +81,7 @@ impl Node { #[cfg(feature = "dim2")] pub fn draw(&mut self, window: &mut Window) { match *self { - // Node::Polyline(ref mut n) => n.draw(_window), + Node::Polyline(ref mut n) => n.draw(window), Node::HeightField(ref mut n) => n.draw(window), // Node::Plane(ref mut n) => n.draw(_window), _ => {} @@ -139,8 +134,7 @@ impl Node { Node::Box(ref n) => n.object(), Node::Capsule(ref n) => n.object(), Node::HeightField(ref n) => n.object(), - // #[cfg(feature = "dim2")] - // Node::Polyline(ref n) => n.object(), + Node::Polyline(ref n) => n.object(), Node::Mesh(ref n) => n.object(), Node::Convex(ref n) => n.object(), Node::Cylinder(ref n) => n.object(), @@ -155,8 +149,7 @@ impl Node { Node::Box(ref mut n) => n.set_color(color), Node::Capsule(ref mut n) => n.set_color(color), Node::HeightField(ref mut n) => n.set_color(color), - // #[cfg(feature = "dim2")] - // Node::Polyline(ref mut n) => n.set_color(color), + Node::Polyline(ref mut n) => n.set_color(color), Node::Mesh(ref mut n) => n.set_color(color), Node::Convex(ref mut n) => n.set_color(color), Node::Cylinder(ref mut n) => n.set_color(color), diff --git a/src_testbed/objects/polyline.rs b/src_testbed/objects/polyline.rs index cdc0f21..98a8f24 100644 --- a/src_testbed/objects/polyline.rs +++ b/src_testbed/objects/polyline.rs @@ -1,38 +1,32 @@ use kiss3d::window::Window; -use na::{Isometry2, Point2, Point3}; -use nphysics2d::object::{ColliderAnchor, DefaultColliderHandle, DefaultColliderSet}; -use parry2d::shape; +use na::Point3; +use rapier::geometry::{ColliderHandle, ColliderSet}; +use rapier::math::{Isometry, Point}; pub struct Polyline { color: Point3<f32>, base_color: Point3<f32>, - vertices: Vec<Point2<f32>>, - indices: Vec<Point2<usize>>, - collider: DefaultColliderHandle, - pos: Isometry2<f32>, + vertices: Vec<Point<f32>>, + indices: Vec<[u32; 2]>, + collider: ColliderHandle, + pos: Isometry<f32>, } impl Polyline { pub fn new( - collider: DefaultColliderHandle, - colliders: &DefaultColliderSet<f32>, - _: Isometry2<f32>, - vertices: Vec<Point2<f32>>, - indices: Vec<Point2<usize>>, + collider: ColliderHandle, + vertices: Vec<Point<f32>>, + indices: Vec<[u32; 2]>, color: Point3<f32>, - _: &mut Window, ) -> Polyline { - let mut res = Polyline { + Polyline { color, - pos: Isometry2::identity(), + pos: Isometry::identity(), base_color: color, vertices, indices, collider, - }; - - res.update(colliders); - res + } } pub fn select(&mut self) { @@ -48,32 +42,26 @@ impl Polyline { self.base_color = color; } - pub fn update(&mut self, colliders: &DefaultColliderSet<f32>) { - // Update if some deformation occurred. - // FIXME: don't update if it did not move. - if let Some(c) = colliders.get(self.collider) { - self.pos = *c.position(); - if let ColliderAnchor::OnDeformableBody { .. } = c.anchor() { - let shape = c.shape().as_shape::<shape::Polyline<f32>>().unwrap(); - self.vertices = shape.points().to_vec(); - self.indices.clear(); - - for e in shape.edges() { - self.indices.push(e.indices); - } - } - } + pub fn update(&mut self, colliders: &ColliderSet) { + self.pos = colliders + .get(self.collider) + .map(|c| *c.position()) + .unwrap_or(Isometry::identity()); } - pub fn object(&self) -> DefaultColliderHandle { + pub fn object(&self) -> ColliderHandle { self.collider } pub fn draw(&mut self, window: &mut Window) { for idx in &self.indices { - let p1 = self.pos * self.vertices[idx.x]; - let p2 = self.pos * self.vertices[idx.y]; - window.draw_planar_line(&p1, &p2, &self.color) + let p1 = self.pos * self.vertices[idx[0] as usize]; + let p2 = self.pos * self.vertices[idx[1] as usize]; + + #[cfg(feature = "dim2")] + window.draw_planar_line(&p1, &p2, &self.color); + #[cfg(feature = "dim3")] + window.draw_line(&p1, &p2, &self.color); } } } |
