From 8d925a02ef97844bc937584a9095c1396daeee35 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Sun, 27 Dec 2020 18:14:22 +0100 Subject: Add convex polygons support. --- examples2d/sensor2.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'examples2d') diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 959ecbf..6649a43 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -1,6 +1,6 @@ use na::{Point2, Point3}; use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet, Proximity}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -70,10 +70,11 @@ pub fn init_world(testbed: &mut Testbed) { // Callback that will be executed on the main loop to handle proximities. testbed.add_callback(move |_, physics, events, graphics, _| { - while let Ok(prox) = events.proximity_events.try_recv() { - let color = match prox.new_status { - Proximity::WithinMargin | Proximity::Intersecting => Point3::new(1.0, 1.0, 0.0), - Proximity::Disjoint => Point3::new(0.5, 0.5, 1.0), + while let Ok(prox) = events.intersection_events.try_recv() { + let color = if prox.intersecting { + Point3::new(1.0, 1.0, 0.0) + } else { + Point3::new(0.5, 0.5, 1.0) }; let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); -- cgit From aa61fe65e3ff0289ecab57b4053a3410cf6d4a87 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 4 Jan 2021 15:14:25 +0100 Subject: Add support of 64-bits reals. --- examples2d/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples2d') diff --git a/examples2d/Cargo.toml b/examples2d/Cargo.toml index f1ed728..6580923 100644 --- a/examples2d/Cargo.toml +++ b/examples2d/Cargo.toml @@ -14,7 +14,7 @@ enhanced-determinism = [ "rapier2d/enhanced-determinism" ] [dependencies] rand = "0.7" Inflector = "0.11" -nalgebra = "0.23" +nalgebra = "0.24" [dependencies.rapier_testbed2d] path = "../build/rapier_testbed2d" -- cgit From cc60809afca7137bc192d56734ce12b385c32eda Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 6 Jan 2021 18:09:12 +0100 Subject: Add 2D trimesh example. --- examples2d/Cargo.toml | 2 + examples2d/all_examples2.rs | 2 + examples2d/trimesh2.rs | 267 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 examples2d/trimesh2.rs (limited to 'examples2d') diff --git a/examples2d/Cargo.toml b/examples2d/Cargo.toml index 6580923..34deb12 100644 --- a/examples2d/Cargo.toml +++ b/examples2d/Cargo.toml @@ -15,6 +15,8 @@ enhanced-determinism = [ "rapier2d/enhanced-determinism" ] rand = "0.7" Inflector = "0.11" nalgebra = "0.24" +lyon = "0.17" +usvg = "0.13" [dependencies.rapier_testbed2d] path = "../build/rapier_testbed2d" diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs index d6cf947..e0b6631 100644 --- a/examples2d/all_examples2.rs +++ b/examples2d/all_examples2.rs @@ -21,6 +21,7 @@ mod platform2; mod pyramid2; mod restitution2; mod sensor2; +mod trimesh2; fn demo_name_from_command_line() -> Option { let mut args = std::env::args(); @@ -65,6 +66,7 @@ pub fn main() { ("Pyramid", pyramid2::init_world), ("Restitution", restitution2::init_world), ("Sensor", sensor2::init_world), + ("Trimesh", trimesh2::init_world), ("(Debug) box ball", debug_box_ball2::init_world), ]; diff --git a/examples2d/trimesh2.rs b/examples2d/trimesh2.rs new file mode 100644 index 0000000..3176861 --- /dev/null +++ b/examples2d/trimesh2.rs @@ -0,0 +1,267 @@ +use na::{Point2, Point3}; +use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; +use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier_testbed2d::Testbed; + +use lyon::math::Point; +use lyon::path::PathEvent; +use lyon::tessellation::geometry_builder::*; +use lyon::tessellation::{self, FillOptions, FillTessellator}; +use usvg::prelude::*; + +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 = 25.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) + .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) + .build(); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, 1.2).build(); + colliders.insert(collider, handle, &mut bodies); + + /* + * Create the trimeshes from a tesselated SVG. + */ + let mut fill_tess = FillTessellator::new(); + let opt = usvg::Options::default(); + let rtree = usvg::Tree::from_str(RAPIER_SVG_STR, &opt).unwrap(); + let mut ith = 0; + + for node in rtree.root().descendants() { + if let usvg::NodeKind::Path(ref p) = *node.borrow() { + let transform = node.transform(); + if p.fill.is_some() { + let path = PathConvIter { + iter: p.data.iter(), + first: Point::new(0.0, 0.0), + prev: Point::new(0.0, 0.0), + deferred: None, + needs_end: false, + }; + + let mut mesh: VertexBuffers<_, u32> = VertexBuffers::new(); + fill_tess + .tessellate( + path, + &FillOptions::tolerance(0.01), + &mut BuffersBuilder::new(&mut mesh, VertexCtor { prim_id: 0 }), + ) + .expect("Tesselation failed."); + + let angle = transform.get_rotate() as f32; + + let (sx, sy) = ( + transform.get_scale().0 as f32 * 0.2, + transform.get_scale().1 as f32 * 0.2, + ); + + let indices: Vec<_> = mesh + .indices + .chunks(3) + .map(|v| Point3::new(v[0], v[1], v[2])) + .collect(); + let vertices: Vec<_> = mesh + .vertices + .iter() + .map(|v| Point2::new(v.position[0] * sx, v.position[1] * -sy)) + .collect(); + + for k in 0..5 { + let collider = + ColliderBuilder::trimesh(vertices.clone(), indices.clone()).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(ith as f32 * 8.0 - 20.0, 20.0 + k as f32 * 11.0) + .rotation(angle) + .build(); + let handle = bodies.insert(rigid_body); + colliders.insert(collider, handle, &mut bodies); + } + + ith += 1; + } + } + } + + /* + * Set up the testbed. + */ + testbed.set_world(bodies, colliders, joints); + testbed.look_at(Point2::new(0.0, 20.0), 17.0); +} + +fn main() { + let testbed = Testbed::from_builders(0, vec![("Balls", init_world)]); + testbed.run() +} + +const RAPIER_SVG_STR: &'static str = r#" + + + + + + + + + + + + + + + + + + + + + + + + + + + +"#; + +pub struct PathConvIter<'a> { + iter: std::slice::Iter<'a, usvg::PathSegment>, + prev: Point, + first: Point, + needs_end: bool, + deferred: Option, +} + +impl<'l> Iterator for PathConvIter<'l> { + type Item = PathEvent; + fn next(&mut self) -> Option { + if self.deferred.is_some() { + return self.deferred.take(); + } + + let next = self.iter.next(); + match next { + Some(usvg::PathSegment::MoveTo { x, y }) => { + if self.needs_end { + let last = self.prev; + let first = self.first; + self.needs_end = false; + self.prev = Point::new(*x as f32, *y as f32); + self.deferred = Some(PathEvent::Begin { at: self.prev }); + self.first = self.prev; + Some(PathEvent::End { + last, + first, + close: false, + }) + } else { + self.first = Point::new(*x as f32, *y as f32); + Some(PathEvent::Begin { at: self.first }) + } + } + Some(usvg::PathSegment::LineTo { x, y }) => { + self.needs_end = true; + let from = self.prev; + self.prev = Point::new(*x as f32, *y as f32); + Some(PathEvent::Line { + from, + to: self.prev, + }) + } + Some(usvg::PathSegment::CurveTo { + x1, + y1, + x2, + y2, + x, + y, + }) => { + self.needs_end = true; + let from = self.prev; + self.prev = Point::new(*x as f32, *y as f32); + Some(PathEvent::Cubic { + from, + ctrl1: Point::new(*x1 as f32, *y1 as f32), + ctrl2: Point::new(*x2 as f32, *y2 as f32), + to: self.prev, + }) + } + Some(usvg::PathSegment::ClosePath) => { + self.needs_end = false; + self.prev = self.first; + Some(PathEvent::End { + last: self.prev, + first: self.first, + close: true, + }) + } + None => { + if self.needs_end { + self.needs_end = false; + let last = self.prev; + let first = self.first; + Some(PathEvent::End { + last, + first, + close: false, + }) + } else { + None + } + } + } + } +} + +pub struct VertexCtor { + pub prim_id: u32, +} + +impl FillVertexConstructor for VertexCtor { + fn new_vertex(&mut self, vertex: tessellation::FillVertex) -> GpuVertex { + GpuVertex { + position: vertex.position().to_array(), + prim_id: self.prim_id, + } + } +} + +impl StrokeVertexConstructor for VertexCtor { + fn new_vertex(&mut self, vertex: tessellation::StrokeVertex) -> GpuVertex { + GpuVertex { + position: vertex.position().to_array(), + prim_id: self.prim_id, + } + } +} + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct GpuVertex { + pub position: [f32; 2], + pub prim_id: u32, +} -- cgit From 0ade350b5f4b6e7c0c4116e1f4f2b30ab86b7e52 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 20 Jan 2021 16:33:42 +0100 Subject: Use newtypes for collider, rigid-body and joint handles. --- examples2d/trimesh2.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'examples2d') diff --git a/examples2d/trimesh2.rs b/examples2d/trimesh2.rs index 3176861..d77d841 100644 --- a/examples2d/trimesh2.rs +++ b/examples2d/trimesh2.rs @@ -79,11 +79,7 @@ pub fn init_world(testbed: &mut Testbed) { transform.get_scale().1 as f32 * 0.2, ); - let indices: Vec<_> = mesh - .indices - .chunks(3) - .map(|v| Point3::new(v[0], v[1], v[2])) - .collect(); + let indices: Vec<_> = mesh.indices.chunks(3).map(|v| [v[0], v[1], v[2]]).collect(); let vertices: Vec<_> = mesh .vertices .iter() -- cgit From 90db26eb501b65cda362dcef34777106f533248b Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Sat, 23 Jan 2021 13:34:03 +0100 Subject: Fix warnings in tests and testbed. --- examples2d/trimesh2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples2d') diff --git a/examples2d/trimesh2.rs b/examples2d/trimesh2.rs index d77d841..baed184 100644 --- a/examples2d/trimesh2.rs +++ b/examples2d/trimesh2.rs @@ -1,4 +1,4 @@ -use na::{Point2, Point3}; +use na::Point2; use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; use rapier2d::geometry::{ColliderBuilder, ColliderSet}; use rapier_testbed2d::Testbed; -- cgit From 23a86c294e48da9c3aad82284a09791aabfeb88d Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 26 Jan 2021 16:41:21 +0100 Subject: Allow using polylines as a collider shape. --- examples2d/all_examples2.rs | 4 ++ examples2d/convex_polygons2.rs | 86 ++++++++++++++++++++++++++++++++++++++++++ examples2d/polyline2.rs | 74 ++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 examples2d/convex_polygons2.rs create mode 100644 examples2d/polyline2.rs (limited to 'examples2d') 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 = 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() +} -- cgit