Skip to main content

rapier3d/dynamics/joint/
generic_joint.rs

1#![allow(clippy::bad_bit_mask)] // Clippy will complain about the bitmasks due to JointAxesMask::FREE_FIXED_AXES being 0.
2#![allow(clippy::unnecessary_cast)] // Casts are needed for switching between f32/f64.
3
4#[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    /// A bit mask identifying multiple degrees of freedom of a joint.
20    #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
21    #[derive(Copy, Clone, PartialEq, Eq, Debug)]
22    pub struct JointAxesMask: u8 {
23        /// The linear (translational) degree of freedom along the local X axis of a joint.
24        const LIN_X = 1 << 0;
25        /// The linear (translational) degree of freedom along the local Y axis of a joint.
26        const LIN_Y = 1 << 1;
27        /// The linear (translational) degree of freedom along the local Z axis of a joint.
28        const LIN_Z = 1 << 2;
29        /// The angular degree of freedom along the local X axis of a joint.
30        const ANG_X = 1 << 3;
31        /// The angular degree of freedom along the local Y axis of a joint.
32        const ANG_Y = 1 << 4;
33        /// The angular degree of freedom along the local Z axis of a joint.
34        const ANG_Z = 1 << 5;
35        /// The set of degrees of freedom locked by a revolute joint.
36        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        /// The set of degrees of freedom locked by a prismatic joint.
38        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        /// The set of degrees of freedom locked by a fixed joint.
40        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        /// The set of degrees of freedom locked by a spherical joint.
42        const LOCKED_SPHERICAL_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits();
43        /// The set of degrees of freedom left free by a revolute joint.
44        const FREE_REVOLUTE_AXES = Self::ANG_X.bits();
45        /// The set of degrees of freedom left free by a prismatic joint.
46        const FREE_PRISMATIC_AXES = Self::LIN_X.bits();
47        /// The set of degrees of freedom left free by a fixed joint.
48        const FREE_FIXED_AXES = 0;
49        /// The set of degrees of freedom left free by a spherical joint.
50        const FREE_SPHERICAL_AXES = Self::ANG_X.bits() | Self::ANG_Y.bits() | Self::ANG_Z.bits();
51        /// The set of all translational degrees of freedom.
52        const LIN_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::LIN_Z.bits();
53        /// The set of all angular degrees of freedom.
54        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    /// A bit mask identifying multiple degrees of freedom of a joint.
61    #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
62    #[derive(Copy, Clone, PartialEq, Eq, Debug)]
63    pub struct JointAxesMask: u8 {
64        /// The linear (translational) degree of freedom along the local X axis of a joint.
65        const LIN_X = 1 << 0;
66        /// The linear (translational) degree of freedom along the local Y axis of a joint.
67        const LIN_Y = 1 << 1;
68        /// The angular degree of freedom of a joint.
69        const ANG_X = 1 << 2;
70        /// The set of degrees of freedom locked by a revolute joint.
71        const LOCKED_REVOLUTE_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits();
72        /// The set of degrees of freedom locked by a prismatic joint.
73        const LOCKED_PRISMATIC_AXES = Self::LIN_Y.bits() | Self::ANG_X.bits();
74        /// The set of degrees of freedom locked by a pin slot joint.
75        const LOCKED_PIN_SLOT_AXES = Self::LIN_Y.bits();
76        /// The set of degrees of freedom locked by a fixed joint.
77        const LOCKED_FIXED_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits() | Self::ANG_X.bits();
78        /// The set of degrees of freedom left free by a revolute joint.
79        const FREE_REVOLUTE_AXES = Self::ANG_X.bits();
80        /// The set of degrees of freedom left free by a prismatic joint.
81        const FREE_PRISMATIC_AXES = Self::LIN_X.bits();
82        /// The set of degrees of freedom left free by a fixed joint.
83        const FREE_FIXED_AXES = 0;
84        /// The set of all translational degrees of freedom.
85        const LIN_AXES = Self::LIN_X.bits() | Self::LIN_Y.bits();
86        /// The set of all angular degrees of freedom.
87        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/// Identifiers of degrees of freedoms of a joint.
98#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
99#[derive(Copy, Clone, Debug, PartialEq)]
100pub enum JointAxis {
101    /// The linear (translational) degree of freedom along the joint’s local X axis.
102    LinX = 0,
103    /// The linear (translational) degree of freedom along the joint’s local Y axis.
104    LinY,
105    /// The linear (translational) degree of freedom along the joint’s local Z axis.
106    #[cfg(feature = "dim3")]
107    LinZ,
108    /// The rotational degree of freedom along the joint’s local X axis.
109    AngX,
110    /// The rotational degree of freedom along the joint’s local Y axis.
111    #[cfg(feature = "dim3")]
112    AngY,
113    /// The rotational degree of freedom along the joint’s local Z axis.
114    #[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/// Limits that restrict a joint's range of motion along one axis.
125///
126/// Use to constrain how far a joint can move/rotate. Examples:
127/// - Door that only opens 90°: revolute joint with limits `[0.0, PI/2.0]`
128/// - Piston with 2-unit stroke: prismatic joint with limits `[0.0, 2.0]`
129/// - Elbow that bends 0-150°: revolute joint with limits `[0.0, 5*PI/6]`
130///
131/// When a joint hits its limit, forces are applied to prevent further movement in that direction.
132#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
133#[derive(Copy, Clone, Debug, PartialEq)]
134pub struct JointLimits<N> {
135    /// Minimum allowed value (angle for revolute, distance for prismatic).
136    pub min: N,
137    /// Maximum allowed value (angle for revolute, distance for prismatic).
138    pub max: N,
139    /// Internal: impulse being applied to enforce the limit.
140    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/// A powered motor that drives a joint toward a target position/velocity.
164///
165/// Motors add actuation to joints - they apply forces to make the joint move toward
166/// a desired state. Think of them as servos, electric motors, or hydraulic actuators.
167///
168/// ## Two control modes
169///
170/// 1. **Velocity control**: Set `target_vel` to make the motor spin/slide at constant speed
171/// 2. **Position control**: Set `target_pos` with `stiffness`/`damping` to reach a target angle/position
172///
173/// You can combine both for precise control.
174///
175/// ## Parameters
176///
177/// - `stiffness`: How strongly to pull toward target (spring constant)
178/// - `damping`: Resistance to motion (prevents oscillation)
179/// - `max_force`: Maximum force/torque the motor can apply
180///
181/// # Example
182/// ```
183/// # use rapier3d::prelude::*;
184/// # use rapier3d::dynamics::{RevoluteJoint, PrismaticJoint};
185/// # let mut revolute_joint = RevoluteJoint::new(Vector::X);
186/// # let mut prismatic_joint = PrismaticJoint::new(Vector::X);
187/// // Motor that spins a wheel at 10 rad/s
188/// revolute_joint.set_motor_velocity(10.0, 0.8);
189///
190/// // Motor that moves to position 5.0
191/// prismatic_joint.set_motor_position(5.0, 100.0, 10.0);  // stiffness=100, damping=10
192/// ```
193#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
194#[derive(Copy, Clone, Debug, PartialEq)]
195pub struct JointMotor {
196    /// Target velocity (units/sec for prismatic, rad/sec for revolute).
197    pub target_vel: Real,
198    /// Target position (units for prismatic, radians for revolute).
199    pub target_pos: Real,
200    /// Spring constant - how strongly to pull toward target position.
201    pub stiffness: Real,
202    /// Damping coefficient - resistance to motion (prevents oscillation).
203    pub damping: Real,
204    /// Maximum force the motor can apply (Newtons for prismatic, Nm for revolute).
205    pub max_force: Real,
206    /// Internal: current impulse being applied.
207    pub impulse: Real,
208    /// Force-based or acceleration-based motor model.
209    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            // keep_lhs,
237            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))]
246/// Enum indicating whether or not a joint is enabled.
247pub enum JointEnabled {
248    /// The joint is enabled.
249    Enabled,
250    /// The joint wasn’t disabled by the user explicitly but it is attached to
251    /// a disabled rigid-body.
252    DisabledByAttachedBody,
253    /// The joint is disabled by the user explicitly.
254    Disabled,
255}
256
257#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
258#[derive(Copy, Clone, Debug, PartialEq)]
259/// A generic joint.
260pub struct GenericJoint {
261    /// The joint’s frame, expressed in the first rigid-body’s local-space.
262    pub local_frame1: Pose,
263    /// The joint’s frame, expressed in the second rigid-body’s local-space.
264    pub local_frame2: Pose,
265    /// The degrees-of-freedoms locked by this joint.
266    pub locked_axes: JointAxesMask,
267    /// The degrees-of-freedoms limited by this joint.
268    pub limit_axes: JointAxesMask,
269    /// The degrees-of-freedoms motorised by this joint.
270    pub motor_axes: JointAxesMask,
271    /// The coupled degrees of freedom of this joint.
272    ///
273    /// Note that coupling degrees of freedoms (DoF) changes the interpretation of the coupled joint’s limits and motors.
274    /// If multiple linear DoF are limited/motorized, only the limits/motor configuration for the first
275    /// coupled linear DoF is applied to all coupled linear DoF. Similarly, if multiple angular DoF are limited/motorized
276    /// only the limits/motor configuration for the first coupled angular DoF is applied to all coupled angular DoF.
277    pub coupled_axes: JointAxesMask,
278    /// The limits, along each degree of freedoms of this joint.
279    ///
280    /// Note that the limit must also be explicitly enabled by the `limit_axes` bitmask.
281    /// For coupled degrees of freedoms (DoF), only the first linear (resp. angular) coupled DoF limit and `limit_axis`
282    /// bitmask is applied to the coupled linear (resp. angular) axes.
283    pub limits: [JointLimits<Real>; SPATIAL_DIM],
284    /// The motors, along each degree of freedoms of this joint.
285    ///
286    /// Note that the motor must also be explicitly enabled by the `motor_axes` bitmask.
287    /// For coupled degrees of freedoms (DoF), only the first linear (resp. angular) coupled DoF motor and `motor_axes`
288    /// bitmask is applied to the coupled linear (resp. angular) axes.
289    pub motors: [JointMotor; SPATIAL_DIM],
290    /// The coefficients controlling the joint constraints’ softness.
291    pub softness: SpringCoefficients<Real>,
292    /// Are contacts between the attached rigid-bodies enabled?
293    pub contacts_enabled: bool,
294    /// Whether the joint is enabled.
295    pub enabled: JointEnabled,
296    /// User-defined data associated to this joint.
297    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    /// Creates a new generic joint that locks the specified degrees of freedom.
321    #[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    /// Can this joint use SIMD-accelerated constraint formulations?
328    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    /// Is this joint enabled?
350    pub fn is_enabled(&self) -> bool {
351        self.enabled == JointEnabled::Enabled
352    }
353
354    /// Set whether this joint is enabled or not.
355    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    /// Add the specified axes to the set of axes locked by this joint.
371    pub fn lock_axes(&mut self, axes: JointAxesMask) -> &mut Self {
372        self.locked_axes |= axes;
373        self
374    }
375
376    /// Sets the joint’s frame, expressed in the first rigid-body’s local-space.
377    pub fn set_local_frame1(&mut self, local_frame: Pose) -> &mut Self {
378        self.local_frame1 = local_frame;
379        self
380    }
381
382    /// Sets the joint’s frame, expressed in the second rigid-body’s local-space.
383    pub fn set_local_frame2(&mut self, local_frame: Pose) -> &mut Self {
384        self.local_frame2 = local_frame;
385        self
386    }
387
388    /// The principal (local X) axis of this joint, expressed in the first rigid-body’s local-space.
389    #[must_use]
390    pub fn local_axis1(&self) -> Vector {
391        self.local_frame1 * Vector::X
392    }
393
394    /// Sets the principal (local X) axis of this joint, expressed in the first rigid-body’s local-space.
395    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    /// The principal (local X) axis of this joint, expressed in the second rigid-body’s local-space.
401    #[must_use]
402    pub fn local_axis2(&self) -> Vector {
403        self.local_frame2 * Vector::X
404    }
405
406    /// Sets the principal (local X) axis of this joint, expressed in the second rigid-body’s local-space.
407    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    /// The anchor of this joint, expressed in the first rigid-body’s local-space.
413    #[must_use]
414    pub fn local_anchor1(&self) -> Vector {
415        self.local_frame1.translation
416    }
417
418    /// Sets anchor of this joint, expressed in the first rigid-body's local-space.
419    pub fn set_local_anchor1(&mut self, anchor1: Vector) -> &mut Self {
420        self.local_frame1.translation = anchor1;
421        self
422    }
423
424    /// The anchor of this joint, expressed in the second rigid-body's local-space.
425    #[must_use]
426    pub fn local_anchor2(&self) -> Vector {
427        self.local_frame2.translation
428    }
429
430    /// Sets anchor of this joint, expressed in the second rigid-body's local-space.
431    pub fn set_local_anchor2(&mut self, anchor2: Vector) -> &mut Self {
432        self.local_frame2.translation = anchor2;
433        self
434    }
435
436    /// Are contacts between the attached rigid-bodies enabled?
437    pub fn contacts_enabled(&self) -> bool {
438        self.contacts_enabled
439    }
440
441    /// Sets whether contacts between the attached rigid-bodies are enabled.
442    pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
443        self.contacts_enabled = enabled;
444        self
445    }
446
447    /// Sets the spring coefficients controlling this joint constraint’s softness.
448    #[must_use]
449    pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
450        self.softness = softness;
451        self
452    }
453
454    /// The joint limits along the specified axis.
455    #[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    /// Sets the joint limits along the specified axis.
466    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    /// The spring-like motor model along the specified axis of this joint.
475    #[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    /// Set the spring-like model used by the motor to reach the desired target velocity and position.
486    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    /// Sets the target velocity this motor needs to reach.
492    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    /// Sets the target angle this motor needs to reach.
508    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    /// Sets the maximum force the motor can deliver along the specified axis.
519    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    /// The motor affecting the joint’s degree of freedom along the specified axis.
525    #[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    /// Configure both the target angle and target velocity of the motor.
536    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    /// Flips the orientation of the joint, including limits and motors.
554    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        /// Converts the joint to its specific variant, if it is one.
590        #[must_use]
591        pub fn $as_joint(&self) -> Option<&$Joint> {
592            if self.locked_axes == $axes {
593                // SAFETY: this is OK because the target joint type is
594                //         a `repr(transparent)` newtype of `Joint`.
595                Some(unsafe { core::mem::transmute::<&Self, &$Joint>(self) })
596            } else {
597                None
598            }
599        }
600
601        /// Converts the joint to its specific mutable variant, if it is one.
602        #[must_use]
603        pub fn $as_joint_mut(&mut self) -> Option<&mut $Joint> {
604            if self.locked_axes == $axes {
605                // SAFETY: this is OK because the target joint type is
606                //         a `repr(transparent)` newtype of `Joint`.
607                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/// Create generic joints using the builder pattern.
651#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
652#[derive(Copy, Clone, Debug, PartialEq)]
653pub struct GenericJointBuilder(pub GenericJoint);
654
655impl GenericJointBuilder {
656    /// Creates a new generic joint builder.
657    #[must_use]
658    pub fn new(locked_axes: JointAxesMask) -> Self {
659        Self(GenericJoint::new(locked_axes))
660    }
661
662    /// Sets the degrees of freedom locked by the joint.
663    #[must_use]
664    pub fn locked_axes(mut self, axes: JointAxesMask) -> Self {
665        self.0.locked_axes = axes;
666        self
667    }
668
669    /// Sets whether contacts between the attached rigid-bodies are enabled.
670    #[must_use]
671    pub fn contacts_enabled(mut self, enabled: bool) -> Self {
672        self.0.contacts_enabled = enabled;
673        self
674    }
675
676    /// Sets the joint’s frame, expressed in the first rigid-body’s local-space.
677    #[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    /// Sets the joint’s frame, expressed in the second rigid-body’s local-space.
684    #[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    /// Sets the principal (local X) axis of this joint, expressed in the first rigid-body’s local-space.
691    #[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    /// Sets the principal (local X) axis of this joint, expressed in the second rigid-body’s local-space.
698    #[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    /// Sets the anchor of this joint, expressed in the first rigid-body’s local-space.
705    #[must_use]
706    pub fn local_anchor1(mut self, anchor1: Vector) -> Self {
707        self.0.set_local_anchor1(anchor1);
708        self
709    }
710
711    /// Sets the anchor of this joint, expressed in the second rigid-body’s local-space.
712    #[must_use]
713    pub fn local_anchor2(mut self, anchor2: Vector) -> Self {
714        self.0.set_local_anchor2(anchor2);
715        self
716    }
717
718    /// Sets the joint limits along the specified axis.
719    #[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    /// Sets the coupled degrees of freedom for this joint’s limits and motor.
726    #[must_use]
727    pub fn coupled_axes(mut self, axes: JointAxesMask) -> Self {
728        self.0.coupled_axes = axes;
729        self
730    }
731
732    /// Set the spring-like model used by the motor to reach the desired target velocity and position.
733    #[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    /// Sets the target velocity this motor needs to reach.
740    #[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    /// Sets the target angle this motor needs to reach.
747    #[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    /// Configure both the target angle and target velocity of the motor.
761    #[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    /// Sets the maximum force the motor can deliver along the specified axis.
776    #[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    /// Sets the softness of this joint’s locked degrees of freedom.
783    #[must_use]
784    pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
785        self.0.softness = softness;
786        self
787    }
788
789    /// An arbitrary user-defined 128-bit integer associated to the joints built by this builder.
790    pub fn user_data(mut self, data: u128) -> Self {
791        self.0.user_data = data;
792        self
793    }
794
795    /// Builds the generic joint.
796    #[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}