Skip to content

Commit b8612bf

Browse files
committed
allow to set local rotation of revolute joint bodies in 3D
1 parent 872e8f0 commit b8612bf

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/dynamics/solver/joints/revolute.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ pub struct RevoluteJoint {
2525
pub local_anchor1: Vector,
2626
/// Attachment point on the second body.
2727
pub local_anchor2: Vector,
28+
/// Rotation applied on the first body. This allows to orient the body relative to the `aligned_axis`.
29+
#[cfg(feature = "3d")]
30+
pub local_rotation1: Rotation,
31+
/// Rotation applied on the second body.
32+
#[cfg(feature = "3d")]
33+
pub local_rotation2: Rotation,
2834
/// A unit vector that controls which axis should be aligned for both entities.
2935
///
3036
/// In 2D this should always be the Z axis.
@@ -105,6 +111,10 @@ impl Joint for RevoluteJoint {
105111
entity2,
106112
local_anchor1: Vector::ZERO,
107113
local_anchor2: Vector::ZERO,
114+
#[cfg(feature = "3d")]
115+
local_rotation1: Rotation::default(),
116+
#[cfg(feature = "3d")]
117+
local_rotation2: Rotation::default(),
108118
aligned_axis: Vector3::Z,
109119
angle_limit: None,
110120
damping_linear: 1.0,
@@ -194,8 +204,8 @@ impl RevoluteJoint {
194204

195205
#[cfg(feature = "3d")]
196206
fn get_rotation_difference(&self, rot1: &Rotation, rot2: &Rotation) -> Vector3 {
197-
let a1 = rot1 * self.aligned_axis;
198-
let a2 = rot2 * self.aligned_axis;
207+
let a1 = *rot1 * self.local_rotation1 * self.aligned_axis;
208+
let a2 = *rot2 * self.local_rotation2 * self.aligned_axis;
199209
a1.cross(a2)
200210
}
201211

@@ -215,9 +225,13 @@ impl RevoluteJoint {
215225
#[cfg(feature = "3d")]
216226
{
217227
// [n, n1, n2] = [a1, b1, b2], where [a, b, c] are perpendicular unit axes on the bodies.
218-
let a1 = *body1.rotation * self.aligned_axis;
219-
let b1 = *body1.rotation * self.aligned_axis.any_orthonormal_vector();
220-
let b2 = *body2.rotation * self.aligned_axis.any_orthonormal_vector();
228+
let a1 = *body1.rotation * self.local_rotation1 * self.aligned_axis;
229+
let b1 = *body1.rotation
230+
* self.local_rotation1
231+
* self.aligned_axis.any_orthonormal_vector();
232+
let b2 = *body2.rotation
233+
* self.local_rotation2
234+
* self.aligned_axis.any_orthonormal_vector();
221235
angle_limit.compute_correction(a1, b1, b2, dt)
222236
}
223237
}) else {

0 commit comments

Comments
 (0)