rapier2d/dynamics/joint/
fixed_joint.rs

1use crate::dynamics::integration_parameters::SpringCoefficients;
2use crate::dynamics::{GenericJoint, GenericJointBuilder, JointAxesMask};
3use crate::math::{Isometry, Point, Real};
4
5#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
6#[derive(Copy, Clone, Debug, PartialEq)]
7#[repr(transparent)]
8/// A joint that rigidly connects two bodies together (like welding them).
9///
10/// Fixed joints lock all relative motion - the two bodies move as if they were a single
11/// solid object. Use for:
12/// - Permanently attaching objects (gluing, welding)
13/// - Composite objects made of multiple bodies
14/// - Connecting parts of a structure
15///
16/// Unlike simply using one body with multiple colliders, fixed joints let you attach
17/// bodies that were created separately, and you can break the connection later if needed.
18pub struct FixedJoint {
19    /// The underlying joint data.
20    pub data: GenericJoint,
21}
22
23impl Default for FixedJoint {
24    fn default() -> Self {
25        FixedJoint::new()
26    }
27}
28
29impl FixedJoint {
30    /// Creates a new fixed joint.
31    #[must_use]
32    pub fn new() -> Self {
33        let data = GenericJointBuilder::new(JointAxesMask::LOCKED_FIXED_AXES).build();
34        Self { data }
35    }
36
37    /// Are contacts between the attached rigid-bodies enabled?
38    pub fn contacts_enabled(&self) -> bool {
39        self.data.contacts_enabled
40    }
41
42    /// Sets whether contacts between the attached rigid-bodies are enabled.
43    pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
44        self.data.set_contacts_enabled(enabled);
45        self
46    }
47
48    /// The joint’s frame, expressed in the first rigid-body’s local-space.
49    #[must_use]
50    pub fn local_frame1(&self) -> &Isometry<Real> {
51        &self.data.local_frame1
52    }
53
54    /// Sets the joint’s frame, expressed in the first rigid-body’s local-space.
55    pub fn set_local_frame1(&mut self, local_frame: Isometry<Real>) -> &mut Self {
56        self.data.set_local_frame1(local_frame);
57        self
58    }
59
60    /// The joint’s frame, expressed in the second rigid-body’s local-space.
61    #[must_use]
62    pub fn local_frame2(&self) -> &Isometry<Real> {
63        &self.data.local_frame2
64    }
65
66    /// Sets joint’s frame, expressed in the second rigid-body’s local-space.
67    pub fn set_local_frame2(&mut self, local_frame: Isometry<Real>) -> &mut Self {
68        self.data.set_local_frame2(local_frame);
69        self
70    }
71
72    /// The joint’s anchor, expressed in the local-space of the first rigid-body.
73    #[must_use]
74    pub fn local_anchor1(&self) -> Point<Real> {
75        self.data.local_anchor1()
76    }
77
78    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
79    pub fn set_local_anchor1(&mut self, anchor1: Point<Real>) -> &mut Self {
80        self.data.set_local_anchor1(anchor1);
81        self
82    }
83
84    /// The joint’s anchor, expressed in the local-space of the second rigid-body.
85    #[must_use]
86    pub fn local_anchor2(&self) -> Point<Real> {
87        self.data.local_anchor2()
88    }
89
90    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
91    pub fn set_local_anchor2(&mut self, anchor2: Point<Real>) -> &mut Self {
92        self.data.set_local_anchor2(anchor2);
93        self
94    }
95
96    /// Gets the softness of this joint’s locked degrees of freedom.
97    #[must_use]
98    pub fn softness(&self) -> SpringCoefficients<Real> {
99        self.data.softness
100    }
101
102    /// Sets the softness of this joint’s locked degrees of freedom.
103    #[must_use]
104    pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
105        self.data.softness = softness;
106        self
107    }
108}
109
110impl From<FixedJoint> for GenericJoint {
111    fn from(val: FixedJoint) -> GenericJoint {
112        val.data
113    }
114}
115
116/// Create fixed joints using the builder pattern.
117#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
118#[derive(Copy, Clone, Debug, PartialEq, Default)]
119pub struct FixedJointBuilder(pub FixedJoint);
120
121impl FixedJointBuilder {
122    /// Creates a new builder for fixed joints.
123    pub fn new() -> Self {
124        Self(FixedJoint::new())
125    }
126
127    /// Sets whether contacts between the attached rigid-bodies are enabled.
128    #[must_use]
129    pub fn contacts_enabled(mut self, enabled: bool) -> Self {
130        self.0.set_contacts_enabled(enabled);
131        self
132    }
133
134    /// Sets the joint’s frame, expressed in the first rigid-body’s local-space.
135    #[must_use]
136    pub fn local_frame1(mut self, local_frame: Isometry<Real>) -> Self {
137        self.0.set_local_frame1(local_frame);
138        self
139    }
140
141    /// Sets joint’s frame, expressed in the second rigid-body’s local-space.
142    #[must_use]
143    pub fn local_frame2(mut self, local_frame: Isometry<Real>) -> Self {
144        self.0.set_local_frame2(local_frame);
145        self
146    }
147
148    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
149    #[must_use]
150    pub fn local_anchor1(mut self, anchor1: Point<Real>) -> Self {
151        self.0.set_local_anchor1(anchor1);
152        self
153    }
154
155    /// Sets the joint's anchor, expressed in the local-space of the second rigid-body.
156    #[must_use]
157    pub fn local_anchor2(mut self, anchor2: Point<Real>) -> Self {
158        self.0.set_local_anchor2(anchor2);
159        self
160    }
161
162    /// Sets the softness of this joint’s locked degrees of freedom.
163    #[must_use]
164    pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
165        self.0.data.softness = softness;
166        self
167    }
168
169    /// Build the fixed joint.
170    #[must_use]
171    pub fn build(self) -> FixedJoint {
172        self.0
173    }
174}
175
176impl From<FixedJointBuilder> for GenericJoint {
177    fn from(val: FixedJointBuilder) -> GenericJoint {
178        val.0.into()
179    }
180}