From 1bef66fea941307a7305ddaebdb0abe3d0cb281f Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 25 May 2021 11:00:13 +0200 Subject: Add prelude + use vectors for setting linvel/translation in builders --- examples2d/add_remove2.rs | 10 ++++---- examples2d/ccd2.rs | 52 +++++++++++++++++++++++----------------- examples2d/collision_groups2.rs | 33 ++++++++++++------------- examples2d/convex_polygons2.rs | 24 +++++++++---------- examples2d/damping2.rs | 14 +++++------ examples2d/debug_box_ball2.rs | 14 +++++------ examples2d/heightfield2.rs | 19 ++++++++------- examples2d/joints2.rs | 16 ++++++------- examples2d/locked_rotations2.rs | 18 +++++++------- examples2d/one_way_platforms2.rs | 39 +++++++++++++----------------- examples2d/platform2.rs | 20 ++++++++-------- examples2d/polyline2.rs | 23 +++++++++--------- examples2d/pyramid2.rs | 14 +++++------ examples2d/restitution2.rs | 14 +++++------ examples2d/sensor2.rs | 40 +++++++++++++++++-------------- examples2d/trimesh2.rs | 22 ++++++++--------- 16 files changed, 183 insertions(+), 189 deletions(-) (limited to 'examples2d') diff --git a/examples2d/add_remove2.rs b/examples2d/add_remove2.rs index 9f9f65a..056e63e 100644 --- a/examples2d/add_remove2.rs +++ b/examples2d/add_remove2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -12,13 +10,13 @@ pub fn init_world(testbed: &mut Testbed) { // Callback that will be executed on the main loop to handle proximities. testbed.add_callback(move |mut graphics, physics, _, _| { let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(0.0, 10.0) + .translation(vector![0.0, 10.0]) .build(); let handle = physics.bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(rad, rad).build(); physics .colliders - .insert(collider, handle, &mut physics.bodies); + .insert_with_parent(collider, handle, &mut physics.bodies); if let Some(graphics) = &mut graphics { graphics.add_body(handle, &physics.bodies, &physics.colliders); @@ -48,5 +46,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 0.0), 20.0); + testbed.look_at(point![0.0, 0.0], 20.0); } diff --git a/examples2d/ccd2.rs b/examples2d/ccd2.rs index 18a6bfe..1200f29 100644 --- a/examples2d/ccd2.rs +++ b/examples2d/ccd2.rs @@ -1,6 +1,4 @@ -use na::{Isometry2, Point2, Point3}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet, SharedShape}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -21,23 +19,23 @@ pub fn init_world(testbed: &mut Testbed) { let ground_handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(ground_size, ground_thickness).build(); - colliders.insert(collider, ground_handle, &mut bodies); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); let collider = ColliderBuilder::cuboid(ground_thickness, ground_size) - .translation(-3.0, 0.0) + .translation(vector![-3.0, 0.0]) .build(); - colliders.insert(collider, ground_handle, &mut bodies); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); let collider = ColliderBuilder::cuboid(ground_thickness, ground_size) - .translation(6.0, 0.0) + .translation(vector![6.0, 0.0]) .build(); - colliders.insert(collider, ground_handle, &mut bodies); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); let collider = ColliderBuilder::cuboid(ground_thickness, ground_size) - .translation(2.5, 0.0) + .translation(vector![2.5, 0.0]) .sensor(true) .build(); - let sensor_handle = colliders.insert(collider, ground_handle, &mut bodies); + let sensor_handle = colliders.insert_with_parent(collider, ground_handle, &mut bodies); /* * Create the shapes @@ -45,9 +43,9 @@ pub fn init_world(testbed: &mut Testbed) { let radx = 0.4; let rady = 0.05; - let delta1 = Isometry2::translation(0.0, radx - rady); - let delta2 = Isometry2::translation(-radx + rady, 0.0); - let delta3 = Isometry2::translation(radx - rady, 0.0); + let delta1 = Isometry::translation(0.0, radx - rady); + let delta2 = Isometry::translation(-radx + rady, 0.0); + let delta3 = Isometry::translation(radx - rady, 0.0); let mut compound_parts = Vec::new(); let vertical = SharedShape::cuboid(rady, radx); @@ -70,8 +68,8 @@ pub fn init_world(testbed: &mut Testbed) { // Build the rigid body. let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(x, y) - .linvel(100.0, -10.0) + .translation(vector![x, y]) + .linvel(vector![100.0, -10.0]) .ccd_enabled(true) .build(); let handle = bodies.insert(rigid_body); @@ -80,12 +78,12 @@ pub fn init_world(testbed: &mut Testbed) { // let collider = ColliderBuilder::new(part.1.clone()) // .position_wrt_parent(part.0) // .build(); - // colliders.insert(collider, handle, &mut bodies); + // colliders.insert_with_parent(collider, handle, &mut bodies); // } let collider = ColliderBuilder::new(compound_shape.clone()).build(); // let collider = ColliderBuilder::cuboid(radx, rady).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } @@ -93,13 +91,23 @@ pub fn init_world(testbed: &mut Testbed) { testbed.add_callback(move |mut graphics, physics, events, _| { while let Ok(prox) = events.intersection_events.try_recv() { let color = if prox.intersecting { - Point3::new(1.0, 1.0, 0.0) + [1.0, 1.0, 0.0] } else { - Point3::new(0.5, 0.5, 1.0) + [0.5, 0.5, 1.0] }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics + .colliders + .get(prox.collider1) + .unwrap() + .parent() + .unwrap(); + let parent_handle2 = physics + .colliders + .get(prox.collider2) + .unwrap() + .parent() + .unwrap(); if let Some(graphics) = &mut graphics { if parent_handle1 != ground_handle && prox.collider1 != sensor_handle { graphics.set_body_color(parent_handle1, color); @@ -115,5 +123,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 2.5), 20.0); + testbed.look_at(point![0.0, 2.5], 20.0); } diff --git a/examples2d/collision_groups2.rs b/examples2d/collision_groups2.rs index 08b73fb..3f2a786 100644 --- a/examples2d/collision_groups2.rs +++ b/examples2d/collision_groups2.rs @@ -1,6 +1,4 @@ -use na::{Point2, Point3}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet, InteractionGroups}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -18,11 +16,11 @@ pub fn init_world(testbed: &mut Testbed) { let ground_height = 0.1; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -ground_height) + .translation(vector![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); + colliders.insert_with_parent(collider, floor_handle, &mut bodies); /* * Setup groups @@ -34,23 +32,24 @@ pub fn init_world(testbed: &mut Testbed) { * 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) + .translation(vector![0.0, 1.0]) .collision_groups(GREEN_GROUP) .build(); - let green_collider_handle = colliders.insert(green_floor, floor_handle, &mut bodies); + let green_collider_handle = + colliders.insert_with_parent(green_floor, floor_handle, &mut bodies); - testbed.set_initial_collider_color(green_collider_handle, Point3::new(0.0, 1.0, 0.0)); + testbed.set_initial_collider_color(green_collider_handle, [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) + .translation(vector![0.0, 2.0]) .collision_groups(BLUE_GROUP) .build(); - let blue_collider_handle = colliders.insert(blue_floor, floor_handle, &mut bodies); + let blue_collider_handle = colliders.insert_with_parent(blue_floor, floor_handle, &mut bodies); - testbed.set_initial_collider_color(blue_collider_handle, Point3::new(0.0, 0.0, 1.0)); + testbed.set_initial_collider_color(blue_collider_handle, [0.0, 0.0, 1.0]); /* * Create the cubes @@ -69,17 +68,19 @@ pub fn init_world(testbed: &mut Testbed) { // Alternate between the green and blue groups. let (group, color) = if i % 2 == 0 { - (GREEN_GROUP, Point3::new(0.0, 1.0, 0.0)) + (GREEN_GROUP, [0.0, 1.0, 0.0]) } else { - (BLUE_GROUP, Point3::new(0.0, 0.0, 1.0)) + (BLUE_GROUP, [0.0, 0.0, 1.0]) }; - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); testbed.set_initial_body_color(handle, color); } @@ -89,5 +90,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 1.0), 100.0); + testbed.look_at(point![0.0, 1.0], 100.0); } diff --git a/examples2d/convex_polygons2.rs b/examples2d/convex_polygons2.rs index 0e4fb0c..4373fcb 100644 --- a/examples2d/convex_polygons2.rs +++ b/examples2d/convex_polygons2.rs @@ -1,8 +1,6 @@ -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 rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -21,23 +19,23 @@ pub fn init_world(testbed: &mut Testbed) { 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); + colliders.insert_with_parent(collider, handle, &mut bodies); let rigid_body = RigidBodyBuilder::new_static() .rotation(std::f32::consts::FRAC_PI_2) - .translation(ground_size, ground_size * 2.0) + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); let rigid_body = RigidBodyBuilder::new_static() .rotation(std::f32::consts::FRAC_PI_2) - .translation(-ground_size, ground_size * 2.0) + .translation(vector![-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); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Create the convex polygons @@ -58,18 +56,20 @@ pub fn init_world(testbed: &mut Testbed) { 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 rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![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); + let pt: Point = distribution.sample(&mut rng); points.push(pt * scale); } let collider = ColliderBuilder::convex_hull(&points).unwrap().build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } @@ -77,5 +77,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 50.0), 10.0); + testbed.look_at(point![0.0, 50.0], 10.0); } diff --git a/examples2d/damping2.rs b/examples2d/damping2.rs index 68fd77d..e308de6 100644 --- a/examples2d/damping2.rs +++ b/examples2d/damping2.rs @@ -1,6 +1,4 @@ -use na::{Point2, Vector2}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -24,8 +22,8 @@ pub fn init_world(testbed: &mut Testbed) { // Build the rigid body. let rb = RigidBodyBuilder::new_dynamic() - .translation(x, y) - .linvel(x * 10.0, y * 10.0) + .translation(vector![x, y]) + .linvel(vector![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) @@ -34,12 +32,12 @@ pub fn init_world(testbed: &mut Testbed) { // Build the collider. let co = ColliderBuilder::cuboid(rad, rad).build(); - colliders.insert(co, rb_handle, &mut bodies); + colliders.insert_with_parent(co, rb_handle, &mut bodies); } /* * Set up the testbed. */ - testbed.set_world_with_params(bodies, colliders, joints, Vector2::zeros(), ()); - testbed.look_at(Point2::new(3.0, 2.0), 50.0); + testbed.set_world_with_params(bodies, colliders, joints, Vector::zeros(), ()); + testbed.look_at(point![3.0, 2.0], 50.0); } diff --git a/examples2d/debug_box_ball2.rs b/examples2d/debug_box_ball2.rs index aad72ae..63b6f95 100644 --- a/examples2d/debug_box_ball2.rs +++ b/examples2d/debug_box_ball2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -16,25 +14,25 @@ pub fn init_world(testbed: &mut Testbed) { */ let rad = 1.0; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -rad) + .translation(vector![0.0, -rad]) .rotation(std::f32::consts::PI / 4.0) .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); // Build the dynamic box rigid body. let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(0.0, 3.0 * rad) + .translation(vector![0.0, 3.0 * rad]) .can_sleep(false) .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 0.0), 50.0); + testbed.look_at(point![0.0, 0.0], 50.0); } diff --git a/examples2d/heightfield2.rs b/examples2d/heightfield2.rs index a834a2c..6246c40 100644 --- a/examples2d/heightfield2.rs +++ b/examples2d/heightfield2.rs @@ -1,6 +1,5 @@ -use na::{DVector, Point2, Vector2}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use na::DVector; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -14,7 +13,7 @@ pub fn init_world(testbed: &mut Testbed) { /* * Ground */ - let ground_size = Vector2::new(50.0, 1.0); + let ground_size = Vector::new(50.0, 1.0); let nsubdivs = 2000; let heights = DVector::from_fn(nsubdivs + 1, |i, _| { @@ -28,7 +27,7 @@ pub fn init_world(testbed: &mut Testbed) { let rigid_body = RigidBodyBuilder::new_static().build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::heightfield(heights, ground_size).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Create the cubes @@ -46,15 +45,17 @@ pub fn init_world(testbed: &mut Testbed) { let y = j as f32 * shift + centery + 3.0; // Build the rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); } else { let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } } @@ -63,5 +64,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 0.0), 10.0); + testbed.look_at(point![0.0, 0.0], 10.0); } diff --git a/examples2d/joints2.rs b/examples2d/joints2.rs index ff5c663..9c94968 100644 --- a/examples2d/joints2.rs +++ b/examples2d/joints2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{BallJoint, JointSet, RigidBodyBuilder, RigidBodySet, RigidBodyType}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -15,7 +13,7 @@ pub fn init_world(testbed: &mut Testbed) { * Create the balls */ // Build the rigid body. - // NOTE: a smaller radius (e.g. 0.1) breaks Box2D so + // NOTE: a smaller radius (e.g. 0.1) breaks Box2D so // in order to be able to compare rapier with Box2D, // we set it to 0.4. let rad = 0.4; @@ -37,16 +35,16 @@ pub fn init_world(testbed: &mut Testbed) { }; let rigid_body = RigidBodyBuilder::new(status) - .translation(fk * shift, -fi * shift) + .translation(vector![fk * shift, -fi * shift]) .build(); let child_handle = bodies.insert(rigid_body); let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, child_handle, &mut bodies); + colliders.insert_with_parent(collider, child_handle, &mut bodies); // Vertical joint. if i > 0 { let parent_handle = *body_handles.last().unwrap(); - let joint = BallJoint::new(Point2::origin(), Point2::new(0.0, shift)); + let joint = BallJoint::new(Point::origin(), point![0.0, shift]); joints.insert(&mut bodies, parent_handle, child_handle, joint); } @@ -54,7 +52,7 @@ pub fn init_world(testbed: &mut Testbed) { if k > 0 { let parent_index = body_handles.len() - numi; let parent_handle = body_handles[parent_index]; - let joint = BallJoint::new(Point2::origin(), Point2::new(-shift, 0.0)); + let joint = BallJoint::new(Point::origin(), point![-shift, 0.0]); joints.insert(&mut bodies, parent_handle, child_handle, joint); } @@ -66,5 +64,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(numk as f32 * rad, numi as f32 * -rad), 20.0); + testbed.look_at(point![numk as f32 * rad, numi as f32 * -rad], 20.0); } diff --git a/examples2d/locked_rotations2.rs b/examples2d/locked_rotations2.rs index 03fb2e7..a1f7ba5 100644 --- a/examples2d/locked_rotations2.rs +++ b/examples2d/locked_rotations2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; // This shows a bug when a cylinder is in contact with a very large @@ -21,38 +19,38 @@ pub fn init_world(testbed: &mut Testbed) { let ground_height = 0.1; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -ground_height) + .translation(vector![0.0, -ground_height]) .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(ground_size, ground_height).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * A rectangle that only rotate. */ let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(0.0, 3.0) + .translation(vector![0.0, 3.0]) .lock_translations() .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(2.0, 0.6).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * A tilted capsule that cannot rotate. */ let rigid_body = RigidBodyBuilder::new_dynamic() - .translation(0.0, 5.0) + .translation(vector![0.0, 5.0]) .rotation(1.0) .lock_rotations() .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::capsule_y(0.6, 0.4).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 0.0), 40.0); + testbed.look_at(point![0.0, 0.0], 40.0); } diff --git a/examples2d/one_way_platforms2.rs b/examples2d/one_way_platforms2.rs index 19b9968..b0f2212 100644 --- a/examples2d/one_way_platforms2.rs +++ b/examples2d/one_way_platforms2.rs @@ -1,7 +1,4 @@ -use na::{Point2, Vector2}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderHandle, ColliderSet}; -use rapier2d::pipeline::{ContactModificationContext, PhysicsHooks, PhysicsHooksFlags}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; struct OneWayPlatformHook { @@ -10,10 +7,6 @@ struct OneWayPlatformHook { } impl PhysicsHooks for OneWayPlatformHook { - fn active_hooks(&self) -> PhysicsHooksFlags { - PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS - } - fn modify_solver_contacts( &self, context: &mut ContactModificationContext, @@ -30,20 +23,20 @@ impl PhysicsHooks for OneWayPlatformHook { // - If context.collider_handle2 == self.platform1 then the allowed normal is -y. // - If context.collider_handle1 == self.platform2 then its allowed normal +y needs to be flipped to -y. // - If context.collider_handle2 == self.platform2 then the allowed normal -y needs to be flipped to +y. - let mut allowed_local_n1 = Vector2::zeros(); + let mut allowed_local_n1 = Vector::zeros(); if context.collider1 == self.platform1 { - allowed_local_n1 = Vector2::y(); + allowed_local_n1 = Vector::y(); } else if context.collider2 == self.platform1 { // Flip the allowed direction. - allowed_local_n1 = -Vector2::y(); + allowed_local_n1 = -Vector::y(); } if context.collider1 == self.platform2 { - allowed_local_n1 = -Vector2::y(); + allowed_local_n1 = -Vector::y(); } else if context.collider2 == self.platform2 { // Flip the allowed direction. - allowed_local_n1 = Vector2::y(); + allowed_local_n1 = Vector::y(); } // Call the helper function that simulates one-way platforms. @@ -78,15 +71,15 @@ pub fn init_world(testbed: &mut Testbed) { let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(25.0, 0.5) - .translation(30.0, 2.0) - .modify_solver_contacts(true) + .translation(vector![30.0, 2.0]) + .active_hooks(PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS) .build(); - let platform1 = colliders.insert(collider, handle, &mut bodies); + let platform1 = colliders.insert_with_parent(collider, handle, &mut bodies); let collider = ColliderBuilder::cuboid(25.0, 0.5) - .translation(-30.0, -2.0) - .modify_solver_contacts(true) + .translation(vector![-30.0, -2.0]) + .active_hooks(PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS) .build(); - let platform2 = colliders.insert(collider, handle, &mut bodies); + let platform2 = colliders.insert_with_parent(collider, handle, &mut bodies); /* * Setup the one-way platform hook. @@ -105,12 +98,12 @@ pub fn init_world(testbed: &mut Testbed) { // Spawn a new cube. let collider = ColliderBuilder::cuboid(1.5, 2.0).build(); let body = RigidBodyBuilder::new_dynamic() - .translation(20.0, 10.0) + .translation(vector![20.0, 10.0]) .build(); let handle = physics.bodies.insert(body); physics .colliders - .insert(collider, handle, &mut physics.bodies); + .insert_with_parent(collider, handle, &mut physics.bodies); if let Some(graphics) = graphics { graphics.add_body(handle, &physics.bodies, &physics.colliders); @@ -134,8 +127,8 @@ pub fn init_world(testbed: &mut Testbed) { bodies, colliders, joints, - Vector2::new(0.0, -9.81), + vector![0.0, -9.81], physics_hooks, ); - testbed.look_at(Point2::new(0.0, 0.0), 20.0); + testbed.look_at(point![0.0, 0.0], 20.0); } diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 7542cd6..6aa73b7 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -18,11 +16,11 @@ pub fn init_world(testbed: &mut Testbed) { let ground_height = 0.1; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -ground_height) + .translation(vector![0.0, -ground_height]) .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(ground_size, ground_height).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Create the boxes @@ -40,10 +38,12 @@ pub fn init_world(testbed: &mut Testbed) { let y = j as f32 * shift + centery; // Build the rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![x, y]) + .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } @@ -51,11 +51,11 @@ pub fn init_world(testbed: &mut Testbed) { * Setup a kinematic rigid body. */ let platform_body = RigidBodyBuilder::new_kinematic() - .translation(-10.0 * rad, 1.5 + 0.8) + .translation(vector![-10.0 * rad, 1.5 + 0.8]) .build(); let platform_handle = bodies.insert(platform_body); let collider = ColliderBuilder::cuboid(rad * 10.0, rad).build(); - colliders.insert(collider, platform_handle, &mut bodies); + colliders.insert_with_parent(collider, platform_handle, &mut bodies); /* * Setup a callback to control the platform. @@ -82,5 +82,5 @@ pub fn init_world(testbed: &mut Testbed) { * Run the simulation. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 1.0), 40.0); + testbed.look_at(point![0.0, 1.0], 40.0); } diff --git a/examples2d/polyline2.rs b/examples2d/polyline2.rs index 0b3be0c..7405e24 100644 --- a/examples2d/polyline2.rs +++ b/examples2d/polyline2.rs @@ -1,6 +1,5 @@ -use na::{ComplexField, Point2}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use na::ComplexField; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -19,18 +18,18 @@ pub fn init_world(testbed: &mut Testbed) { let step_size = ground_size / (nsubdivs as f32); let mut points = Vec::new(); - points.push(Point2::new(-ground_size / 2.0, 40.0)); + points.push(point![-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(point![x, y]); } - points.push(Point2::new(ground_size / 2.0, 40.0)); + points.push(point![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); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Create the cubes @@ -48,15 +47,17 @@ pub fn init_world(testbed: &mut Testbed) { let y = j as f32 * shift + centery + 3.0; // Build the rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); } else { let collider = ColliderBuilder::ball(rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } } @@ -65,5 +66,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 0.0), 10.0); + testbed.look_at(point![0.0, 0.0], 10.0); } diff --git a/examples2d/pyramid2.rs b/examples2d/pyramid2.rs index dfa64c9..76616be 100644 --- a/examples2d/pyramid2.rs +++ b/examples2d/pyramid2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -20,7 +18,7 @@ pub fn init_world(testbed: &mut Testbed) { let rigid_body = RigidBodyBuilder::new_static().build(); let ground_handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(ground_size, ground_thickness).build(); - colliders.insert(collider, ground_handle, &mut bodies); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); /* * Create the cubes @@ -40,10 +38,12 @@ pub fn init_world(testbed: &mut Testbed) { let y = fi * shift + centery; // Build the rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![x, y]) + .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); } } @@ -51,5 +51,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 2.5), 20.0); + testbed.look_at(point![0.0, 2.5], 20.0); } diff --git a/examples2d/restitution2.rs b/examples2d/restitution2.rs index 025a9cd..1be4298 100644 --- a/examples2d/restitution2.rs +++ b/examples2d/restitution2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -18,13 +16,13 @@ pub fn init_world(testbed: &mut Testbed) { let ground_height = 1.0; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -ground_height) + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); let num = 10; let rad = 0.5; @@ -33,13 +31,13 @@ pub fn init_world(testbed: &mut Testbed) { 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)) + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); } } @@ -47,5 +45,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 1.0), 25.0); + testbed.look_at(point![0.0, 1.0], 25.0); } diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index a651f64..17ed970 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -1,6 +1,4 @@ -use na::{Point2, Point3}; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; pub fn init_world(testbed: &mut Testbed) { @@ -18,11 +16,11 @@ pub fn init_world(testbed: &mut Testbed) { let ground_height = 0.1; let rigid_body = RigidBodyBuilder::new_static() - .translation(0.0, -ground_height) + .translation(vector![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); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); /* * Create some boxes. @@ -38,12 +36,14 @@ pub fn init_world(testbed: &mut Testbed) { let y = 3.0; // Build the rigid body. - let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build(); + let rigid_body = RigidBodyBuilder::new_dynamic() + .translation(vector![x, y]) + .build(); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(rad, rad).build(); - colliders.insert(collider, handle, &mut bodies); + colliders.insert_with_parent(collider, handle, &mut bodies); - testbed.set_initial_body_color(handle, Point3::new(0.5, 0.5, 1.0)); + testbed.set_initial_body_color(handle, [0.5, 0.5, 1.0]); } /* @@ -52,33 +52,37 @@ pub fn init_world(testbed: &mut Testbed) { // Rigid body so that the sensor can move. let sensor = RigidBodyBuilder::new_dynamic() - .translation(0.0, 10.0) + .translation(vector![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).build(); - colliders.insert(collider, sensor_handle, &mut bodies); + colliders.insert_with_parent(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); + let sensor_collider = ColliderBuilder::ball(rad * 5.0) + .density(0.0) + .sensor(true) + .build(); + colliders.insert_with_parent(sensor_collider, sensor_handle, &mut bodies); - testbed.set_initial_body_color(sensor_handle, Point3::new(0.5, 1.0, 1.0)); + testbed.set_initial_body_color(sensor_handle, [0.5, 1.0, 1.0]); // Callback that will be executed on the main loop to handle proximities. testbed.add_callback(move |mut graphics, physics, events, _| { while let Ok(prox) = events.intersection_events.try_recv() { let color = if prox.intersecting { - Point3::new(1.0, 1.0, 0.0) + [1.0, 1.0, 0.0] } else { - Point3::new(0.5, 0.5, 1.0) + [0.5, 0.5, 1.0] }; - let parent_handle1 = physics.colliders.get(prox.collider1).unwrap().parent(); - let parent_handle2 = physics.colliders.get(prox.collider2).unwrap().parent(); + let parent_handle1 = physics.colliders[prox.collider1].parent().unwrap(); + let parent_handle2 = physics.colliders[prox.collider2].parent().unwrap(); + if let Some(graphics) = &mut graphics { if parent_handle1 != ground_handle && parent_handle1 != sensor_handle { graphics.set_body_color(parent_handle1, color); @@ -94,5 +98,5 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 1.0), 100.0); + testbed.look_at(point![0.0, 1.0], 100.0); } diff --git a/examples2d/trimesh2.rs b/examples2d/trimesh2.rs index adc8b6b..a4b049a 100644 --- a/examples2d/trimesh2.rs +++ b/examples2d/trimesh2.rs @@ -1,6 +1,4 @@ -use na::Point2; -use rapier2d::dynamics::{JointSet, RigidBodyBuilder, RigidBodySet}; -use rapier2d::geometry::{ColliderBuilder, ColliderSet}; +use rapier2d::prelude::*; use rapier_testbed2d::Testbed; use lyon::math::Point; @@ -25,23 +23,23 @@ pub fn init_world(testbed: &mut Testbed) { 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); + colliders.insert_with_parent(collider, handle, &mut bodies); let rigid_body = RigidBodyBuilder::new_static() .rotation(std::f32::consts::FRAC_PI_2) - .translation(ground_size, ground_size) + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); let rigid_body = RigidBodyBuilder::new_static() .rotation(std::f32::consts::FRAC_PI_2) - .translation(-ground_size, ground_size) + .translation(vector![-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); + colliders.insert_with_parent(collider, handle, &mut bodies); /* * Create the trimeshes from a tesselated SVG. @@ -83,18 +81,18 @@ pub fn init_world(testbed: &mut Testbed) { let vertices: Vec<_> = mesh .vertices .iter() - .map(|v| Point2::new(v.position[0] * sx, v.position[1] * -sy)) + .map(|v| point![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) + .translation(vector![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); + colliders.insert_with_parent(collider, handle, &mut bodies); } ith += 1; @@ -106,7 +104,7 @@ pub fn init_world(testbed: &mut Testbed) { * Set up the testbed. */ testbed.set_world(bodies, colliders, joints); - testbed.look_at(Point2::new(0.0, 20.0), 17.0); + testbed.look_at(point![0.0, 20.0], 17.0); } const RAPIER_SVG_STR: &'static str = r#" -- cgit From 826ce5f014281fd04b7a18238f102f2591d0b255 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 1 Jun 2021 12:36:01 +0200 Subject: Rework the event system --- examples2d/ccd2.rs | 1 + examples2d/one_way_platforms2.rs | 4 ++-- examples2d/sensor2.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'examples2d') diff --git a/examples2d/ccd2.rs b/examples2d/ccd2.rs index 1200f29..1f25586 100644 --- a/examples2d/ccd2.rs +++ b/examples2d/ccd2.rs @@ -34,6 +34,7 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::cuboid(ground_thickness, ground_size) .translation(vector![2.5, 0.0]) .sensor(true) + .active_events(ActiveEvents::INTERSECTION_EVENTS) .build(); let sensor_handle = colliders.insert_with_parent(collider, ground_handle, &mut bodies); diff --git a/examples2d/one_way_platforms2.rs b/examples2d/one_way_platforms2.rs index b0f2212..6cbb2f0 100644 --- a/examples2d/one_way_platforms2.rs +++ b/examples2d/one_way_platforms2.rs @@ -72,12 +72,12 @@ pub fn init_world(testbed: &mut Testbed) { let collider = ColliderBuilder::cuboid(25.0, 0.5) .translation(vector![30.0, 2.0]) - .active_hooks(PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS) + .active_hooks(ActiveHooks::MODIFY_SOLVER_CONTACTS) .build(); let platform1 = colliders.insert_with_parent(collider, handle, &mut bodies); let collider = ColliderBuilder::cuboid(25.0, 0.5) .translation(vector![-30.0, -2.0]) - .active_hooks(PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS) + .active_hooks(ActiveHooks::MODIFY_SOLVER_CONTACTS) .build(); let platform2 = colliders.insert_with_parent(collider, handle, &mut bodies); diff --git a/examples2d/sensor2.rs b/examples2d/sensor2.rs index 17ed970..5dc5940 100644 --- a/examples2d/sensor2.rs +++ b/examples2d/sensor2.rs @@ -66,6 +66,7 @@ pub fn init_world(testbed: &mut Testbed) { let sensor_collider = ColliderBuilder::ball(rad * 5.0) .density(0.0) .sensor(true) + .active_events(ActiveEvents::INTERSECTION_EVENTS) .build(); colliders.insert_with_parent(sensor_collider, sensor_handle, &mut bodies); -- cgit From 1839f61d816af37c95889caf592b5a7cf8d89412 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 1 Jun 2021 15:02:48 +0200 Subject: Add a velocity-based platform the the platform demos. --- examples2d/platform2.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'examples2d') diff --git a/examples2d/platform2.rs b/examples2d/platform2.rs index 6aa73b7..48129a0 100644 --- a/examples2d/platform2.rs +++ b/examples2d/platform2.rs @@ -48,34 +48,42 @@ pub fn init_world(testbed: &mut Testbed) { } /* - * Setup a kinematic rigid body. + * Setup a position-based kinematic rigid body. */ - let platform_body = RigidBodyBuilder::new_kinematic() + let platform_body = RigidBodyBuilder::new_kinematic_velocity_based() .translation(vector![-10.0 * rad, 1.5 + 0.8]) .build(); - let platform_handle = bodies.insert(platform_body); + let velocity_based_platform_handle = bodies.insert(platform_body); let collider = ColliderBuilder::cuboid(rad * 10.0, rad).build(); - colliders.insert_with_parent(collider, platform_handle, &mut bodies); + colliders.insert_with_parent(collider, velocity_based_platform_handle, &mut bodies); + + /* + * Setup a velocity-based kinematic rigid body. + */ + let platform_body = RigidBodyBuilder::new_kinematic_position_based() + .translation(vector![-10.0 * rad, 2.0 + 1.5 + 0.8]) + .build(); + let position_based_platform_handle = bodies.insert(platform_body); + let collider = ColliderBuilder::cuboid(rad * 10.0, rad).build(); + colliders.insert_with_parent(collider, position_based_platform_handle, &mut bodies); /* * Setup a callback to control the platform. */ testbed.add_callback(move |_, physics, _, run_state| { - let platform = physics.bodies.get_mut(platform_handle).unwrap(); - let mut next_pos = *platform.position(); - - let dt = 0.016; - next_pos.translation.vector.y += (run_state.time * 5.0).sin() * dt; - next_pos.translation.vector.x += run_state.time.sin() * 5.0 * dt; + let velocity = vector![run_state.time.sin() * 5.0, (run_state.time * 5.0).sin()]; - if next_pos.translation.vector.x >= rad * 10.0 { - next_pos.translation.vector.x -= dt; - } - if next_pos.translation.vector.x <= -rad * 10.0 { - next_pos.translation.vector.x += dt; + // Update the velocity-based kinematic body by setting its velocity. + if let Some(platform) = physics.bodies.get_mut(velocity_based_platform_handle) { + platform.set_linvel(velocity, true); } - platform.set_next_kinematic_position(next_pos); + // Update the position-based kinematic body by setting its next position. + if let Some(platform) = physics.bodies.get_mut(position_based_platform_handle) { + let mut next_tra = *platform.translation(); + next_tra += velocity * physics.integration_parameters.dt; + platform.set_next_kinematic_translation(next_tra); + } }); /* -- cgit From e0e341214c35347a30c3e76265d216396abc2cfb Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 2 Jun 2021 16:00:23 +0200 Subject: Update dependencies --- 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 b367ff8..6ade796 100644 --- a/examples2d/Cargo.toml +++ b/examples2d/Cargo.toml @@ -15,7 +15,7 @@ enhanced-determinism = [ "rapier2d/enhanced-determinism" ] [dependencies] rand = "0.8" Inflector = "0.11" -nalgebra = "0.26" +nalgebra = "0.27" lyon = "0.17" usvg = "0.13" -- cgit