bevy_rapier3d/dynamics/
rope_joint.rs1use 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)]
10pub struct RopeJoint {
12 pub data: GenericJoint,
14}
15
16impl RopeJoint {
17 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 pub fn contacts_enabled(&self) -> bool {
29 self.data.contacts_enabled()
30 }
31
32 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
34 self.data.set_contacts_enabled(enabled);
35 self
36 }
37
38 #[must_use]
40 pub fn local_anchor1(&self) -> Vect {
41 self.data.local_anchor1()
42 }
43
44 pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
46 self.data.set_local_anchor1(anchor1);
47 self
48 }
49
50 #[must_use]
52 pub fn local_anchor2(&self) -> Vect {
53 self.data.local_anchor2()
54 }
55
56 pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
58 self.data.set_local_anchor2(anchor2);
59 self
60 }
61
62 #[must_use]
64 pub fn local_axis1(&self) -> Vect {
65 self.data.local_axis1()
66 }
67
68 pub fn set_local_axis1(&mut self, axis1: Vect) -> &mut Self {
70 self.data.set_local_axis1(axis1);
71 self
72 }
73
74 #[must_use]
76 pub fn local_axis2(&self) -> Vect {
77 self.data.local_axis2()
78 }
79
80 pub fn set_local_axis2(&mut self, axis2: Vect) -> &mut Self {
82 self.data.set_local_axis2(axis2);
83 self
84 }
85
86 #[must_use]
88 pub fn motor(&self, axis: JointAxis) -> Option<&JointMotor> {
89 self.data.motor(axis)
90 }
91
92 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 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 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 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 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 #[must_use]
138 pub fn limits(&self, axis: JointAxis) -> Option<&JointLimits<Real>> {
139 self.data.limits(axis)
140 }
141
142 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 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#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
169#[derive(Copy, Clone, Debug, PartialEq)]
170pub struct RopeJointBuilder(RopeJoint);
171
172impl RopeJointBuilder {
173 pub fn new(max_dist: Real) -> Self {
175 Self(RopeJoint::new(max_dist))
176 }
177
178 #[must_use]
180 pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
181 self.0.set_local_anchor1(anchor1);
182 self
183 }
184
185 #[must_use]
187 pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
188 self.0.set_local_anchor2(anchor2);
189 self
190 }
191
192 #[must_use]
194 pub fn local_axis1(mut self, axis1: Vect) -> Self {
195 self.0.set_local_axis1(axis1);
196 self
197 }
198
199 #[must_use]
201 pub fn local_axis2(mut self, axis2: Vect) -> Self {
202 self.0.set_local_axis2(axis2);
203 self
204 }
205
206 #[must_use]
208 pub fn motor_model(mut self, model: MotorModel) -> Self {
209 self.0.set_motor_model(model);
210 self
211 }
212
213 #[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 #[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 #[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 #[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 #[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 #[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}