1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use crate::{
dynamics::joints::{EntityConstraint, JointSystems, motor::AngularMotor},
prelude::*,
};
use bevy::{
ecs::{
entity::{EntityMapper, MapEntities},
reflect::ReflectMapEntities,
},
prelude::*,
};
#[cfg_attr(
feature = "2d",
doc = "A revolute [joint](dynamics::joints) or hinge prevents any relative movement between two bodies, except for rotation about a pivot point defined by the joint anchor."
)]
#[cfg_attr(
feature = "3d",
doc = "A revolute [joint](dynamics::joints) or hinge prevents any relative movement between two bodies, except for rotation about the [`hinge_axis`](Self::hinge_axis) at a pivot point defined by the joint anchor."
)]
///
/// This can be useful for things like wheels, fans, doors, and other rotating mechanisms.
///
#[cfg_attr(
feature = "2d",
doc = "Each revolute joint is defined by a [`JointFrame`] on each body,"
)]
#[cfg_attr(
feature = "3d",
doc = "Each revolute joint is defined by a [`JointFrame`] on each body, a [`hinge_axis`](Self::hinge_axis) about which the bodies can rotate,"
)]
/// and an optional [`AngleLimit`] that defines the extents of the allowed rotation. The joint aims to keep the anchor point of each frame aligned,
#[cfg_attr(feature = "2d", doc = "while allowing rotation at the anchor point.")]
#[cfg_attr(
feature = "3d",
doc = "while allowing rotation about the hinge axis at the anchor point."
)]
///
#[doc = include_str!("./images/revolute_joint.svg")]
///
/// The joint can also include an [`AngularMotor`] for driving the rotation about the pivot point.
/// Use this to create wheels, fans, servos, or other rotating mechanisms.
#[derive(Component, Clone, Debug, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Component, Debug, MapEntities, PartialEq)]
#[doc(alias = "HingeJoint")]
pub struct RevoluteJoint {
/// The first body constrained by the joint.
pub body1: Entity,
/// The second body constrained by the joint.
pub body2: Entity,
/// The reference frame of the first body, defining the joint anchor and basis
/// relative to the body transform.
pub frame1: JointFrame,
/// The reference frame of the second body, defining the joint anchor and basis
/// relative to the body transform.
pub frame2: JointFrame,
/// The local axis about which the bodies can rotate relative to each other.
///
/// By default, this is the z-axis.
#[cfg(feature = "3d")]
pub hinge_axis: Vector,
/// The extents of the allowed relative rotation of the bodies.
pub angle_limit: Option<AngleLimit>,
/// The compliance of the point-to-point constraint (inverse of stiffness, m / N).
pub point_compliance: Scalar,
/// The compliance used for aligning the bodies along the [`hinge_axis`](Self::hinge_axis) (inverse of stiffness, N * m / rad).
#[cfg(feature = "3d")]
pub align_compliance: Scalar,
/// The compliance of the angle limit (inverse of stiffness, N * m / rad).
pub limit_compliance: Scalar,
/// A motor for driving the joint.
pub motor: AngularMotor,
}
impl EntityConstraint<2> for RevoluteJoint {
fn entities(&self) -> [Entity; 2] {
[self.body1, self.body2]
}
}
impl RevoluteJoint {
/// The default [`hinge_axis`](Self::hinge_axis) for a revolute joint.
#[cfg(feature = "3d")]
pub const DEFAULT_HINGE_AXIS: Vector = Vector::Z;
/// Creates a new [`RevoluteJoint`] between two entities.
#[inline]
pub const fn new(body1: Entity, body2: Entity) -> Self {
Self {
body1,
body2,
frame1: JointFrame::IDENTITY,
frame2: JointFrame::IDENTITY,
#[cfg(feature = "3d")]
hinge_axis: Self::DEFAULT_HINGE_AXIS,
angle_limit: None,
point_compliance: 0.0,
#[cfg(feature = "3d")]
align_compliance: 0.0,
limit_compliance: 0.0,
motor: AngularMotor::new_disabled(MotorModel::DEFAULT),
}
}
/// Sets the [`hinge_axis`](Self::hinge_axis) about which the bodies can rotate relative to each other.
///
/// The axis should be a unit vector. By default, this is the z-axis.
#[inline]
#[cfg(feature = "3d")]
pub const fn with_hinge_axis(mut self, axis: Vector) -> Self {
self.hinge_axis = axis;
self
}
/// Sets the [`hinge_axis`](Self::hinge_axis) about which the bodies can rotate relative to each other.
///
/// The axis should be a unit vector. By default, this is the x-axis.
///
/// This method is deprecated in favor of [`with_hinge_axis`](Self::with_hinge_axis).
#[inline]
#[deprecated(since = "0.4.0", note = "Use `with_hinge_axis` instead.")]
#[cfg(feature = "3d")]
pub const fn with_aligned_axis(self, axis: Vector) -> Self {
self.with_hinge_axis(axis)
}
/// Sets the local [`JointFrame`] of the first body, configuring both the [`JointAnchor`] and [`JointBasis`].
#[inline]
pub fn with_local_frame1(mut self, frame: impl Into<Isometry>) -> Self {
self.frame1 = JointFrame::local(frame);
self
}
/// Sets the local [`JointFrame`] of the second body, configuring both the [`JointAnchor`] and [`JointBasis`].
#[inline]
pub fn with_local_frame2(mut self, frame: impl Into<Isometry>) -> Self {
self.frame2 = JointFrame::local(frame);
self
}
/// Sets the global anchor point on both bodies.
///
/// This configures the [`JointAnchor`] of each [`JointFrame`].
#[inline]
pub const fn with_anchor(mut self, anchor: Vector) -> Self {
self.frame1.anchor = JointAnchor::FromGlobal(anchor);
self.frame2.anchor = JointAnchor::FromGlobal(anchor);
self
}
/// Sets the local anchor point on the first body.
///
/// This configures the [`JointAnchor`] of the first [`JointFrame`].
#[inline]
pub const fn with_local_anchor1(mut self, anchor: Vector) -> Self {
self.frame1.anchor = JointAnchor::Local(anchor);
self
}
/// Sets the local anchor point on the second body.
///
/// This configures the [`JointAnchor`] of the second [`JointFrame`].
#[inline]
pub const fn with_local_anchor2(mut self, anchor: Vector) -> Self {
self.frame2.anchor = JointAnchor::Local(anchor);
self
}
/// Sets the global basis for both bodies.
///
/// This configures the [`JointBasis`] of each [`JointFrame`].
#[inline]
pub fn with_basis(mut self, basis: impl Into<Rot>) -> Self {
let basis = basis.into();
self.frame1.basis = JointBasis::FromGlobal(basis);
self.frame2.basis = JointBasis::FromGlobal(basis);
self
}
/// Sets the local basis for the first body.
///
/// This configures the [`JointBasis`] of the first [`JointFrame`].
#[inline]
pub fn with_local_basis1(mut self, basis: impl Into<Rot>) -> Self {
self.frame1.basis = JointBasis::Local(basis.into());
self
}
/// Sets the local basis for the second body.
///
/// This configures the [`JointBasis`] of the second [`JointFrame`].
#[inline]
pub fn with_local_basis2(mut self, basis: impl Into<Rot>) -> Self {
self.frame2.basis = JointBasis::Local(basis.into());
self
}
/// Returns the local [`JointFrame`] of the first body.
///
/// If the [`JointAnchor`] is set to [`FromGlobal`](JointAnchor::FromGlobal),
/// and the local anchor has not yet been computed, or the [`JointBasis`] is set to
/// [`FromGlobal`](JointBasis::FromGlobal), and the local basis has not yet
/// been computed, this will return `None`.
#[inline]
pub fn local_frame1(&self) -> Option<Isometry> {
self.frame1.get_local_isometry()
}
/// Returns the local [`JointFrame`] of the second body.
///
/// If the [`JointAnchor`] is set to [`FromGlobal`](JointAnchor::FromGlobal),
/// and the local anchor has not yet been computed, or the [`JointBasis`] is set to
/// [`FromGlobal`](JointBasis::FromGlobal), and the local basis has not yet
/// been computed, this will return `None`.
#[inline]
pub fn local_frame2(&self) -> Option<Isometry> {
self.frame2.get_local_isometry()
}
/// Returns the local anchor point on the first body.
///
/// If the [`JointAnchor`] is set to [`FromGlobal`](JointAnchor::FromGlobal),
/// and the local anchor has not yet been computed, this will return `None`.
#[inline]
pub const fn local_anchor1(&self) -> Option<Vector> {
match self.frame1.anchor {
JointAnchor::Local(anchor) => Some(anchor),
_ => None,
}
}
/// Returns the local anchor point on the second body.
///
/// If the [`JointAnchor`] is set to [`FromGlobal`](JointAnchor::FromGlobal),
/// and the local anchor has not yet been computed, this will return `None`.
#[inline]
pub const fn local_anchor2(&self) -> Option<Vector> {
match self.frame2.anchor {
JointAnchor::Local(anchor) => Some(anchor),
_ => None,
}
}
/// Returns the local basis of the first body.
///
/// If the [`JointBasis`] is set to [`FromGlobal`](JointBasis::FromGlobal),
/// and the local basis has not yet been computed, this will return `None`.
#[inline]
pub fn local_basis1(&self) -> Option<Rot> {
match self.frame1.basis {
JointBasis::Local(basis) => Some(basis),
_ => None,
}
}
/// Returns the local basis of the second body.
///
/// If the [`JointBasis`] is set to [`FromGlobal`](JointBasis::FromGlobal),
/// and the local basis has not yet been computed, this will return `None`.
#[inline]
pub fn local_basis2(&self) -> Option<Rot> {
match self.frame2.basis {
JointBasis::Local(basis) => Some(basis),
_ => None,
}
}
/// Returns the local hinge axis of the first body.
///
/// This is equivalent to rotating the [`hinge_axis`](Self::hinge_axis)
/// by the local basis of [`frame1`](Self::frame1).
///
/// If the [`JointBasis`] is set to [`FromGlobal`](JointBasis::FromGlobal),
/// and the local basis has not yet been computed, this will return `None`.
#[inline]
#[cfg(feature = "3d")]
pub fn local_hinge_axis1(&self) -> Option<Vector> {
match self.frame1.basis {
JointBasis::Local(basis) => Some(basis * self.hinge_axis),
_ => None,
}
}
/// Returns the local hinge axis of the second body.
///
/// This is equivalent to rotating the [`hinge_axis`](Self::hinge_axis)
/// by the local basis of [`frame2`](Self::frame2).
///
/// If the [`JointBasis`] is set to [`FromGlobal`](JointBasis::FromGlobal),
/// and the local basis has not yet been computed, this will return `None`.
#[inline]
#[cfg(feature = "3d")]
pub fn local_hinge_axis2(&self) -> Option<Vector> {
match self.frame2.basis {
JointBasis::Local(basis) => Some(basis * self.hinge_axis),
_ => None,
}
}
/// Sets the limits of the allowed relative rotation.
#[inline]
pub const fn with_angle_limits(mut self, min: Scalar, max: Scalar) -> Self {
self.angle_limit = Some(AngleLimit::new(min, max));
self
}
/// Sets the joint's compliance (inverse of stiffness, m / N).
#[inline]
#[deprecated(
since = "0.4.0",
note = "Use `with_point_compliance`, `with_align_compliance`, and `with_limit_compliance` instead."
)]
pub const fn with_compliance(mut self, compliance: Scalar) -> Self {
self.point_compliance = compliance;
#[cfg(feature = "3d")]
{
self.align_compliance = compliance;
}
self.limit_compliance = compliance;
self
}
/// Sets the compliance of the point-to-point constraint (inverse of stiffness, m / N).
#[inline]
pub const fn with_point_compliance(mut self, compliance: Scalar) -> Self {
self.point_compliance = compliance;
self
}
/// Sets the compliance of the axis alignment constraint (inverse of stiffness, N * m / rad).
#[inline]
#[cfg(feature = "3d")]
pub const fn with_align_compliance(mut self, compliance: Scalar) -> Self {
self.align_compliance = compliance;
self
}
/// Sets the compliance of the angle limit (inverse of stiffness, N * m / rad).
#[inline]
pub const fn with_limit_compliance(mut self, compliance: Scalar) -> Self {
self.limit_compliance = compliance;
self
}
/// Sets the motor for the joint.
#[inline]
pub const fn with_motor(mut self, motor: AngularMotor) -> Self {
self.motor = motor;
self
}
}
impl MapEntities for RevoluteJoint {
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
self.body1 = entity_mapper.get_mapped(self.body1);
self.body2 = entity_mapper.get_mapped(self.body2);
}
}
pub(super) fn plugin(app: &mut App) {
app.add_systems(
PhysicsSchedule,
update_local_frames.in_set(JointSystems::PrepareLocalFrames),
);
}
fn update_local_frames(
mut joints: Query<&mut RevoluteJoint, Changed<RevoluteJoint>>,
bodies: Query<(&Position, &Rotation)>,
) {
for mut joint in &mut joints {
if matches!(joint.frame1.anchor, JointAnchor::Local(_))
&& matches!(joint.frame2.anchor, JointAnchor::Local(_))
&& matches!(joint.frame1.basis, JointBasis::Local(_))
&& matches!(joint.frame2.basis, JointBasis::Local(_))
{
continue;
}
let Ok([(pos1, rot1), (pos2, rot2)]) = bodies.get_many(joint.entities()) else {
continue;
};
let [frame1, frame2] =
JointFrame::compute_local(joint.frame1, joint.frame2, pos1.0, pos2.0, rot1, rot2);
joint.frame1 = frame1;
joint.frame2 = frame2;
}
}
#[cfg(feature = "debug-plugin")]
impl DebugRenderConstraint<2> for RevoluteJoint {
type Context = ();
fn debug_render(
&self,
positions: [Vector; 2],
rotations: [Rotation; 2],
_context: &mut Self::Context,
gizmos: &mut Gizmos<PhysicsGizmos>,
config: &PhysicsGizmos,
) {
let [pos1, pos2] = positions;
let [rot1, rot2] = rotations;
let Some(local_anchor1) = self.local_anchor1() else {
return;
};
let Some(local_anchor2) = self.local_anchor2() else {
return;
};
let anchor1 = pos1 + rot1 * local_anchor1;
let anchor2 = pos2 + rot2 * local_anchor2;
if let Some(anchor_color) = config.joint_anchor_color {
gizmos.draw_line(pos1, anchor1, anchor_color);
gizmos.draw_line(pos2, anchor2, anchor_color);
}
if let Some(color) = config.joint_separation_color {
gizmos.draw_line(anchor1, anchor2, color);
}
}
}