1#![allow(clippy::bad_bit_mask)] #![allow(clippy::unnecessary_cast)] #[cfg(feature = "alloc")]
5use crate::dynamics::RigidBody;
6use crate::dynamics::integration_parameters::SpringCoefficients;
7#[cfg(feature = "alloc")]
8use crate::dynamics::solver::MotorParameters;
9use crate::dynamics::{FixedJoint, MotorModel, PrismaticJoint, RevoluteJoint, RopeJoint};
10use crate::math::{Pose, Real, Rotation, SPATIAL_DIM, Vector};
11use crate::utils::{OrthonormalBasis, SimdRealCopy};
12use parry::math::Matrix;
13
14#[cfg(feature = "dim3")]
15use crate::dynamics::SphericalJoint;
16
17#[cfg(feature = "dim3")]
18bitflags::bitflags! {
19 #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
21 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
22 pub struct JointAxesMask: u8 {
23 const LIN_X = 1 << 0;
25 const LIN_Y = 1 << 1;
27 const LIN_Z = 1 << 2;
29 const ANG_X = 1 << 3;
31 const ANG_Y = 1 << 4;
33 const ANG_Z = 1 << 5;
35 const LOCKED_REVOLUTE_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
37 const LOCKED_PRISMATIC_AXES = Self::LIN_Y.bits() | Self::LIN_Z.bits() | Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
39 const LOCKED_FIXED_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits() | Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
41 const LOCKED_SPHERICAL_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits();
43 const FREE_REVOLUTE_AXES = Self::ANG_X.bits();
45 const FREE_PRISMATIC_AXES = Self::LIN_X.bits();
47 const FREE_FIXED_AXES = 0;
49 const FREE_SPHERICAL_AXES = Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
51 const LIN_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits();
53 const ANG_AXES = Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
55 }
56}
57
58#[cfg(feature = "dim2")]
59bitflags::bitflags! {
60 #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
62 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
63 pub struct JointAxesMask: u8 {
64 const LIN_X = 1 << 0;
66 const LIN_Y = 1 << 1;
68 const ANG_X = 1 << 2;
70 const LOCKED_REVOLUTE_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits();
72 const LOCKED_PRISMATIC_AXES = Self::LIN_Y.bits() | Self::ANG_X.bits();
74 const LOCKED_PIN_SLOT_AXES = Self::LIN_Y.bits();
76 const LOCKED_FIXED_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::ANG_X.bits();
78 const FREE_REVOLUTE_AXES = Self::ANG_X.bits();
80 const FREE_PRISMATIC_AXES = Self::LIN_X.bits();
82 const FREE_FIXED_AXES = 0;
84 const LIN_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits();
86 const ANG_AXES = Self::ANG_X.bits();
88 }
89}
90
91impl Default for JointAxesMask {
92 fn default() -> Self {
93 Self::empty()
94 }
95}
96
97#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
99#[derive(Copy, Clone, Debug, PartialEq)]
100pub enum JointAxis {
101 LinX = 0,
103 LinY,
105 #[cfg(feature = "dim3")]
107 LinZ,
108 AngX,
110 #[cfg(feature = "dim3")]
112 AngY,
113 #[cfg(feature = "dim3")]
115 AngZ,
116}
117
118impl From<JointAxis> for JointAxesMask {
119 fn from(axis: JointAxis) -> Self {
120 JointAxesMask::from_bits(1 << axis as usize).unwrap()
121 }
122}
123
124#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
133#[derive(Copy, Clone, Debug, PartialEq)]
134pub struct JointLimits<N> {
135 pub min: N,
137 pub max: N,
139 pub impulse: N,
141}
142
143impl<N: SimdRealCopy> Default for JointLimits<N> {
144 fn default() -> Self {
145 Self {
146 min: -N::splat(Real::MAX),
147 max: N::splat(Real::MAX),
148 impulse: N::splat(0.0),
149 }
150 }
151}
152
153impl<N: SimdRealCopy> From<[N; 2]> for JointLimits<N> {
154 fn from(value: [N; 2]) -> Self {
155 Self {
156 min: value[0],
157 max: value[1],
158 impulse: N::splat(0.0),
159 }
160 }
161}
162
163#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
194#[derive(Copy, Clone, Debug, PartialEq)]
195pub struct JointMotor {
196 pub target_vel: Real,
198 pub target_pos: Real,
200 pub stiffness: Real,
202 pub damping: Real,
204 pub max_force: Real,
206 pub impulse: Real,
208 pub model: MotorModel,
210}
211
212impl Default for JointMotor {
213 fn default() -> Self {
214 Self {
215 target_pos: 0.0,
216 target_vel: 0.0,
217 stiffness: 0.0,
218 damping: 0.0,
219 max_force: Real::MAX,
220 impulse: 0.0,
221 model: MotorModel::AccelerationBased,
222 }
223 }
224}
225
226#[cfg(feature = "alloc")]
227impl JointMotor {
228 pub(crate) fn motor_params(&self, dt: Real) -> MotorParameters<Real> {
229 let (erp_inv_dt, cfm_coeff, cfm_gain) =
230 self.model
231 .combine_coefficients(dt, self.stiffness, self.damping);
232 MotorParameters {
233 erp_inv_dt,
234 cfm_coeff,
235 cfm_gain,
236 target_pos: self.target_pos,
238 target_vel: self.target_vel,
239 max_impulse: self.max_force * dt,
240 }
241 }
242}
243
244#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
245#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
246pub enum JointEnabled {
248 Enabled,
250 DisabledByAttachedBody,
253 Disabled,
255}
256
257#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
258#[derive(Copy, Clone, Debug, PartialEq)]
259pub struct GenericJoint {
261 pub local_frame1: Pose,
263 pub local_frame2: Pose,
265 pub locked_axes: JointAxesMask,
267 pub limit_axes: JointAxesMask,
269 pub motor_axes: JointAxesMask,
271 pub coupled_axes: JointAxesMask,
278 pub limits: [JointLimits<Real>; SPATIAL_DIM],
284 pub motors: [JointMotor; SPATIAL_DIM],
290 pub softness: SpringCoefficients<Real>,
292 pub contacts_enabled: bool,
294 pub enabled: JointEnabled,
296 pub user_data: u128,
298}
299
300impl Default for GenericJoint {
301 fn default() -> Self {
302 Self {
303 local_frame1: Pose::IDENTITY,
304 local_frame2: Pose::IDENTITY,
305 locked_axes: JointAxesMask::empty(),
306 limit_axes: JointAxesMask::empty(),
307 motor_axes: JointAxesMask::empty(),
308 coupled_axes: JointAxesMask::empty(),
309 limits: [JointLimits::default(); SPATIAL_DIM],
310 motors: [JointMotor::default(); SPATIAL_DIM],
311 softness: SpringCoefficients::joint_defaults(),
312 contacts_enabled: true,
313 enabled: JointEnabled::Enabled,
314 user_data: 0,
315 }
316 }
317}
318
319impl GenericJoint {
320 #[must_use]
322 pub fn new(locked_axes: JointAxesMask) -> Self {
323 *Self::default().lock_axes(locked_axes)
324 }
325
326 #[cfg(feature = "simd-is-enabled")]
327 pub(crate) fn supports_simd_constraints(&self) -> bool {
329 self.limit_axes.is_empty() && self.motor_axes.is_empty()
330 }
331
332 #[doc(hidden)]
333 pub fn complete_ang_frame(axis: Vector) -> Rotation {
334 let basis = axis.orthonormal_basis();
335
336 #[cfg(feature = "dim2")]
337 {
338 let mat = Matrix::from_cols(axis, basis[0]);
339 Rotation::from_matrix_unchecked(mat)
340 }
341
342 #[cfg(feature = "dim3")]
343 {
344 let mat = Matrix::from_cols(axis, basis[0], basis[1]);
345 Rotation::from_mat3(&mat)
346 }
347 }
348
349 pub fn is_enabled(&self) -> bool {
351 self.enabled == JointEnabled::Enabled
352 }
353
354 pub fn set_enabled(&mut self, enabled: bool) {
356 match self.enabled {
357 JointEnabled::Enabled | JointEnabled::DisabledByAttachedBody => {
358 if !enabled {
359 self.enabled = JointEnabled::Disabled;
360 }
361 }
362 JointEnabled::Disabled => {
363 if enabled {
364 self.enabled = JointEnabled::Enabled;
365 }
366 }
367 }
368 }
369
370 pub fn lock_axes(&mut self, axes: JointAxesMask) -> &mut Self {
372 self.locked_axes |= axes;
373 self
374 }
375
376 pub fn set_local_frame1(&mut self, local_frame: Pose) -> &mut Self {
378 self.local_frame1 = local_frame;
379 self
380 }
381
382 pub fn set_local_frame2(&mut self, local_frame: Pose) -> &mut Self {
384 self.local_frame2 = local_frame;
385 self
386 }
387
388 #[must_use]
390 pub fn local_axis1(&self) -> Vector {
391 self.local_frame1 * Vector::X
392 }
393
394 pub fn set_local_axis1(&mut self, local_axis: Vector) -> &mut Self {
396 self.local_frame1.rotation = Self::complete_ang_frame(local_axis);
397 self
398 }
399
400 #[must_use]
402 pub fn local_axis2(&self) -> Vector {
403 self.local_frame2 * Vector::X
404 }
405
406 pub fn set_local_axis2(&mut self, local_axis: Vector) -> &mut Self {
408 self.local_frame2.rotation = Self::complete_ang_frame(local_axis);
409 self
410 }
411
412 #[must_use]
414 pub fn local_anchor1(&self) -> Vector {
415 self.local_frame1.translation
416 }
417
418 pub fn set_local_anchor1(&mut self, anchor1: Vector) -> &mut Self {
420 self.local_frame1.translation = anchor1;
421 self
422 }
423
424 #[must_use]
426 pub fn local_anchor2(&self) -> Vector {
427 self.local_frame2.translation
428 }
429
430 pub fn set_local_anchor2(&mut self, anchor2: Vector) -> &mut Self {
432 self.local_frame2.translation = anchor2;
433 self
434 }
435
436 pub fn contacts_enabled(&self) -> bool {
438 self.contacts_enabled
439 }
440
441 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
443 self.contacts_enabled = enabled;
444 self
445 }
446
447 #[must_use]
449 pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
450 self.softness = softness;
451 self
452 }
453
454 #[must_use]
456 pub fn limits(&self, axis: JointAxis) -> Option<&JointLimits<Real>> {
457 let i = axis as usize;
458 if self.limit_axes.contains(axis.into()) {
459 Some(&self.limits[i])
460 } else {
461 None
462 }
463 }
464
465 pub fn set_limits(&mut self, axis: JointAxis, limits: [Real; 2]) -> &mut Self {
467 let i = axis as usize;
468 self.limit_axes |= axis.into();
469 self.limits[i].min = limits[0];
470 self.limits[i].max = limits[1];
471 self
472 }
473
474 #[must_use]
476 pub fn motor_model(&self, axis: JointAxis) -> Option<MotorModel> {
477 let i = axis as usize;
478 if self.motor_axes.contains(axis.into()) {
479 Some(self.motors[i].model)
480 } else {
481 None
482 }
483 }
484
485 pub fn set_motor_model(&mut self, axis: JointAxis, model: MotorModel) -> &mut Self {
487 self.motors[axis as usize].model = model;
488 self
489 }
490
491 pub fn set_motor_velocity(
493 &mut self,
494 axis: JointAxis,
495 target_vel: Real,
496 factor: Real,
497 ) -> &mut Self {
498 self.set_motor(
499 axis,
500 self.motors[axis as usize].target_pos,
501 target_vel,
502 0.0,
503 factor,
504 )
505 }
506
507 pub fn set_motor_position(
509 &mut self,
510 axis: JointAxis,
511 target_pos: Real,
512 stiffness: Real,
513 damping: Real,
514 ) -> &mut Self {
515 self.set_motor(axis, target_pos, 0.0, stiffness, damping)
516 }
517
518 pub fn set_motor_max_force(&mut self, axis: JointAxis, max_force: Real) -> &mut Self {
520 self.motors[axis as usize].max_force = max_force;
521 self
522 }
523
524 #[must_use]
526 pub fn motor(&self, axis: JointAxis) -> Option<&JointMotor> {
527 let i = axis as usize;
528 if self.motor_axes.contains(axis.into()) {
529 Some(&self.motors[i])
530 } else {
531 None
532 }
533 }
534
535 pub fn set_motor(
537 &mut self,
538 axis: JointAxis,
539 target_pos: Real,
540 target_vel: Real,
541 stiffness: Real,
542 damping: Real,
543 ) -> &mut Self {
544 self.motor_axes |= axis.into();
545 let i = axis as usize;
546 self.motors[i].target_vel = target_vel;
547 self.motors[i].target_pos = target_pos;
548 self.motors[i].stiffness = stiffness;
549 self.motors[i].damping = damping;
550 self
551 }
552
553 pub fn flip(&mut self) {
555 core::mem::swap(&mut self.local_frame1, &mut self.local_frame2);
556
557 let coupled_bits = self.coupled_axes.bits();
558
559 for dim in 0..SPATIAL_DIM {
560 if coupled_bits & (1 << dim) == 0 {
561 let limit = self.limits[dim];
562 self.limits[dim].min = -limit.max;
563 self.limits[dim].max = -limit.min;
564 }
565
566 self.motors[dim].target_vel = -self.motors[dim].target_vel;
567 self.motors[dim].target_pos = -self.motors[dim].target_pos;
568 }
569 }
570
571 #[cfg(feature = "alloc")]
572 pub(crate) fn transform_to_solver_body_space(&mut self, rb1: &RigidBody, rb2: &RigidBody) {
573 if rb1.is_fixed() {
574 self.local_frame1 = rb1.pos.position * self.local_frame1;
575 } else {
576 self.local_frame1.translation -= rb1.mprops.local_mprops.local_com;
577 }
578
579 if rb2.is_fixed() {
580 self.local_frame2 = rb2.pos.position * self.local_frame2;
581 } else {
582 self.local_frame2.translation -= rb2.mprops.local_mprops.local_com;
583 }
584 }
585}
586
587macro_rules! joint_conversion_methods(
588 ($as_joint: ident, $as_joint_mut: ident, $Joint: ty, $axes: expr) => {
589 #[must_use]
591 pub fn $as_joint(&self) -> Option<&$Joint> {
592 if self.locked_axes == $axes {
593 Some(unsafe { core::mem::transmute::<&Self, &$Joint>(self) })
596 } else {
597 None
598 }
599 }
600
601 #[must_use]
603 pub fn $as_joint_mut(&mut self) -> Option<&mut $Joint> {
604 if self.locked_axes == $axes {
605 Some(unsafe { core::mem::transmute::<&mut Self, &mut $Joint>(self) })
608 } else {
609 None
610 }
611 }
612 }
613);
614
615impl GenericJoint {
616 joint_conversion_methods!(
617 as_revolute,
618 as_revolute_mut,
619 RevoluteJoint,
620 JointAxesMask::LOCKED_REVOLUTE_AXES
621 );
622 joint_conversion_methods!(
623 as_fixed,
624 as_fixed_mut,
625 FixedJoint,
626 JointAxesMask::LOCKED_FIXED_AXES
627 );
628 joint_conversion_methods!(
629 as_prismatic,
630 as_prismatic_mut,
631 PrismaticJoint,
632 JointAxesMask::LOCKED_PRISMATIC_AXES
633 );
634 joint_conversion_methods!(
635 as_rope,
636 as_rope_mut,
637 RopeJoint,
638 JointAxesMask::FREE_FIXED_AXES
639 );
640
641 #[cfg(feature = "dim3")]
642 joint_conversion_methods!(
643 as_spherical,
644 as_spherical_mut,
645 SphericalJoint,
646 JointAxesMask::LOCKED_SPHERICAL_AXES
647 );
648}
649
650#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
652#[derive(Copy, Clone, Debug, PartialEq)]
653pub struct GenericJointBuilder(pub GenericJoint);
654
655impl GenericJointBuilder {
656 #[must_use]
658 pub fn new(locked_axes: JointAxesMask) -> Self {
659 Self(GenericJoint::new(locked_axes))
660 }
661
662 #[must_use]
664 pub fn locked_axes(mut self, axes: JointAxesMask) -> Self {
665 self.0.locked_axes = axes;
666 self
667 }
668
669 #[must_use]
671 pub fn contacts_enabled(mut self, enabled: bool) -> Self {
672 self.0.contacts_enabled = enabled;
673 self
674 }
675
676 #[must_use]
678 pub fn local_frame1(mut self, local_frame: Pose) -> Self {
679 self.0.set_local_frame1(local_frame);
680 self
681 }
682
683 #[must_use]
685 pub fn local_frame2(mut self, local_frame: Pose) -> Self {
686 self.0.set_local_frame2(local_frame);
687 self
688 }
689
690 #[must_use]
692 pub fn local_axis1(mut self, local_axis: Vector) -> Self {
693 self.0.set_local_axis1(local_axis);
694 self
695 }
696
697 #[must_use]
699 pub fn local_axis2(mut self, local_axis: Vector) -> Self {
700 self.0.set_local_axis2(local_axis);
701 self
702 }
703
704 #[must_use]
706 pub fn local_anchor1(mut self, anchor1: Vector) -> Self {
707 self.0.set_local_anchor1(anchor1);
708 self
709 }
710
711 #[must_use]
713 pub fn local_anchor2(mut self, anchor2: Vector) -> Self {
714 self.0.set_local_anchor2(anchor2);
715 self
716 }
717
718 #[must_use]
720 pub fn limits(mut self, axis: JointAxis, limits: [Real; 2]) -> Self {
721 self.0.set_limits(axis, limits);
722 self
723 }
724
725 #[must_use]
727 pub fn coupled_axes(mut self, axes: JointAxesMask) -> Self {
728 self.0.coupled_axes = axes;
729 self
730 }
731
732 #[must_use]
734 pub fn motor_model(mut self, axis: JointAxis, model: MotorModel) -> Self {
735 self.0.set_motor_model(axis, model);
736 self
737 }
738
739 #[must_use]
741 pub fn motor_velocity(mut self, axis: JointAxis, target_vel: Real, factor: Real) -> Self {
742 self.0.set_motor_velocity(axis, target_vel, factor);
743 self
744 }
745
746 #[must_use]
748 pub fn motor_position(
749 mut self,
750 axis: JointAxis,
751 target_pos: Real,
752 stiffness: Real,
753 damping: Real,
754 ) -> Self {
755 self.0
756 .set_motor_position(axis, target_pos, stiffness, damping);
757 self
758 }
759
760 #[must_use]
762 pub fn set_motor(
763 mut self,
764 axis: JointAxis,
765 target_pos: Real,
766 target_vel: Real,
767 stiffness: Real,
768 damping: Real,
769 ) -> Self {
770 self.0
771 .set_motor(axis, target_pos, target_vel, stiffness, damping);
772 self
773 }
774
775 #[must_use]
777 pub fn motor_max_force(mut self, axis: JointAxis, max_force: Real) -> Self {
778 self.0.set_motor_max_force(axis, max_force);
779 self
780 }
781
782 #[must_use]
784 pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
785 self.0.softness = softness;
786 self
787 }
788
789 pub fn user_data(mut self, data: u128) -> Self {
791 self.0.user_data = data;
792 self
793 }
794
795 #[must_use]
797 pub fn build(self) -> GenericJoint {
798 self.0
799 }
800}
801
802impl From<GenericJointBuilder> for GenericJoint {
803 fn from(val: GenericJointBuilder) -> GenericJoint {
804 val.0
805 }
806}