Add actuated ball and revolute joint to the 3D joint demo.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use na::{Isometry3, Point3, Unit, Vector3};
|
use na::{Isometry3, Point3, Unit, UnitQuaternion, Vector3};
|
||||||
use rapier3d::dynamics::{
|
use rapier3d::dynamics::{
|
||||||
BallJoint, BodyStatus, FixedJoint, JointSet, PrismaticJoint, RevoluteJoint, RigidBodyBuilder,
|
BallJoint, BodyStatus, FixedJoint, JointSet, PrismaticJoint, RevoluteJoint, RigidBodyBuilder,
|
||||||
RigidBodySet,
|
RigidBodyHandle, RigidBodySet,
|
||||||
};
|
};
|
||||||
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
|
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
|
||||||
use rapier_testbed3d::Testbed;
|
use rapier_testbed3d::Testbed;
|
||||||
@@ -14,7 +14,7 @@ fn create_prismatic_joints(
|
|||||||
num: usize,
|
num: usize,
|
||||||
) {
|
) {
|
||||||
let rad = 0.4;
|
let rad = 0.4;
|
||||||
let shift = 1.0;
|
let shift = 2.0;
|
||||||
|
|
||||||
let ground = RigidBodyBuilder::new_static()
|
let ground = RigidBodyBuilder::new_static()
|
||||||
.translation(origin.x, origin.y, origin.z)
|
.translation(origin.x, origin.y, origin.z)
|
||||||
@@ -40,7 +40,7 @@ fn create_prismatic_joints(
|
|||||||
|
|
||||||
let z = Vector3::z();
|
let z = Vector3::z();
|
||||||
let mut prism = PrismaticJoint::new(
|
let mut prism = PrismaticJoint::new(
|
||||||
Point3::origin(),
|
Point3::new(0.0, 0.0, 0.0),
|
||||||
axis,
|
axis,
|
||||||
z,
|
z,
|
||||||
Point3::new(0.0, 0.0, -shift),
|
Point3::new(0.0, 0.0, -shift),
|
||||||
@@ -50,6 +50,7 @@ fn create_prismatic_joints(
|
|||||||
prism.limits_enabled = true;
|
prism.limits_enabled = true;
|
||||||
prism.limits[0] = -2.0;
|
prism.limits[0] = -2.0;
|
||||||
prism.limits[1] = 2.0;
|
prism.limits[1] = 2.0;
|
||||||
|
|
||||||
joints.insert(bodies, curr_parent, curr_child, prism);
|
joints.insert(bodies, curr_parent, curr_child, prism);
|
||||||
|
|
||||||
curr_parent = curr_child;
|
curr_parent = curr_child;
|
||||||
@@ -222,6 +223,130 @@ fn create_ball_joints(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_actuated_revolute_joints(
|
||||||
|
bodies: &mut RigidBodySet,
|
||||||
|
colliders: &mut ColliderSet,
|
||||||
|
joints: &mut JointSet,
|
||||||
|
origin: Point3<f32>,
|
||||||
|
num: usize,
|
||||||
|
) {
|
||||||
|
let rad = 0.4;
|
||||||
|
let shift = 2.0;
|
||||||
|
|
||||||
|
// We will reuse this base configuration for all the joints here.
|
||||||
|
let joint_template = RevoluteJoint::new(
|
||||||
|
Point3::origin(),
|
||||||
|
Vector3::z_axis(),
|
||||||
|
Point3::new(0.0, 0.0, -shift),
|
||||||
|
Vector3::z_axis(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut parent_handle = RigidBodyHandle::invalid();
|
||||||
|
|
||||||
|
for i in 0..num {
|
||||||
|
let fi = i as f32;
|
||||||
|
|
||||||
|
// NOTE: the num - 2 test is to avoid two consecutive
|
||||||
|
// fixed bodies. Because physx will crash if we add
|
||||||
|
// a joint between these.
|
||||||
|
let status = if i == 0 {
|
||||||
|
BodyStatus::Static
|
||||||
|
} else {
|
||||||
|
BodyStatus::Dynamic
|
||||||
|
};
|
||||||
|
|
||||||
|
let shifty = (i >= 1) as u32 as f32 * -2.0;
|
||||||
|
|
||||||
|
let rigid_body = RigidBodyBuilder::new(status)
|
||||||
|
.translation(origin.x, origin.y + shifty, origin.z + fi * shift)
|
||||||
|
// .rotation(Vector3::new(0.0, fi * 1.1, 0.0))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let child_handle = bodies.insert(rigid_body);
|
||||||
|
let collider = ColliderBuilder::cuboid(rad * 2.0, rad * 6.0 / (fi + 1.0), rad).build();
|
||||||
|
colliders.insert(collider, child_handle, bodies);
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
let mut joint = joint_template.clone();
|
||||||
|
|
||||||
|
if i % 3 == 1 {
|
||||||
|
joint.configure_motor_velocity(-20.0, 0.1);
|
||||||
|
} else if i == num - 1 {
|
||||||
|
let stiffness = 0.2;
|
||||||
|
let damping = 1.0;
|
||||||
|
joint.configure_motor_position(3.14 / 2.0, stiffness, damping);
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == 1 {
|
||||||
|
joint.local_anchor2.y = 2.0;
|
||||||
|
joint.configure_motor_velocity(-2.0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
joints.insert(bodies, parent_handle, child_handle, joint);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent_handle = child_handle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_actuated_ball_joints(
|
||||||
|
bodies: &mut RigidBodySet,
|
||||||
|
colliders: &mut ColliderSet,
|
||||||
|
joints: &mut JointSet,
|
||||||
|
origin: Point3<f32>,
|
||||||
|
num: usize,
|
||||||
|
) {
|
||||||
|
let rad = 0.4;
|
||||||
|
let shift = 2.0;
|
||||||
|
|
||||||
|
// We will reuse this base configuration for all the joints here.
|
||||||
|
let joint_template = BallJoint::new(Point3::new(0.0, 0.0, shift), Point3::origin());
|
||||||
|
|
||||||
|
let mut parent_handle = RigidBodyHandle::invalid();
|
||||||
|
|
||||||
|
for i in 0..num {
|
||||||
|
let fi = i as f32;
|
||||||
|
|
||||||
|
// NOTE: the num - 2 test is to avoid two consecutive
|
||||||
|
// fixed bodies. Because physx will crash if we add
|
||||||
|
// a joint between these.
|
||||||
|
let status = if i == 0 {
|
||||||
|
BodyStatus::Static
|
||||||
|
} else {
|
||||||
|
BodyStatus::Dynamic
|
||||||
|
};
|
||||||
|
|
||||||
|
let rigid_body = RigidBodyBuilder::new(status)
|
||||||
|
.translation(origin.x, origin.y, origin.z + fi * shift)
|
||||||
|
// .rotation(Vector3::new(0.0, fi * 1.1, 0.0))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let child_handle = bodies.insert(rigid_body);
|
||||||
|
let collider = ColliderBuilder::capsule_y(rad * 2.0 / (fi + 1.0), rad).build();
|
||||||
|
colliders.insert(collider, child_handle, bodies);
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
let mut joint = joint_template.clone();
|
||||||
|
|
||||||
|
if i == 1 {
|
||||||
|
joint.configure_motor_velocity(Vector3::new(0.0, 0.5, -2.0), 0.1);
|
||||||
|
} else if i == num - 1 {
|
||||||
|
let stiffness = 0.2;
|
||||||
|
let damping = 1.0;
|
||||||
|
joint.configure_motor_position(
|
||||||
|
UnitQuaternion::new(Vector3::new(0.0, 1.0, 3.14 / 2.0)),
|
||||||
|
stiffness,
|
||||||
|
damping,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
joints.insert(bodies, parent_handle, child_handle, joint);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent_handle = child_handle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init_world(testbed: &mut Testbed) {
|
pub fn init_world(testbed: &mut Testbed) {
|
||||||
/*
|
/*
|
||||||
* World
|
* World
|
||||||
@@ -234,8 +359,8 @@ pub fn init_world(testbed: &mut Testbed) {
|
|||||||
&mut bodies,
|
&mut bodies,
|
||||||
&mut colliders,
|
&mut colliders,
|
||||||
&mut joints,
|
&mut joints,
|
||||||
Point3::new(20.0, 10.0, 0.0),
|
Point3::new(20.0, 5.0, 0.0),
|
||||||
5,
|
4,
|
||||||
);
|
);
|
||||||
create_revolute_joints(
|
create_revolute_joints(
|
||||||
&mut bodies,
|
&mut bodies,
|
||||||
@@ -249,7 +374,21 @@ pub fn init_world(testbed: &mut Testbed) {
|
|||||||
&mut colliders,
|
&mut colliders,
|
||||||
&mut joints,
|
&mut joints,
|
||||||
Point3::new(0.0, 10.0, 0.0),
|
Point3::new(0.0, 10.0, 0.0),
|
||||||
5,
|
10,
|
||||||
|
);
|
||||||
|
create_actuated_revolute_joints(
|
||||||
|
&mut bodies,
|
||||||
|
&mut colliders,
|
||||||
|
&mut joints,
|
||||||
|
Point3::new(20.0, 10.0, 0.0),
|
||||||
|
6,
|
||||||
|
);
|
||||||
|
create_actuated_ball_joints(
|
||||||
|
&mut bodies,
|
||||||
|
&mut colliders,
|
||||||
|
&mut joints,
|
||||||
|
Point3::new(13.0, 10.0, 0.0),
|
||||||
|
3,
|
||||||
);
|
);
|
||||||
create_ball_joints(&mut bodies, &mut colliders, &mut joints, 15);
|
create_ball_joints(&mut bodies, &mut colliders, &mut joints, 15);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user