bevy_rapier3d/dynamics/
fixed_joint.rs

1use crate::dynamics::{GenericJoint, GenericJointBuilder};
2use crate::math::{Rot, Vect};
3use rapier::dynamics::JointAxesMask;
4
5use super::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 joint’s basis, expressed in the first rigid-body’s local-space.
41    #[must_use]
42    pub fn local_basis1(&self) -> Rot {
43        self.data.local_basis1()
44    }
45
46    /// Sets the joint’s basis, expressed in the first rigid-body’s local-space.
47    pub fn set_local_basis1(&mut self, local_basis: Rot) -> &mut Self {
48        self.data.set_local_basis1(local_basis);
49        self
50    }
51
52    /// The joint’s basis, expressed in the second rigid-body’s local-space.
53    #[must_use]
54    pub fn local_basis2(&self) -> Rot {
55        self.data.local_basis2()
56    }
57
58    /// Sets joint’s basis, expressed in the second rigid-body’s local-space.
59    pub fn set_local_basis2(&mut self, local_basis: Rot) -> &mut Self {
60        self.data.set_local_basis2(local_basis);
61        self
62    }
63
64    /// The joint’s anchor, expressed in the local-space of the first rigid-body.
65    #[must_use]
66    pub fn local_anchor1(&self) -> Vect {
67        self.data.local_anchor1()
68    }
69
70    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
71    pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
72        self.data.set_local_anchor1(anchor1);
73        self
74    }
75
76    /// The joint’s anchor, expressed in the local-space of the second rigid-body.
77    #[must_use]
78    pub fn local_anchor2(&self) -> Vect {
79        self.data.local_anchor2()
80    }
81
82    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
83    pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
84        self.data.set_local_anchor2(anchor2);
85        self
86    }
87}
88
89impl From<FixedJoint> for GenericJoint {
90    fn from(joint: FixedJoint) -> GenericJoint {
91        joint.data
92    }
93}
94
95/// Create fixed joints using the builder pattern.
96#[derive(Copy, Clone, Debug, PartialEq, Default)]
97pub struct FixedJointBuilder(FixedJoint);
98
99impl FixedJointBuilder {
100    /// Creates a new builder for fixed joints.
101    pub fn new() -> Self {
102        Self(FixedJoint::new())
103    }
104
105    /// Sets the joint’s basis, expressed in the first rigid-body’s local-space.
106    #[must_use]
107    pub fn local_basis1(mut self, local_basis: Rot) -> Self {
108        self.0.set_local_basis1(local_basis);
109        self
110    }
111
112    /// Sets joint’s basis, expressed in the second rigid-body’s local-space.
113    #[must_use]
114    pub fn local_basis2(mut self, local_basis: Rot) -> Self {
115        self.0.set_local_basis2(local_basis);
116        self
117    }
118
119    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
120    #[must_use]
121    pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
122        self.0.set_local_anchor1(anchor1);
123        self
124    }
125
126    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
127    #[must_use]
128    pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
129        self.0.set_local_anchor2(anchor2);
130        self
131    }
132
133    /// Build the fixed joint.
134    #[must_use]
135    pub fn build(self) -> FixedJoint {
136        self.0
137    }
138}
139
140impl From<FixedJointBuilder> for TypedJoint {
141    fn from(joint: FixedJointBuilder) -> TypedJoint {
142        joint.0.into()
143    }
144}
145
146impl From<FixedJoint> for TypedJoint {
147    fn from(joint: FixedJoint) -> TypedJoint {
148        TypedJoint::FixedJoint(joint)
149    }
150}