rapier3d/dynamics/joint/
prismatic_joint.rs1use crate::dynamics::joint::{GenericJoint, GenericJointBuilder, JointAxesMask};
2use crate::dynamics::{JointAxis, MotorModel};
3use crate::math::{Point, Real, UnitVector};
4
5use super::{JointLimits, JointMotor};
6
7#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
8#[derive(Copy, Clone, Debug, PartialEq)]
9#[repr(transparent)]
10pub struct PrismaticJoint {
25 pub data: GenericJoint,
27}
28
29impl PrismaticJoint {
30 pub fn new(axis: UnitVector<Real>) -> Self {
34 let data = GenericJointBuilder::new(JointAxesMask::LOCKED_PRISMATIC_AXES)
35 .local_axis1(axis)
36 .local_axis2(axis)
37 .build();
38 Self { data }
39 }
40
41 pub fn data(&self) -> &GenericJoint {
43 &self.data
44 }
45
46 pub fn contacts_enabled(&self) -> bool {
48 self.data.contacts_enabled
49 }
50
51 pub fn set_contacts_enabled(&mut self, enabled: bool) -> &mut Self {
53 self.data.set_contacts_enabled(enabled);
54 self
55 }
56
57 #[must_use]
59 pub fn local_anchor1(&self) -> Point<Real> {
60 self.data.local_anchor1()
61 }
62
63 pub fn set_local_anchor1(&mut self, anchor1: Point<Real>) -> &mut Self {
65 self.data.set_local_anchor1(anchor1);
66 self
67 }
68
69 #[must_use]
71 pub fn local_anchor2(&self) -> Point<Real> {
72 self.data.local_anchor2()
73 }
74
75 pub fn set_local_anchor2(&mut self, anchor2: Point<Real>) -> &mut Self {
77 self.data.set_local_anchor2(anchor2);
78 self
79 }
80
81 #[must_use]
83 pub fn local_axis1(&self) -> UnitVector<Real> {
84 self.data.local_axis1()
85 }
86
87 pub fn set_local_axis1(&mut self, axis1: UnitVector<Real>) -> &mut Self {
89 self.data.set_local_axis1(axis1);
90 self
91 }
92
93 #[must_use]
95 pub fn local_axis2(&self) -> UnitVector<Real> {
96 self.data.local_axis2()
97 }
98
99 pub fn set_local_axis2(&mut self, axis2: UnitVector<Real>) -> &mut Self {
101 self.data.set_local_axis2(axis2);
102 self
103 }
104
105 #[must_use]
107 pub fn motor(&self) -> Option<&JointMotor> {
108 self.data.motor(JointAxis::LinX)
109 }
110
111 pub fn set_motor_model(&mut self, model: MotorModel) -> &mut Self {
113 self.data.set_motor_model(JointAxis::LinX, model);
114 self
115 }
116
117 pub fn set_motor_velocity(&mut self, target_vel: Real, factor: Real) -> &mut Self {
125 self.data
126 .set_motor_velocity(JointAxis::LinX, target_vel, factor);
127 self
128 }
129
130 pub fn set_motor_position(
139 &mut self,
140 target_pos: Real,
141 stiffness: Real,
142 damping: Real,
143 ) -> &mut Self {
144 self.data
145 .set_motor_position(JointAxis::LinX, target_pos, stiffness, damping);
146 self
147 }
148
149 pub fn set_motor(
151 &mut self,
152 target_pos: Real,
153 target_vel: Real,
154 stiffness: Real,
155 damping: Real,
156 ) -> &mut Self {
157 self.data
158 .set_motor(JointAxis::LinX, target_pos, target_vel, stiffness, damping);
159 self
160 }
161
162 pub fn set_motor_max_force(&mut self, max_force: Real) -> &mut Self {
164 self.data.set_motor_max_force(JointAxis::LinX, max_force);
165 self
166 }
167
168 #[must_use]
170 pub fn limits(&self) -> Option<&JointLimits<Real>> {
171 self.data.limits(JointAxis::LinX)
172 }
173
174 pub fn set_limits(&mut self, limits: [Real; 2]) -> &mut Self {
176 self.data.set_limits(JointAxis::LinX, limits);
177 self
178 }
179}
180
181impl From<PrismaticJoint> for GenericJoint {
182 fn from(val: PrismaticJoint) -> GenericJoint {
183 val.data
184 }
185}
186
187#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
191#[derive(Copy, Clone, Debug, PartialEq)]
192pub struct PrismaticJointBuilder(pub PrismaticJoint);
193
194impl PrismaticJointBuilder {
195 pub fn new(axis: UnitVector<Real>) -> Self {
199 Self(PrismaticJoint::new(axis))
200 }
201
202 #[must_use]
204 pub fn contacts_enabled(mut self, enabled: bool) -> Self {
205 self.0.set_contacts_enabled(enabled);
206 self
207 }
208
209 #[must_use]
211 pub fn local_anchor1(mut self, anchor1: Point<Real>) -> Self {
212 self.0.set_local_anchor1(anchor1);
213 self
214 }
215
216 #[must_use]
218 pub fn local_anchor2(mut self, anchor2: Point<Real>) -> Self {
219 self.0.set_local_anchor2(anchor2);
220 self
221 }
222
223 #[must_use]
225 pub fn local_axis1(mut self, axis1: UnitVector<Real>) -> Self {
226 self.0.set_local_axis1(axis1);
227 self
228 }
229
230 #[must_use]
232 pub fn local_axis2(mut self, axis2: UnitVector<Real>) -> Self {
233 self.0.set_local_axis2(axis2);
234 self
235 }
236
237 #[must_use]
239 pub fn motor_model(mut self, model: MotorModel) -> Self {
240 self.0.set_motor_model(model);
241 self
242 }
243
244 #[must_use]
246 pub fn motor_velocity(mut self, target_vel: Real, factor: Real) -> Self {
247 self.0.set_motor_velocity(target_vel, factor);
248 self
249 }
250
251 #[must_use]
253 pub fn motor_position(mut self, target_pos: Real, stiffness: Real, damping: Real) -> Self {
254 self.0.set_motor_position(target_pos, stiffness, damping);
255 self
256 }
257
258 #[must_use]
260 pub fn set_motor(
261 mut self,
262 target_pos: Real,
263 target_vel: Real,
264 stiffness: Real,
265 damping: Real,
266 ) -> Self {
267 self.0.set_motor(target_pos, target_vel, stiffness, damping);
268 self
269 }
270
271 #[must_use]
273 pub fn motor_max_force(mut self, max_force: Real) -> Self {
274 self.0.set_motor_max_force(max_force);
275 self
276 }
277
278 #[must_use]
280 pub fn limits(mut self, limits: [Real; 2]) -> Self {
281 self.0.set_limits(limits);
282 self
283 }
284
285 #[must_use]
287 pub fn build(self) -> PrismaticJoint {
288 self.0
289 }
290}
291
292impl From<PrismaticJointBuilder> for GenericJoint {
293 fn from(val: PrismaticJointBuilder) -> GenericJoint {
294 val.0.into()
295 }
296}