bevy_rapier3d/dynamics/
rope_joint.rs

1use crate::dynamics::{GenericJoint, GenericJointBuilder};
2use crate::math::{Real, Vect};
3use rapier::dynamics::{JointAxesMask, JointAxis, JointLimits, JointMotor, MotorModel};
4
5use super::TypedJoint;
6
7#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
8#[derive(Copy, Clone, Debug, PartialEq)]
9#[repr(transparent)]
10/// A rope joint, limits the maximum distance between two bodies
11pub struct RopeJoint {
12    /// The underlying joint data.
13    pub data: GenericJoint,
14}
15
16impl RopeJoint {
17    /// Creates a new rope joint limiting the max distance between to bodies
18    pub fn new(max_dist: Real) -> Self {
19        let data = GenericJointBuilder::new(JointAxesMask::empty())
20            .coupled_axes(JointAxesMask::LIN_AXES)
21            .build();
22        let mut result = Self { data };
23        result.set_max_distance(max_dist);
24        result
25    }
26
27    /// Are contacts between the attached rigid-bodies enabled?
28    pub fn contacts_enabled(&self) -> bool {
29        self.data.contacts_enabled()
30    }
31
32    /// Sets whether contacts between the attached rigid-bodies are enabled.
33    pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
34        self.data.set_contacts_enabled(enabled);
35        self
36    }
37
38    /// The joint’s anchor, expressed in the local-space of the first rigid-body.
39    #[must_use]
40    pub fn local_anchor1(&self) -> Vect {
41        self.data.local_anchor1()
42    }
43
44    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
45    pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
46        self.data.set_local_anchor1(anchor1);
47        self
48    }
49
50    /// The joint’s anchor, expressed in the local-space of the second rigid-body.
51    #[must_use]
52    pub fn local_anchor2(&self) -> Vect {
53        self.data.local_anchor2()
54    }
55
56    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
57    pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
58        self.data.set_local_anchor2(anchor2);
59        self
60    }
61
62    /// The principal axis of the joint, expressed in the local-space of the first rigid-body.
63    #[must_use]
64    pub fn local_axis1(&self) -> Vect {
65        self.data.local_axis1()
66    }
67
68    /// Sets the principal axis of the joint, expressed in the local-space of the first rigid-body.
69    pub fn set_local_axis1(&mut self, axis1: Vect) -> &mut Self {
70        self.data.set_local_axis1(axis1);
71        self
72    }
73
74    /// The principal axis of the joint, expressed in the local-space of the second rigid-body.
75    #[must_use]
76    pub fn local_axis2(&self) -> Vect {
77        self.data.local_axis2()
78    }
79
80    /// Sets the principal axis of the joint, expressed in the local-space of the second rigid-body.
81    pub fn set_local_axis2(&mut self, axis2: Vect) -> &mut Self {
82        self.data.set_local_axis2(axis2);
83        self
84    }
85
86    /// The motor affecting the joint’s translational degree of freedom.
87    #[must_use]
88    pub fn motor(&self, axis: JointAxis) -> Option<&JointMotor> {
89        self.data.motor(axis)
90    }
91
92    /// Set the spring-like model used by the motor to reach the desired target velocity and position.
93    pub fn set_motor_model(&mut self, model: MotorModel) -> &mut Self {
94        self.data.set_motor_model(JointAxis::LinX, model);
95        self
96    }
97
98    /// Sets the target velocity this motor needs to reach.
99    pub fn set_motor_velocity(&mut self, target_vel: Real, factor: Real) -> &mut Self {
100        self.data
101            .set_motor_velocity(JointAxis::LinX, target_vel, factor);
102        self
103    }
104
105    /// Sets the target angle this motor needs to reach.
106    pub fn set_motor_position(
107        &mut self,
108        target_pos: Real,
109        stiffness: Real,
110        damping: Real,
111    ) -> &mut Self {
112        self.data
113            .set_motor_position(JointAxis::LinX, target_pos, stiffness, damping);
114        self
115    }
116
117    /// Configure both the target angle and target velocity of the motor.
118    pub fn set_motor(
119        &mut self,
120        target_pos: Real,
121        target_vel: Real,
122        stiffness: Real,
123        damping: Real,
124    ) -> &mut Self {
125        self.data
126            .set_motor(JointAxis::LinX, target_pos, target_vel, stiffness, damping);
127        self
128    }
129
130    /// Sets the maximum force the motor can deliver.
131    pub fn set_motor_max_force(&mut self, max_force: Real) -> &mut Self {
132        self.data.set_motor_max_force(JointAxis::LinX, max_force);
133        self
134    }
135
136    /// The limit distance attached bodies can translate along the joint’s principal axis.
137    #[must_use]
138    pub fn limits(&self, axis: JointAxis) -> Option<&JointLimits<Real>> {
139        self.data.limits(axis)
140    }
141
142    /// Sets the maximum allowed distance between the attached bodies.
143    ///
144    /// The `max_dist` must be strictly greater than 0.0.
145    pub fn set_max_distance(&mut self, max_dist: Real) -> &mut Self {
146        self.data.set_limits(JointAxis::LinX, [0.0, max_dist]);
147        self
148    }
149
150    /// The maximum distance between the attached bodies.
151    pub fn max_distance(&self) -> Real {
152        self.data
153            .limits(JointAxis::LinX)
154            .map(|l| l.max)
155            .unwrap_or(Real::MAX)
156    }
157}
158
159impl From<RopeJoint> for GenericJoint {
160    fn from(joint: RopeJoint) -> GenericJoint {
161        joint.data
162    }
163}
164
165/// Create rope joints using the builder pattern.
166///
167/// A rope joint, limits the maximum distance between two bodies.
168#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
169#[derive(Copy, Clone, Debug, PartialEq)]
170pub struct RopeJointBuilder(RopeJoint);
171
172impl RopeJointBuilder {
173    /// Creates a new builder for rope joints.
174    pub fn new(max_dist: Real) -> Self {
175        Self(RopeJoint::new(max_dist))
176    }
177
178    /// Sets the joint’s anchor, expressed in the local-space of the first rigid-body.
179    #[must_use]
180    pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
181        self.0.set_local_anchor1(anchor1);
182        self
183    }
184
185    /// Sets the joint’s anchor, expressed in the local-space of the second rigid-body.
186    #[must_use]
187    pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
188        self.0.set_local_anchor2(anchor2);
189        self
190    }
191
192    /// Sets the principal axis of the joint, expressed in the local-space of the first rigid-body.
193    #[must_use]
194    pub fn local_axis1(mut self, axis1: Vect) -> Self {
195        self.0.set_local_axis1(axis1);
196        self
197    }
198
199    /// Sets the principal axis of the joint, expressed in the local-space of the second rigid-body.
200    #[must_use]
201    pub fn local_axis2(mut self, axis2: Vect) -> Self {
202        self.0.set_local_axis2(axis2);
203        self
204    }
205
206    /// Set the spring-like model used by the motor to reach the desired target velocity and position.
207    #[must_use]
208    pub fn motor_model(mut self, model: MotorModel) -> Self {
209        self.0.set_motor_model(model);
210        self
211    }
212
213    /// Sets the target velocity this motor needs to reach.
214    #[must_use]
215    pub fn motor_velocity(mut self, target_vel: Real, factor: Real) -> Self {
216        self.0.set_motor_velocity(target_vel, factor);
217        self
218    }
219
220    /// Sets the target angle this motor needs to reach.
221    #[must_use]
222    pub fn motor_position(mut self, target_pos: Real, stiffness: Real, damping: Real) -> Self {
223        self.0.set_motor_position(target_pos, stiffness, damping);
224        self
225    }
226
227    /// Configure both the target angle and target velocity of the motor.
228    #[must_use]
229    pub fn set_motor(
230        mut self,
231        target_pos: Real,
232        target_vel: Real,
233        stiffness: Real,
234        damping: Real,
235    ) -> Self {
236        self.0.set_motor(target_pos, target_vel, stiffness, damping);
237        self
238    }
239
240    /// Sets the maximum force the motor can deliver.
241    #[must_use]
242    pub fn motor_max_force(mut self, max_force: Real) -> Self {
243        self.0.set_motor_max_force(max_force);
244        self
245    }
246
247    /// Sets the maximum allowed distance between the attached bodies.
248    ///
249    /// The `max_dist` must be strictly greater than 0.0.    
250    #[must_use]
251    pub fn max_distance(mut self, max_dist: Real) -> Self {
252        self.0.set_max_distance(max_dist);
253        self
254    }
255
256    /// Builds the rope joint.
257    #[must_use]
258    pub fn build(self) -> RopeJoint {
259        self.0
260    }
261}
262
263impl From<RopeJointBuilder> for TypedJoint {
264    fn from(joint: RopeJointBuilder) -> TypedJoint {
265        joint.0.into()
266    }
267}
268
269impl From<RopeJoint> for TypedJoint {
270    fn from(joint: RopeJoint) -> TypedJoint {
271        TypedJoint::RopeJoint(joint)
272    }
273}