Skip to content

Commit

Permalink
Merge pull request #119 from dimforge/joint_drive
Browse files Browse the repository at this point in the history
Add joint motors
  • Loading branch information
sebcrozet committed Feb 22, 2021
2 parents c650bb1 + e5c4c2e commit d31a327
Show file tree
Hide file tree
Showing 28 changed files with 3,517 additions and 369 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ members = [ "build/rapier2d", "build/rapier2d-f64", "build/rapier_testbed2d", "e
#nalgebra = { path = "../nalgebra" }

#kiss3d = { git = "https://github.com/sebcrozet/kiss3d" }
#parry2d = { git = "https://github.com/sebcrozet/parry" }
#parry3d = { git = "https://github.com/sebcrozet/parry" }
#parry2d-f64 = { git = "https://github.com/sebcrozet/parry" }
#parry3d-f64 = { git = "https://github.com/sebcrozet/parry" }
nalgebra = { git = "https://github.com/dimforge/nalgebra", branch = "dev" }
parry2d = { git = "https://github.com/dimforge/parry" }
parry3d = { git = "https://github.com/dimforge/parry" }
parry2d-f64 = { git = "https://github.com/dimforge/parry" }
parry3d-f64 = { git = "https://github.com/dimforge/parry" }
#ncollide2d = { git = "https://github.com/dimforge/ncollide" }
#ncollide3d = { git = "https://github.com/dimforge/ncollide" }
#nphysics2d = { git = "https://github.com/dimforge/nphysics" }
Expand Down
227 changes: 220 additions & 7 deletions examples3d/joints3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use na::{Isometry3, Point3, Unit, Vector3};
use na::{Isometry3, Point3, Unit, UnitQuaternion, Vector3};
use rapier3d::dynamics::{
BallJoint, BodyStatus, FixedJoint, JointSet, PrismaticJoint, RevoluteJoint, RigidBodyBuilder,
RigidBodySet,
RigidBodyHandle, RigidBodySet,
};
use rapier3d::geometry::{ColliderBuilder, ColliderSet};
use rapier_testbed3d::Testbed;
Expand All @@ -14,7 +14,7 @@ fn create_prismatic_joints(
num: usize,
) {
let rad = 0.4;
let shift = 1.0;
let shift = 2.0;

let ground = RigidBodyBuilder::new_static()
.translation(origin.x, origin.y, origin.z)
Expand All @@ -40,7 +40,7 @@ fn create_prismatic_joints(

let z = Vector3::z();
let mut prism = PrismaticJoint::new(
Point3::origin(),
Point3::new(0.0, 0.0, 0.0),
axis,
z,
Point3::new(0.0, 0.0, -shift),
Expand All @@ -50,6 +50,74 @@ fn create_prismatic_joints(
prism.limits_enabled = true;
prism.limits[0] = -2.0;
prism.limits[1] = 2.0;

joints.insert(bodies, curr_parent, curr_child, prism);

curr_parent = curr_child;
}
}

fn create_actuated_prismatic_joints(
bodies: &mut RigidBodySet,
colliders: &mut ColliderSet,
joints: &mut JointSet,
origin: Point3<f32>,
num: usize,
) {
let rad = 0.4;
let shift = 2.0;

let ground = RigidBodyBuilder::new_static()
.translation(origin.x, origin.y, origin.z)
.build();
let mut curr_parent = bodies.insert(ground);
let collider = ColliderBuilder::cuboid(rad, rad, rad).build();
colliders.insert(collider, curr_parent, bodies);

for i in 0..num {
let z = origin.z + (i + 1) as f32 * shift;
let rigid_body = RigidBodyBuilder::new_dynamic()
.translation(origin.x, origin.y, z)
.build();
let curr_child = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad, rad).build();
colliders.insert(collider, curr_child, bodies);

let axis = if i % 2 == 0 {
Unit::new_normalize(Vector3::new(1.0, 1.0, 0.0))
} else {
Unit::new_normalize(Vector3::new(-1.0, 1.0, 0.0))
};

let z = Vector3::z();
let mut prism = PrismaticJoint::new(
Point3::new(0.0, 0.0, 0.0),
axis,
z,
Point3::new(0.0, 0.0, -shift),
axis,
z,
);

if i == 1 {
prism.configure_motor_velocity(1.0, 1.0);
prism.limits_enabled = true;
prism.limits[1] = 5.0;
// We set a max impulse so that the motor doesn't fight
// the limits with large forces.
prism.motor_max_impulse = 1.0;
} else if i > 1 {
prism.configure_motor_position(2.0, 0.2, 1.0);
} else {
prism.configure_motor_velocity(1.0, 1.0);
// We set a max impulse so that the motor doesn't fight
// the limits with large forces.
prism.motor_max_impulse = 1.0;
prism.limits_enabled = true;
prism.limits[0] = -2.0;
prism.limits[1] = 5.0;
}

joints.insert(bodies, curr_parent, curr_child, prism);

curr_parent = curr_child;
Expand Down Expand Up @@ -222,6 +290,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) {
/*
* World
Expand All @@ -234,8 +426,15 @@ pub fn init_world(testbed: &mut Testbed) {
&mut bodies,
&mut colliders,
&mut joints,
Point3::new(20.0, 10.0, 0.0),
5,
Point3::new(20.0, 5.0, 0.0),
4,
);
create_actuated_prismatic_joints(
&mut bodies,
&mut colliders,
&mut joints,
Point3::new(25.0, 5.0, 0.0),
4,
);
create_revolute_joints(
&mut bodies,
Expand All @@ -249,7 +448,21 @@ pub fn init_world(testbed: &mut Testbed) {
&mut colliders,
&mut joints,
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);

Expand Down
2 changes: 1 addition & 1 deletion src/dynamics/integration_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::math::Real;

/// Parameters for a time-step of the physics engine.
#[derive(Clone)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct IntegrationParameters {
/// The timestep length (default: `1.0 / 60.0`)
Expand Down
Loading

0 comments on commit d31a327

Please sign in to comment.