From 6432909110c4b96e59d4ee2ebdae1d79abe8d4b3 Mon Sep 17 00:00:00 2001 From: pellico Date: Sun, 27 Nov 2022 18:47:35 +0100 Subject: Fix #378 Added one example join_motor_position --- examples2d/all_examples2.rs | 2 ++ examples2d/joint_motor_position2.rs | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 examples2d/joint_motor_position2.rs (limited to 'examples2d') diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs index d102b46..fff20c5 100644 --- a/examples2d/all_examples2.rs +++ b/examples2d/all_examples2.rs @@ -16,6 +16,7 @@ mod convex_polygons2; mod damping2; mod debug_box_ball2; mod drum2; +mod joint_motor_position2; mod heightfield2; mod joints2; mod locked_rotations2; @@ -79,6 +80,7 @@ pub fn main() { ("Rope Joints", rope_joints2::init_world), ("Sensor", sensor2::init_world), ("Trimesh", trimesh2::init_world), + ("Joint motor position", joint_motor_position2::init_world), ("(Debug) box ball", debug_box_ball2::init_world), ]; diff --git a/examples2d/joint_motor_position2.rs b/examples2d/joint_motor_position2.rs new file mode 100644 index 0000000..5a79874 --- /dev/null +++ b/examples2d/joint_motor_position2.rs @@ -0,0 +1,46 @@ +use rapier2d::prelude::*; +use rapier_testbed2d::Testbed; + +pub fn init_world(testbed: &mut Testbed) { + /* + * World + */ + let mut bodies = RigidBodySet::new(); + let mut colliders = ColliderSet::new(); + let mut impulse_joints = ImpulseJointSet::new(); + let multibody_joints = MultibodyJointSet::new(); + + /* + * The ground + */ + let ground_size = 5.0; + let ground_height = 0.1; + + let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height]); + let ground_handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(ground_size, ground_height); + colliders.insert_with_parent(collider, ground_handle, &mut bodies); + + /* + * A rectangle on a motor + */ + for num in 0..9 { + let x_pos = -6.0 + 1.5 * num as f32; + let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x_pos, 2.0]); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(0.1, 0.5); + colliders.insert_with_parent(collider, handle, &mut bodies); + + let joint = RevoluteJointBuilder::new() + .local_anchor2(point![x_pos, 1.5]) + .local_anchor1(point![0.0, -0.5]) + .motor_position(std::f32::consts::PI/4.0*num as f32, 100.0, 15.0); + impulse_joints.insert(handle,ground_handle, joint, true); + } + + /* + * Set up the testbed. + */ + testbed.set_world_with_params(bodies, colliders, impulse_joints, multibody_joints,vector![0.0,0.0],()); + testbed.look_at(point![0.0, 0.0], 40.0); +} -- cgit From 9f3b5c86421e957e94f14e23b7e9912ada88d7e2 Mon Sep 17 00:00:00 2001 From: pellico Date: Mon, 28 Nov 2022 16:47:15 +0100 Subject: Fixed formatting of modified files. --- examples2d/all_examples2.rs | 2 +- examples2d/joint_motor_position2.rs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'examples2d') diff --git a/examples2d/all_examples2.rs b/examples2d/all_examples2.rs index fff20c5..032e4d6 100644 --- a/examples2d/all_examples2.rs +++ b/examples2d/all_examples2.rs @@ -16,8 +16,8 @@ mod convex_polygons2; mod damping2; mod debug_box_ball2; mod drum2; -mod joint_motor_position2; mod heightfield2; +mod joint_motor_position2; mod joints2; mod locked_rotations2; mod one_way_platforms2; diff --git a/examples2d/joint_motor_position2.rs b/examples2d/joint_motor_position2.rs index 5a79874..a701bd0 100644 --- a/examples2d/joint_motor_position2.rs +++ b/examples2d/joint_motor_position2.rs @@ -25,7 +25,7 @@ pub fn init_world(testbed: &mut Testbed) { * A rectangle on a motor */ for num in 0..9 { - let x_pos = -6.0 + 1.5 * num as f32; + let x_pos = -6.0 + 1.5 * num as f32; let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x_pos, 2.0]); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(0.1, 0.5); @@ -34,13 +34,20 @@ pub fn init_world(testbed: &mut Testbed) { let joint = RevoluteJointBuilder::new() .local_anchor2(point![x_pos, 1.5]) .local_anchor1(point![0.0, -0.5]) - .motor_position(std::f32::consts::PI/4.0*num as f32, 100.0, 15.0); - impulse_joints.insert(handle,ground_handle, joint, true); + .motor_position(std::f32::consts::PI / 4.0 * num as f32, 100.0, 15.0); + impulse_joints.insert(handle, ground_handle, joint, true); } /* * Set up the testbed. */ - testbed.set_world_with_params(bodies, colliders, impulse_joints, multibody_joints,vector![0.0,0.0],()); + testbed.set_world_with_params( + bodies, + colliders, + impulse_joints, + multibody_joints, + vector![0.0, 0.0], + (), + ); testbed.look_at(point![0.0, 0.0], 40.0); } -- cgit From a05622cfe995cb681f01bc22956addf99c208cc6 Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Mon, 10 Jul 2023 09:14:16 +0200 Subject: fix: avoid perpetual movement when the target ang motor position is overshot --- examples2d/joint_motor_position2.rs | 44 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'examples2d') diff --git a/examples2d/joint_motor_position2.rs b/examples2d/joint_motor_position2.rs index a701bd0..c44ea13 100644 --- a/examples2d/joint_motor_position2.rs +++ b/examples2d/joint_motor_position2.rs @@ -22,20 +22,52 @@ pub fn init_world(testbed: &mut Testbed) { colliders.insert_with_parent(collider, ground_handle, &mut bodies); /* - * A rectangle on a motor + * A rectangle on a motor with target position. */ for num in 0..9 { let x_pos = -6.0 + 1.5 * num as f32; - let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x_pos, 2.0]); + let rigid_body = RigidBodyBuilder::dynamic() + .translation(vector![x_pos, 2.0]) + .rotation(std::f32::consts::PI) + .can_sleep(false); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(0.1, 0.5); colliders.insert_with_parent(collider, handle, &mut bodies); let joint = RevoluteJointBuilder::new() - .local_anchor2(point![x_pos, 1.5]) - .local_anchor1(point![0.0, -0.5]) - .motor_position(std::f32::consts::PI / 4.0 * num as f32, 100.0, 15.0); - impulse_joints.insert(handle, ground_handle, joint, true); + .local_anchor1(point![x_pos, 1.5]) + .local_anchor2(point![0.0, -0.5]) + .motor_position( + (std::f32::consts::PI - std::f32::consts::PI / 4.0 * num as f32), + 1000.0, + 150.0, + ); + impulse_joints.insert(ground_handle, handle, joint, true); + } + + /* + * A rectangle on a motor with limits. + */ + for num in 2..3 { + let x_pos = -6.0 + 1.5 * num as f32; + let rigid_body = RigidBodyBuilder::dynamic() + .translation(vector![x_pos, 5.0]) + .angvel(4.0) + .can_sleep(false); + let handle = bodies.insert(rigid_body); + let collider = ColliderBuilder::cuboid(0.1, 0.5); + colliders.insert_with_parent(collider, handle, &mut bodies); + + let joint = RevoluteJointBuilder::new() + .local_anchor1(point![x_pos, 5.0]) + .local_anchor2(point![0.0, -0.5]) + .motor_velocity(1.5, 30.0) + .motor_max_force(100.0) + .limits([ + -std::f32::consts::PI, + std::f32::consts::PI / 4.0 * num as f32, + ]); + impulse_joints.insert(ground_handle, handle, joint, true); } /* -- cgit From 7c43e96943dd00bd57546213d918d44703d37b72 Mon Sep 17 00:00:00 2001 From: pellico Date: Thu, 27 Jul 2023 18:12:35 +0200 Subject: Removed not required parentheses --- examples2d/joint_motor_position2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples2d') diff --git a/examples2d/joint_motor_position2.rs b/examples2d/joint_motor_position2.rs index c44ea13..56866ac 100644 --- a/examples2d/joint_motor_position2.rs +++ b/examples2d/joint_motor_position2.rs @@ -38,7 +38,7 @@ pub fn init_world(testbed: &mut Testbed) { .local_anchor1(point![x_pos, 1.5]) .local_anchor2(point![0.0, -0.5]) .motor_position( - (std::f32::consts::PI - std::f32::consts::PI / 4.0 * num as f32), + std::f32::consts::PI - std::f32::consts::PI / 4.0 * num as f32, 1000.0, 150.0, ); -- cgit From efa1ac36097c1dbb085b76d6a48d58e1d717b253 Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Sun, 10 Dec 2023 21:52:27 +0100 Subject: fix initialization of the joint_motor_position examples --- examples2d/joint_motor_position2.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'examples2d') diff --git a/examples2d/joint_motor_position2.rs b/examples2d/joint_motor_position2.rs index 56866ac..c8374ff 100644 --- a/examples2d/joint_motor_position2.rs +++ b/examples2d/joint_motor_position2.rs @@ -11,15 +11,10 @@ pub fn init_world(testbed: &mut Testbed) { let multibody_joints = MultibodyJointSet::new(); /* - * The ground + * Fixed ground to attach one end of the joints. */ - let ground_size = 5.0; - let ground_height = 0.1; - - let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height]); + let rigid_body = RigidBodyBuilder::fixed(); let ground_handle = bodies.insert(rigid_body); - let collider = ColliderBuilder::cuboid(ground_size, ground_height); - colliders.insert_with_parent(collider, ground_handle, &mut bodies); /* * A rectangle on a motor with target position. @@ -28,7 +23,6 @@ pub fn init_world(testbed: &mut Testbed) { let x_pos = -6.0 + 1.5 * num as f32; let rigid_body = RigidBodyBuilder::dynamic() .translation(vector![x_pos, 2.0]) - .rotation(std::f32::consts::PI) .can_sleep(false); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(0.1, 0.5); @@ -38,7 +32,7 @@ pub fn init_world(testbed: &mut Testbed) { .local_anchor1(point![x_pos, 1.5]) .local_anchor2(point![0.0, -0.5]) .motor_position( - std::f32::consts::PI - std::f32::consts::PI / 4.0 * num as f32, + -std::f32::consts::PI + std::f32::consts::PI / 4.0 * num as f32, 1000.0, 150.0, ); @@ -48,11 +42,11 @@ pub fn init_world(testbed: &mut Testbed) { /* * A rectangle on a motor with limits. */ - for num in 2..3 { + for num in 0..8 { let x_pos = -6.0 + 1.5 * num as f32; let rigid_body = RigidBodyBuilder::dynamic() - .translation(vector![x_pos, 5.0]) - .angvel(4.0) + .translation(vector![x_pos, 4.5]) + .rotation(std::f32::consts::PI) .can_sleep(false); let handle = bodies.insert(rigid_body); let collider = ColliderBuilder::cuboid(0.1, 0.5); @@ -65,7 +59,7 @@ pub fn init_world(testbed: &mut Testbed) { .motor_max_force(100.0) .limits([ -std::f32::consts::PI, - std::f32::consts::PI / 4.0 * num as f32, + -std::f32::consts::PI + std::f32::consts::PI / 4.0 * num as f32, ]); impulse_joints.insert(ground_handle, handle, joint, true); } -- cgit