rapier2d/dynamics/joint/
prismatic_joint.rs1use crate::dynamics::integration_parameters::SpringCoefficients;
2use crate::dynamics::joint::{GenericJoint, GenericJointBuilder, JointAxesMask};
3use crate::dynamics::{JointAxis, MotorModel};
4use crate::math::{Point, Real, UnitVector};
5
6use super::{JointLimits, JointMotor};
7
8#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
9#[derive(Copy, Clone, Debug, PartialEq)]
10#[repr(transparent)]
11pub struct PrismaticJoint {
26 pub data: GenericJoint,
28}
29
30impl PrismaticJoint {
31 pub fn new(axis: UnitVector<Real>) -> Self {
35 let data = GenericJointBuilder::new(JointAxesMask::LOCKED_PRISMATIC_AXES)
36 .local_axis1(axis)
37 .local_axis2(axis)
38 .build();
39 Self { data }
40 }
41
42 pub fn data(&self) -> &GenericJoint {
44 &self.data
45 }
46
47 pub fn contacts_enabled(&self) -> bool {
49 self.data.contacts_enabled
50 }
51
52 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
54 self.data.set_contacts_enabled(enabled);
55 self
56 }
57
58 #[must_use]
60 pub fn local_anchor1(&self) -> Point<Real> {
61 self.data.local_anchor1()
62 }
63
64 pub fn set_local_anchor1(&mut self, anchor1: Point<Real>) -> &mut Self {
66 self.data.set_local_anchor1(anchor1);
67 self
68 }
69
70 #[must_use]
72 pub fn local_anchor2(&self) -> Point<Real> {
73 self.data.local_anchor2()
74 }
75
76 pub fn set_local_anchor2(&mut self, anchor2: Point<Real>) -> &mut Self {
78 self.data.set_local_anchor2(anchor2);
79 self
80 }
81
82 #[must_use]
84 pub fn local_axis1(&self) -> UnitVector<Real> {
85 self.data.local_axis1()
86 }
87
88 pub fn set_local_axis1(&mut self, axis1: UnitVector<Real>) -> &mut Self {
90 self.data.set_local_axis1(axis1);
91 self
92 }
93
94 #[must_use]
96 pub fn local_axis2(&self) -> UnitVector<Real> {
97 self.data.local_axis2()
98 }
99
100 pub fn set_local_axis2(&mut self, axis2: UnitVector<Real>) -> &mut Self {
102 self.data.set_local_axis2(axis2);
103 self
104 }
105
106 #[must_use]
108 pub fn motor(&self) -> Option<&JointMotor> {
109 self.data.motor(JointAxis::LinX)
110 }
111
112 pub fn set_motor_model(&mut self, model: MotorModel) -> &mut Self {
114 self.data.set_motor_model(JointAxis::LinX, model);
115 self
116 }
117
118 pub fn set_motor_velocity(&mut self, target_vel: Real, factor: Real) -> &mut Self {
126 self.data
127 .set_motor_velocity(JointAxis::LinX, target_vel, factor);
128 self
129 }
130
131 pub fn set_motor_position(
140 &mut self,
141 target_pos: Real,
142 stiffness: Real,
143 damping: Real,
144 ) -> &mut Self {
145 self.data
146 .set_motor_position(JointAxis::LinX, target_pos, stiffness, damping);
147 self
148 }
149
150 pub fn set_motor(
152 &mut self,
153 target_pos: Real,
154 target_vel: Real,
155 stiffness: Real,
156 damping: Real,
157 ) -> &mut Self {
158 self.data
159 .set_motor(JointAxis::LinX, target_pos, target_vel, stiffness, damping);
160 self
161 }
162
163 pub fn set_motor_max_force(&mut self, max_force: Real) -> &mut Self {
165 self.data.set_motor_max_force(JointAxis::LinX, max_force);
166 self
167 }
168
169 #[must_use]
171 pub fn limits(&self) -> Option<&JointLimits<Real>> {
172 self.data.limits(JointAxis::LinX)
173 }
174
175 pub fn set_limits(&mut self, limits: [Real; 2]) -> &mut Self {
177 self.data.set_limits(JointAxis::LinX, limits);
178 self
179 }
180
181 #[must_use]
183 pub fn softness(&self) -> SpringCoefficients<Real> {
184 self.data.softness
185 }
186
187 #[must_use]
189 pub fn set_softness(&mut self, softness: SpringCoefficients<Real>) -> &mut Self {
190 self.data.softness = softness;
191 self
192 }
193}
194
195impl From<PrismaticJoint> for GenericJoint {
196 fn from(val: PrismaticJoint) -> GenericJoint {
197 val.data
198 }
199}
200
201#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
205#[derive(Copy, Clone, Debug, PartialEq)]
206pub struct PrismaticJointBuilder(pub PrismaticJoint);
207
208impl PrismaticJointBuilder {
209 pub fn new(axis: UnitVector<Real>) -> Self {
213 Self(PrismaticJoint::new(axis))
214 }
215
216 #[must_use]
218 pub fn contacts_enabled(mut self, enabled: bool) -> Self {
219 self.0.set_contacts_enabled(enabled);
220 self
221 }
222
223 #[must_use]
225 pub fn local_anchor1(mut self, anchor1: Point<Real>) -> Self {
226 self.0.set_local_anchor1(anchor1);
227 self
228 }
229
230 #[must_use]
232 pub fn local_anchor2(mut self, anchor2: Point<Real>) -> Self {
233 self.0.set_local_anchor2(anchor2);
234 self
235 }
236
237 #[must_use]
239 pub fn local_axis1(mut self, axis1: UnitVector<Real>) -> Self {
240 self.0.set_local_axis1(axis1);
241 self
242 }
243
244 #[must_use]
246 pub fn local_axis2(mut self, axis2: UnitVector<Real>) -> Self {
247 self.0.set_local_axis2(axis2);
248 self
249 }
250
251 #[must_use]
253 pub fn motor_model(mut self, model: MotorModel) -> Self {
254 self.0.set_motor_model(model);
255 self
256 }
257
258 #[must_use]
260 pub fn motor_velocity(mut self, target_vel: Real, factor: Real) -> Self {
261 self.0.set_motor_velocity(target_vel, factor);
262 self
263 }
264
265 #[must_use]
267 pub fn motor_position(mut self, target_pos: Real, stiffness: Real, damping: Real) -> Self {
268 self.0.set_motor_position(target_pos, stiffness, damping);
269 self
270 }
271
272 #[must_use]
274 pub fn set_motor(
275 mut self,
276 target_pos: Real,
277 target_vel: Real,
278 stiffness: Real,
279 damping: Real,
280 ) -> Self {
281 self.0.set_motor(target_pos, target_vel, stiffness, damping);
282 self
283 }
284
285 #[must_use]
287 pub fn motor_max_force(mut self, max_force: Real) -> Self {
288 self.0.set_motor_max_force(max_force);
289 self
290 }
291
292 #[must_use]
294 pub fn limits(mut self, limits: [Real; 2]) -> Self {
295 self.0.set_limits(limits);
296 self
297 }
298
299 #[must_use]
301 pub fn softness(mut self, softness: SpringCoefficients<Real>) -> Self {
302 self.0.data.softness = softness;
303 self
304 }
305
306 #[must_use]
308 pub fn build(self) -> PrismaticJoint {
309 self.0
310 }
311}
312
313impl From<PrismaticJointBuilder> for GenericJoint {
314 fn from(val: PrismaticJointBuilder) -> GenericJoint {
315 val.0.into()
316 }
317}