aboutsummaryrefslogtreecommitdiff
path: root/examples2d
diff options
context:
space:
mode:
Diffstat (limited to 'examples2d')
-rw-r--r--examples2d/Cargo.toml2
-rw-r--r--examples2d/all_examples2.rs6
-rw-r--r--examples2d/collision_groups2.rs98
-rw-r--r--examples2d/damping2.rs45
-rw-r--r--examples2d/restitution2.rs56
5 files changed, 206 insertions, 1 deletions
diff --git a/examples2d/Cargo.toml b/examples2d/Cargo.toml
index ad63958..f1ed728 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.22"
+nalgebra = "0.23"
[dependencies.rapier_testbed2d]
path = "../build/rapier_testbed2d"
diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs
index 0ebef88..92606d8 100644
--- a/examples2d/all_examples2.rs
+++ b/examples2d/all_examples2.rs
@@ -11,11 +11,14 @@ use rapier_testbed2d::Testbed;
use std::cmp::Ordering;
mod add_remove2;
+mod collision_groups2;
+mod damping2;
mod debug_box_ball2;
mod heightfield2;
mod joints2;
mod platform2;
mod pyramid2;
+mod restitution2;
mod sensor2;
fn demo_name_from_command_line() -> Option<String> {
@@ -52,10 +55,13 @@ pub fn main() {
let mut builders: Vec<(_, fn(&mut Testbed))> = vec![
("Add remove", add_remove2::init_world),
+ ("Collision groups", collision_groups2::init_world),
+ ("Damping", damping2::init_world),
("Heightfield", heightfield2::init_world),
("Joints", joints2::init_world),
("Platform", platform2::init_world),
("Pyramid", pyramid2::init_world),
+ ("Restitution", restitution2::init_world),
("Sensor", sensor2::init_world),
("(Debug) box ball", debug_box_ball2::init_world),
];
diff --git a/examples2d/collision_groups2.rs b/examples2d/collision_groups2.rs
new file mode 100644
index 0000000..9fd9f0b
--- /dev/null
+++ b/examples2d/collision_groups2.rs
@@ -0,0 +1,98 @@
+use na::{Point2, Point3};
+use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
+use rapier2d::geometry::{ColliderBuilder, ColliderSet, InteractionGroups};
+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 = 5.0;
+ let ground_height = 0.1;
+
+ let rigid_body = RigidBodyBuilder::new_static()
+ .translation(0.0, -ground_height)
+ .build();
+ let floor_handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(ground_size, ground_height).build();
+ colliders.insert(collider, floor_handle, &mut bodies);
+
+ /*
+ * Setup groups
+ */
+ const GREEN_GROUP: InteractionGroups = InteractionGroups::new(0b01, 0b01);
+ const BLUE_GROUP: InteractionGroups = InteractionGroups::new(0b10, 0b10);
+
+ /*
+ * A green floor that will collide with the GREEN group only.
+ */
+ let green_floor = ColliderBuilder::cuboid(1.0, 0.1)
+ .translation(0.0, 1.0)
+ .collision_groups(GREEN_GROUP)
+ .build();
+ let green_collider_handle = colliders.insert(green_floor, floor_handle, &mut bodies);
+
+ testbed.set_collider_initial_color(green_collider_handle, Point3::new(0.0, 1.0, 0.0));
+
+ /*
+ * A blue floor that will collide with the BLUE group only.
+ */
+ let blue_floor = ColliderBuilder::cuboid(1.0, 0.1)
+ .translation(0.0, 2.0)
+ .collision_groups(BLUE_GROUP)
+ .build();
+ let blue_collider_handle = colliders.insert(blue_floor, floor_handle, &mut bodies);
+
+ testbed.set_collider_initial_color(blue_collider_handle, Point3::new(0.0, 0.0, 1.0));
+
+ /*
+ * Create the cubes
+ */
+ let num = 8;
+ let rad = 0.1;
+
+ let shift = rad * 2.0;
+ let centerx = shift * (num / 2) as f32;
+ let centery = 2.5;
+
+ for j in 0usize..4 {
+ for i in 0..num {
+ let x = i as f32 * shift - centerx;
+ let y = j as f32 * shift + centery;
+
+ // Alternate between the green and blue groups.
+ let (group, color) = if i % 2 == 0 {
+ (GREEN_GROUP, Point3::new(0.0, 1.0, 0.0))
+ } else {
+ (BLUE_GROUP, Point3::new(0.0, 0.0, 1.0))
+ };
+
+ let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(rad, rad)
+ .collision_groups(group)
+ .build();
+ colliders.insert(collider, handle, &mut bodies);
+
+ testbed.set_body_color(handle, color);
+ }
+ }
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world(bodies, colliders, joints);
+ testbed.look_at(Point2::new(0.0, 1.0), 100.0);
+}
+
+fn main() {
+ let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]);
+ testbed.run()
+}
diff --git a/examples2d/damping2.rs b/examples2d/damping2.rs
new file mode 100644
index 0000000..43ca2b6
--- /dev/null
+++ b/examples2d/damping2.rs
@@ -0,0 +1,45 @@
+use na::{Point2, Vector2};
+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();
+
+ /*
+ * Create the balls
+ */
+ let num = 10;
+ let rad = 0.2;
+
+ let subdiv = 1.0 / (num as f32);
+
+ for i in 0usize..num {
+ let (x, y) = (i as f32 * subdiv * std::f32::consts::PI * 2.0).sin_cos();
+
+ // Build the rigid body.
+ let rb = RigidBodyBuilder::new_dynamic()
+ .translation(x, y)
+ .linvel(x * 10.0, y * 10.0)
+ .angvel(100.0)
+ .linear_damping((i + 1) as f32 * subdiv * 10.0)
+ .angular_damping((num - i) as f32 * subdiv * 10.0)
+ .build();
+ let rb_handle = bodies.insert(rb);
+
+ // Build the collider.
+ let co = ColliderBuilder::cuboid(rad, rad).build();
+ colliders.insert(co, rb_handle, &mut bodies);
+ }
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world_with_gravity(bodies, colliders, joints, Vector2::zeros());
+ testbed.look_at(Point2::new(3.0, 2.0), 50.0);
+}
diff --git a/examples2d/restitution2.rs b/examples2d/restitution2.rs
new file mode 100644
index 0000000..fc97456
--- /dev/null
+++ b/examples2d/restitution2.rs
@@ -0,0 +1,56 @@
+use na::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 = 20.;
+ let ground_height = 1.0;
+
+ let rigid_body = RigidBodyBuilder::new_static()
+ .translation(0.0, -ground_height)
+ .build();
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(ground_size, ground_height)
+ .restitution(1.0)
+ .build();
+ colliders.insert(collider, handle, &mut bodies);
+
+ let num = 10;
+ let rad = 0.5;
+
+ for j in 0..2 {
+ for i in 0..=num {
+ let x = (i as f32) - num as f32 / 2.0;
+ let rigid_body = RigidBodyBuilder::new_dynamic()
+ .translation(x * 2.0, 10.0 * (j as f32 + 1.0))
+ .build();
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::ball(rad)
+ .restitution((i as f32) / (num as f32))
+ .build();
+ colliders.insert(collider, handle, &mut bodies);
+ }
+ }
+
+ /*
+ * Set up the testbed.
+ */
+ testbed.set_world(bodies, colliders, joints);
+ testbed.look_at(Point2::new(0.0, 1.0), 25.0);
+}
+
+fn main() {
+ let testbed = Testbed::from_builders(0, vec![("Boxes", init_world)]);
+ testbed.run()
+}