bevy_rapier2d/dynamics/
pin_slot_joint.rs1use crate::dynamics::{GenericJoint, GenericJointBuilder};
2use crate::math::{Real, Vect};
3use rapier::dynamics::{JointAxesMask, JointAxis, JointLimits, JointMotor, MotorModel};
4
5use super::{SpringCoefficients, TypedJoint};
6
7#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
8#[derive(Copy, Clone, Debug, PartialEq)]
9#[repr(transparent)]
10pub struct PinSlotJoint {
15 pub data: GenericJoint,
17}
18
19impl PinSlotJoint {
20 pub fn new(axis: Vect) -> Self {
24 let data = GenericJointBuilder::new(JointAxesMask::LOCKED_PIN_SLOT_AXES)
25 .local_axis1(axis)
26 .local_axis2(axis)
27 .build();
28 Self { data }
29 }
30
31 pub fn contacts_enabled(&self) -> bool {
33 self.data.contacts_enabled()
34 }
35
36 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
38 self.data.set_contacts_enabled(enabled);
39 self
40 }
41
42 #[must_use]
44 pub fn softness(&self) -> SpringCoefficients<Real> {
45 self.data.softness()
46 }
47
48 pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
50 self.data.set_softness(softness);
51 self
52 }
53
54 #[must_use]
56 pub fn local_anchor1(&self) -> Vect {
57 self.data.local_anchor1()
58 }
59
60 pub fn set_local_anchor1(&mut self, anchor1: Vect) -> &mut Self {
62 self.data.set_local_anchor1(anchor1);
63 self
64 }
65
66 #[must_use]
68 pub fn local_anchor2(&self) -> Vect {
69 self.data.local_anchor2()
70 }
71
72 pub fn set_local_anchor2(&mut self, anchor2: Vect) -> &mut Self {
74 self.data.set_local_anchor2(anchor2);
75 self
76 }
77
78 #[must_use]
80 pub fn local_axis1(&self) -> Vect {
81 self.data.local_axis1()
82 }
83
84 pub fn set_local_axis1(&mut self, axis1: Vect) -> &mut Self {
86 self.data.set_local_axis1(axis1);
87 self
88 }
89
90 #[must_use]
92 pub fn local_axis2(&self) -> Vect {
93 self.data.local_axis2()
94 }
95
96 pub fn set_local_axis2(&mut self, axis2: Vect) -> &mut Self {
98 self.data.set_local_axis2(axis2);
99 self
100 }
101
102 #[must_use]
104 pub fn motor(&self) -> Option<&JointMotor> {
105 self.data.motor(JointAxis::LinX)
106 }
107
108 pub fn set_motor_model(&mut self, model: MotorModel) -> &mut Self {
110 self.data.set_motor_model(JointAxis::LinX, model);
111 self
112 }
113
114 pub fn set_motor_velocity(&mut self, target_vel: Real, factor: Real) -> &mut Self {
116 self.data
117 .set_motor_velocity(JointAxis::LinX, target_vel, factor);
118 self
119 }
120
121 pub fn set_motor_position(
123 &mut self,
124 target_pos: Real,
125 stiffness: Real,
126 damping: Real,
127 ) -> &mut Self {
128 self.data
129 .set_motor_position(JointAxis::LinX, target_pos, stiffness, damping);
130 self
131 }
132
133 pub fn set_motor(
135 &mut self,
136 target_pos: Real,
137 target_vel: Real,
138 stiffness: Real,
139 damping: Real,
140 ) -> &mut Self {
141 self.data
142 .set_motor(JointAxis::LinX, target_pos, target_vel, stiffness, damping);
143 self
144 }
145
146 pub fn set_motor_max_force(&mut self, max_force: Real) -> &mut Self {
148 self.data.set_motor_max_force(JointAxis::LinX, max_force);
149 self
150 }
151
152 #[must_use]
154 pub fn limits(&self) -> Option<&JointLimits<Real>> {
155 self.data.limits(JointAxis::LinX)
156 }
157
158 pub fn set_limits(&mut self, limits: [Real; 2]) -> &mut Self {
160 self.data.set_limits(JointAxis::LinX, limits);
161 self
162 }
163}
164
165impl From<PinSlotJoint> for GenericJoint {
166 fn from(joint: PinSlotJoint) -> GenericJoint {
167 joint.data
168 }
169}
170
171#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
176#[derive(Copy, Clone, Debug, PartialEq)]
177pub struct PinSlotJointBuilder(PinSlotJoint);
178
179impl PinSlotJointBuilder {
180 pub fn new(axis: Vect) -> Self {
184 Self(PinSlotJoint::new(axis))
185 }
186
187 #[must_use]
189 pub fn local_anchor1(mut self, anchor1: Vect) -> Self {
190 self.0.set_local_anchor1(anchor1);
191 self
192 }
193
194 #[must_use]
196 pub fn local_anchor2(mut self, anchor2: Vect) -> Self {
197 self.0.set_local_anchor2(anchor2);
198 self
199 }
200
201 #[must_use]
203 pub fn local_axis1(mut self, axis1: Vect) -> Self {
204 self.0.set_local_axis1(axis1);
205 self
206 }
207
208 #[must_use]
210 pub fn local_axis2(mut self, axis2: Vect) -> Self {
211 self.0.set_local_axis2(axis2);
212 self
213 }
214
215 #[must_use]
217 pub fn motor_model(mut self, model: MotorModel) -> Self {
218 self.0.set_motor_model(model);
219 self
220 }
221
222 #[must_use]
224 pub fn motor_velocity(mut self, target_vel: Real, factor: Real) -> Self {
225 self.0.set_motor_velocity(target_vel, factor);
226 self
227 }
228
229 #[must_use]
231 pub fn motor_position(mut self, target_pos: Real, stiffness: Real, damping: Real) -> Self {
232 self.0.set_motor_position(target_pos, stiffness, damping);
233 self
234 }
235
236 #[must_use]
238 pub fn set_motor(
239 mut self,
240 target_pos: Real,
241 target_vel: Real,
242 stiffness: Real,
243 damping: Real,
244 ) -> Self {
245 self.0.set_motor(target_pos, target_vel, stiffness, damping);
246 self
247 }
248
249 #[must_use]
251 pub fn motor_max_force(mut self, max_force: Real) -> Self {
252 self.0.set_motor_max_force(max_force);
253 self
254 }
255
256 #[must_use]
258 pub fn limits(mut self, limits: [Real; 2]) -> Self {
259 self.0.set_limits(limits);
260 self
261 }
262
263 #[must_use]
265 pub fn contacts_enabled(mut self, enabled: bool) -> Self {
266 self.0.set_contacts_enabled(enabled);
267 self
268 }
269
270 #[must_use]
272 pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
273 self.0.set_softness(softness);
274 self
275 }
276
277 #[must_use]
279 pub fn build(self) -> PinSlotJoint {
280 self.0
281 }
282}
283
284impl From<PinSlotJointBuilder> for TypedJoint {
285 fn from(joint: PinSlotJointBuilder) -> TypedJoint {
286 joint.0.into()
287 }
288}
289
290impl From<PinSlotJoint> for TypedJoint {
291 fn from(joint: PinSlotJoint) -> TypedJoint {
292 TypedJoint::PinSlotJoint(joint)
293 }
294}