aboutsummaryrefslogtreecommitdiff
path: root/examples2d/sensor2.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2020-08-25 22:10:25 +0200
committerSébastien Crozet <developer@crozet.re>2020-08-25 22:10:25 +0200
commit754a48b7ff6d8c58b1ee08651e60112900b60455 (patch)
tree7d777a6c003f1f5d8f8d24f533f35a95a88957fe /examples2d/sensor2.rs
downloadrapier-0.1.0.tar.gz
rapier-0.1.0.tar.bz2
rapier-0.1.0.zip
First public release of Rapier.v0.1.0
Diffstat (limited to 'examples2d/sensor2.rs')
-rw-r--r--examples2d/sensor2.rs101
1 files changed, 101 insertions, 0 deletions
diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs
new file mode 100644
index 0000000..75634a8
--- /dev/null
+++ b/examples2d/sensor2.rs
@@ -0,0 +1,101 @@
+use na::{Point2, Point3};
+use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet};
+use rapier2d::geometry::{ColliderBuilder, ColliderSet, Proximity};
+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 = 200.1;
+ let ground_height = 0.1;
+
+ let rigid_body = RigidBodyBuilder::new_static()
+ .translation(0.0, -ground_height)
+ .build();
+ let ground_handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(ground_size, ground_height).build();
+ colliders.insert(collider, ground_handle, &mut bodies);
+
+ /*
+ * Create some boxes.
+ */
+ let num = 10;
+ let rad = 0.2;
+
+ let shift = rad * 2.0;
+ let centerx = shift * num as f32 / 2.0;
+
+ for i in 0usize..num {
+ let x = i as f32 * shift - centerx;
+ let y = 3.0;
+
+ // Build the rigid body.
+ let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
+ let handle = bodies.insert(rigid_body);
+ let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
+ colliders.insert(collider, handle, &mut bodies);
+
+ testbed.set_body_color(handle, Point3::new(0.5, 0.5, 1.0));
+ }
+
+ /*
+ * Create a cube that will have a ball-shaped sensor attached.
+ */
+
+ // Rigid body so that the sensor can move.
+ let sensor = RigidBodyBuilder::new_dynamic()
+ .translation(0.0, 10.0)
+ .build();
+ let sensor_handle = bodies.insert(sensor);
+
+ // Solid cube attached to the sensor which
+ // other colliders can touch.
+ let collider = ColliderBuilder::cuboid(rad, rad).density(1.0).build();
+ colliders.insert(collider, sensor_handle, &mut bodies);
+
+ // We create a collider desc without density because we don't
+ // want it to contribute to the rigid body mass.
+ let sensor_collider = ColliderBuilder::ball(rad * 5.0).sensor(true).build();
+ colliders.insert(sensor_collider, sensor_handle, &mut bodies);
+
+ testbed.set_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0));
+
+ // Callback that will be executed on the main loop to handle proximities.
+ testbed.add_callback(move |_, colliders, 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),
+ };
+
+ let parent_handle1 = colliders.get(prox.collider1).unwrap().parent();
+ let parent_handle2 = colliders.get(prox.collider2).unwrap().parent();
+
+ if parent_handle1 != ground_handle && parent_handle1 != sensor_handle {
+ graphics.set_body_color(parent_handle1, color);
+ }
+ if parent_handle2 != ground_handle && parent_handle2 != sensor_handle {
+ graphics.set_body_color(parent_handle2, 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![("Sensor", init_world)]);
+ testbed.run()
+}