aboutsummaryrefslogtreecommitdiff
path: root/examples2d
diff options
context:
space:
mode:
authorCrozet Sébastien <developer@crozet.re>2021-01-26 16:41:21 +0100
committerCrozet Sébastien <developer@crozet.re>2021-01-26 16:41:21 +0100
commit23a86c294e48da9c3aad82284a09791aabfeb88d (patch)
treec4adf2ccf4703ef103247f5035838f4071bf8c7c /examples2d
parente1f50eb6e8daa9529e41f7044e67736cc5c50953 (diff)
downloadrapier-23a86c294e48da9c3aad82284a09791aabfeb88d.tar.gz
rapier-23a86c294e48da9c3aad82284a09791aabfeb88d.tar.bz2
rapier-23a86c294e48da9c3aad82284a09791aabfeb88d.zip
Allow using polylines as a collider shape.
Diffstat (limited to 'examples2d')
-rw-r--r--examples2d/all_examples2.rs4
-rw-r--r--examples2d/convex_polygons2.rs86
-rw-r--r--examples2d/polyline2.rs74
3 files changed, 164 insertions, 0 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()
+}