Skip to main content

bevy_rapier3d/dynamics/
fixed_joint.rs

1use crate::dynamics::{GenericJoint, GenericJointBuilder};
2use crate::math::{Real, Rot, Vect};
3use rapier::dynamics::JointAxesMask;
4
5use super::{SpringCoefficients, TypedJoint};
6
7#[derive(Copy, Clone, Debug, PartialEq)]
8#[repr(transparent)]
9/// A fixed joint, locks all relative motion between two bodies.
10pub struct FixedJoint {
11    /// The underlying joint data.
12    pub data: GenericJoint,
13}
14
15impl Default for FixedJoint {
16    fn default() -> Self {
17        FixedJoint::new()
18    }
19}
20
21impl FixedJoint {
22    /// Creates a new fixed joint.
23    #[must_use]
24    pub fn new() -> Self {
25        let data = GenericJointBuilder::new(JointAxesMask::LOCKED_FIXED_AXES).build();
26        Self { data }
27    }
28
29    /// Are contacts between the attached rigid-bodies enabled?
30    pub fn contacts_enabled(&self) -> bool {
31        self.data.contacts_enabled()
32    }
33
34    /// Sets whether contacts between the attached rigid-bodies are enabled.
35    pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
36        self.data.set_contacts_enabled(enabled);
37        self
38    }
39
40    /// The spring coefficients controlling the softness of this joint’s locked degrees of freedom.
41    #[must_use]
42    pub fn softness(&self) -> SpringCoefficients<Real> {
43        self.data.softness()
44    }
45
46    /// Sets the spring coefficients controlling the softness of this joint’s locked degrees of freedom.
47    pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
48        self.data.set_softness(softness);
49        self
50    }
51
52    /// The joint’s basis, expressed in the first rigid-body’s local-space.
53    #[must_use]
54    pub fn local_basis1(&self) -> Rot {
55        self.data.local_basis1()
56    }
57
58    /// Sets the joint’s basis, expressed in the first rigid-body’s local-space.
59    pub fn set_local_basis1(&mut self, local_basis: Rot) -> &mut Self {
60        self.data.set_local_basis1(local_basis);
61        self
62    }
63
64    /// The joint’s basis, expressed in the second rigid-body’s local-space.
65    #[must_use]
66    pub fn local_basis2(&self) -> Rot {
67        self.data.local_basis2()
68    }
69
70    /// Sets joint’s basis, expressed in the second rigid-body’s local-space.
71    pub fn set_local_basis2(&mut self, local_basis: Rot) -> &mut Self {
72        self.data.set_local_basis2(local_basis);
73        self
74    }
75
76    /// The joint’s anchor, expressed in the local-space of the first rigid-body.
77    #[must_use]
78    pub fn local_anchor1(&self) -> Vect {
79        self.data.local_anchor1()
80    }
81
82    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
83    pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
84        self.data.set_local_anchor1(anchor1);
85        self
86    }
87
88    /// The joint’s anchor, expressed in the local-space of the second rigid-body.
89    #[must_use]
90    pub fn local_anchor2(&self) -> Vect {
91        self.data.local_anchor2()
92    }
93
94    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
95    pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
96        self.data.set_local_anchor2(anchor2);
97        self
98    }
99}
100
101impl From<FixedJoint> for GenericJoint {
102    fn from(joint: FixedJoint) -> GenericJoint {
103        joint.data
104    }
105}
106
107/// Create fixed joints using the builder pattern.
108#[derive(Copy, Clone, Debug, PartialEq, Default)]
109pub struct FixedJointBuilder(FixedJoint);
110
111impl FixedJointBuilder {
112    /// Creates a new builder for fixed joints.
113    pub fn new() -> Self {
114        Self(FixedJoint::new())
115    }
116
117    /// Sets the joint’s basis, expressed in the first rigid-body’s local-space.
118    #[must_use]
119    pub fn local_basis1(mut self, local_basis: Rot) -> Self {
120        self.0.set_local_basis1(local_basis);
121        self
122    }
123
124    /// Sets joint’s basis, expressed in the second rigid-body’s local-space.
125    #[must_use]
126    pub fn local_basis2(mut self, local_basis: Rot) -> Self {
127        self.0.set_local_basis2(local_basis);
128        self
129    }
130
131    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
132    #[must_use]
133    pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
134        self.0.set_local_anchor1(anchor1);
135        self
136    }
137
138    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
139    #[must_use]
140    pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
141        self.0.set_local_anchor2(anchor2);
142        self
143    }
144
145    /// Sets whether contacts between the attached rigid-bodies are enabled.
146    #[must_use]
147    pub fn contacts_enabled(mut self, enabled: bool) -> Self {
148        self.0.set_contacts_enabled(enabled);
149        self
150    }
151
152    /// Sets the spring coefficients controlling the softness of this joint’s locked degrees of freedom.
153    #[must_use]
154    pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
155        self.0.set_softness(softness);
156        self
157    }
158
159    /// Build the fixed joint.
160    #[must_use]
161    pub fn build(self) -> FixedJoint {
162        self.0
163    }
164}
165
166impl From<FixedJointBuilder> for TypedJoint {
167    fn from(joint: FixedJointBuilder) -> TypedJoint {
168        joint.0.into()
169    }
170}
171
172impl From<FixedJoint> for TypedJoint {
173    fn from(joint: FixedJoint) -> TypedJoint {
174        TypedJoint::FixedJoint(joint)
175    }
176}