Skip to main content

rapier2d/dynamics/joint/impulse_joint/
impulse_joint.rs

1use crate::dynamics::{GenericJoint, ImpulseJointHandle, RigidBodyHandle};
2use crate::math::SpatialVector;
3
4#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
5#[derive(Clone, Debug, PartialEq)]
6/// An impulse-based joint attached to two bodies.
7pub struct ImpulseJoint {
8    /// Handle to the first body attached to this joint.
9    ///
10    /// Kept crate-private so the attached bodies can only be changed via
11    /// [`ImpulseJointSet::set_bodies`], which also updates the interaction
12    /// graph and island-manager bookkeeping. Read it through
13    /// [`ImpulseJoint::body1`].
14    pub(crate) body1: RigidBodyHandle,
15    /// Handle to the second body attached to this joint. See
16    /// [`ImpulseJoint::body1`] for why this is not directly writeable.
17    pub(crate) body2: RigidBodyHandle,
18
19    /// The joint’s description.
20    pub data: GenericJoint,
21
22    /// The impulses applied by this joint.
23    pub impulses: SpatialVector,
24
25    // A joint needs to know its handle to simplify its removal.
26    pub(crate) handle: ImpulseJointHandle,
27
28    /// The solver-body index (`active_set_id`) of each attached body, or
29    /// `u32::MAX` for a side that is world-attached from the solver's point of
30    /// view (fixed or sleeping).
31    #[cfg_attr(
32        feature = "serde-serialize",
33        serde(skip, default = "default_solver_body_ids")
34    )]
35    pub(crate) solver_body_ids: [u32; 2],
36    /// The solver graph color assigned to this joint.
37    #[cfg_attr(feature = "serde-serialize", serde(default = "default_solver_color"))]
38    pub(crate) solver_color: u8,
39}
40
41#[cfg(feature = "serde-serialize")]
42fn default_solver_color() -> u8 {
43    crate::geometry::contact_pair::SOLVER_COLOR_UNCOLORED
44}
45
46#[cfg(feature = "serde-serialize")]
47fn default_solver_body_ids() -> [u32; 2] {
48    [u32::MAX; 2]
49}
50
51impl ImpulseJoint {
52    /// Handle to the first body attached to this joint.
53    #[inline]
54    pub fn body1(&self) -> RigidBodyHandle {
55        self.body1
56    }
57
58    /// Handle to the second body attached to this joint.
59    #[inline]
60    pub fn body2(&self) -> RigidBodyHandle {
61        self.body2
62    }
63}