rapier2d/dynamics/joint/
pin_slot_joint.rs1#[cfg(feature = "dim2")]
2use crate::dynamics::joint::{GenericJointBuilder, JointAxesMask};
3
4use crate::dynamics::integration_parameters::SpringCoefficients;
5use crate::dynamics::joint::GenericJoint;
6use crate::dynamics::{JointAxis, MotorModel};
7use crate::math::{Point, Real, UnitVector};
8
9use super::{JointLimits, JointMotor};
10
11#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
12#[derive(Copy, Clone, Debug, PartialEq)]
13#[repr(transparent)]
14pub struct PinSlotJoint {
17 pub data: GenericJoint,
19}
20
21impl PinSlotJoint {
22 #[cfg(feature = "dim2")]
26 pub fn new(axis: UnitVector<Real>) -> Self {
27 let data = GenericJointBuilder::new(JointAxesMask::LOCKED_PIN_SLOT_AXES)
28 .local_axis1(axis)
29 .local_axis2(axis)
30 .build();
31 Self { data }
32 }
33
34 pub fn data(&self) -> &GenericJoint {
36 &self.data
37 }
38
39 pub fn contacts_enabled(&self) -> bool {
41 self.data.contacts_enabled
42 }
43
44 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
46 self.data.set_contacts_enabled(enabled);
47 self
48 }
49
50 #[must_use]
52 pub fn local_anchor1(&self) -> Point<Real> {
53 self.data.local_anchor1()
54 }
55
56 pub fn set_local_anchor1(&mut self, anchor1: Point<Real>) -> &mut Self {
58 self.data.set_local_anchor1(anchor1);
59 self
60 }
61
62 #[must_use]
64 pub fn local_anchor2(&self) -> Point<Real> {
65 self.data.local_anchor2()
66 }
67
68 pub fn set_local_anchor2(&mut self, anchor2: Point<Real>) -> &mut Self {
70 self.data.set_local_anchor2(anchor2);
71 self
72 }
73
74 #[must_use]
76 pub fn local_axis1(&self) -> UnitVector<Real> {
77 self.data.local_axis1()
78 }
79
80 pub fn set_local_axis1(&mut self, axis1: UnitVector<Real>) -> &mut Self {
82 self.data.set_local_axis1(axis1);
83 self
84 }
85
86 #[must_use]
88 pub fn local_axis2(&self) -> UnitVector<Real> {
89 self.data.local_axis2()
90 }
91
92 pub fn set_local_axis2(&mut self, axis2: UnitVector<Real>) -> &mut Self {
94 self.data.set_local_axis2(axis2);
95 self
96 }
97
98 #[must_use]
100 pub fn motor(&self) -> Option<&JointMotor> {
101 self.data.motor(JointAxis::LinX)
102 }
103
104 pub fn set_motor_model(&mut self, model: MotorModel) -> &mut Self {
106 self.data.set_motor_model(JointAxis::LinX, model);
107 self
108 }
109
110 pub fn set_motor_velocity(&mut self, target_vel: Real, factor: Real) -> &mut Self {
112 self.data
113 .set_motor_velocity(JointAxis::LinX, target_vel, factor);
114 self
115 }
116
117 pub fn set_motor_position(
119 &mut self,
120 target_pos: Real,
121 stiffness: Real,
122 damping: Real,
123 ) -> &mut Self {
124 self.data
125 .set_motor_position(JointAxis::LinX, target_pos, stiffness, damping);
126 self
127 }
128
129 pub fn set_motor(
131 &mut self,
132 target_pos: Real,
133 target_vel: Real,
134 stiffness: Real,
135 damping: Real,
136 ) -> &mut Self {
137 self.data
138 .set_motor(JointAxis::LinX, target_pos, target_vel, stiffness, damping);
139 self
140 }
141
142 pub fn set_motor_max_force(&mut self, max_force: Real) -> &mut Self {
144 self.data.set_motor_max_force(JointAxis::LinX, max_force);
145 self
146 }
147
148 #[must_use]
150 pub fn limits(&self) -> Option<&JointLimits<Real>> {
151 self.data.limits(JointAxis::LinX)
152 }
153
154 pub fn set_limits(&mut self, limits: [Real; 2]) -> &mut Self {
156 self.data.set_limits(JointAxis::LinX, limits);
157 self
158 }
159
160 #[must_use]
162 pub fn softness(&self) -> SpringCoefficients<Real> {
163 self.data.softness
164 }
165
166 #[must_use]
168 pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
169 self.data.softness = softness;
170 self
171 }
172}
173
174impl From<PinSlotJoint> for GenericJoint {
175 fn from(val: PinSlotJoint) -> GenericJoint {
176 val.data
177 }
178}
179
180#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
184#[derive(Copy, Clone, Debug, PartialEq)]
185pub struct PinSlotJointBuilder(pub PinSlotJoint);
186
187impl PinSlotJointBuilder {
188 #[cfg(feature = "dim2")]
192 pub fn new(axis: UnitVector<Real>) -> Self {
193 Self(PinSlotJoint::new(axis))
194 }
195
196 #[must_use]
198 pub fn contacts_enabled(mut self, enabled: bool) -> Self {
199 self.0.set_contacts_enabled(enabled);
200 self
201 }
202
203 #[must_use]
205 pub fn local_anchor1(mut self, anchor1: Point<Real>) -> Self {
206 self.0.set_local_anchor1(anchor1);
207 self
208 }
209
210 #[must_use]
212 pub fn local_anchor2(mut self, anchor2: Point<Real>) -> Self {
213 self.0.set_local_anchor2(anchor2);
214 self
215 }
216
217 #[must_use]
219 pub fn local_axis1(mut self, axis1: UnitVector<Real>) -> Self {
220 self.0.set_local_axis1(axis1);
221 self
222 }
223
224 #[must_use]
226 pub fn local_axis2(mut self, axis2: UnitVector<Real>) -> Self {
227 self.0.set_local_axis2(axis2);
228 self
229 }
230
231 #[must_use]
233 pub fn motor_model(mut self, model: MotorModel) -> Self {
234 self.0.set_motor_model(model);
235 self
236 }
237
238 #[must_use]
240 pub fn motor_velocity(mut self, target_vel: Real, factor: Real) -> Self {
241 self.0.set_motor_velocity(target_vel, factor);
242 self
243 }
244
245 #[must_use]
247 pub fn motor_position(mut self, target_pos: Real, stiffness: Real, damping: Real) -> Self {
248 self.0.set_motor_position(target_pos, stiffness, damping);
249 self
250 }
251
252 #[must_use]
254 pub fn set_motor(
255 mut self,
256 target_pos: Real,
257 target_vel: Real,
258 stiffness: Real,
259 damping: Real,
260 ) -> Self {
261 self.0.set_motor(target_pos, target_vel, stiffness, damping);
262 self
263 }
264
265 #[must_use]
267 pub fn motor_max_force(mut self, max_force: Real) -> Self {
268 self.0.set_motor_max_force(max_force);
269 self
270 }
271
272 #[must_use]
274 pub fn limits(mut self, limits: [Real; 2]) -> Self {
275 self.0.set_limits(limits);
276 self
277 }
278
279 #[must_use]
281 pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
282 self.0.data.softness = softness;
283 self
284 }
285
286 #[must_use]
288 pub fn build(self) -> PinSlotJoint {
289 self.0
290 }
291}
292
293impl From<PinSlotJointBuilder> for GenericJoint {
294 fn from(val: PinSlotJointBuilder) -> GenericJoint {
295 val.0.into()
296 }
297}