Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qd6joint.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qd6joint_p.h"
5
6#include "physxnode/qphysxactorbody_p.h"
9
10#include <extensions/PxD6Joint.h>
11#include <PxPhysics.h>
12
14
15
16/*!
17 \qmltype D6Joint
18 \inqmlmodule QtQuick3D.Physics
19 \since 6.13
20 \inherits PhysicsJoint
21 \brief A configurable joint with up to 6 degrees of freedom (3 linear, 3 angular).
22
23 The D6Joint allows custom configuration for each axis of movement and rotation.
24 Each degree of freedom can be set to \c Locked, \c Limited, or \c Free using the \l Motion enum.
25 Soft limits with spring stiffness and damping can be configured for linear and angular constraints.
26
27 \sa {DistanceJoint}
28 \sa {FixedJoint}
29 \sa {PrismaticJoint}
30 \sa {RevoluteJoint}
31 \sa {SphericalJoint}
32*/
33
34/*!
35 \qmlenum D6Joint::Motion
36 \since 6.13
37
38 This enum specifies how the joint constrains one degree of freedom, that is, the movement
39 along one axis or the rotation around it.
40
41 \value Locked
42 No movement is allowed.
43 \value Limited
44 Movement is allowed only within the limits set by the corresponding limit properties.
45 \value Free
46 Movement is unrestricted.
47
48 \sa xMotion, yMotion, zMotion, twistMotion, swingMotionY, swingMotionZ
49*/
50
51/*!
52 \qmlproperty Motion D6Joint::xMotion
53 \qmlenumeratorsfrom D6Joint::Motion
54 \since 6.13
55 \default D6Joint.Locked
56
57 This property holds the motion constraint along the X axis.
58 When set to \c D6Joint.Limited, the movement is limited by \l linearLimitXLower
59 and \l linearLimitXUpper.
60
61 \sa Motion
62*/
63
64/*!
65 \qmlproperty Motion D6Joint::yMotion
66 \qmlenumeratorsfrom D6Joint::Motion
67 \since 6.13
68 \default D6Joint.Locked
69
70 This property holds the motion constraint along the Y axis.
71 When set to \c D6Joint.Limited, the movement is limited by \l linearLimitYLower
72 and \l linearLimitYUpper.
73
74 \sa Motion
75*/
76
77/*!
78 \qmlproperty Motion D6Joint::zMotion
79 \qmlenumeratorsfrom D6Joint::Motion
80 \since 6.13
81 \default D6Joint.Locked
82
83 This property holds the motion constraint along the Z axis.
84 When set to \c D6Joint.Limited, the movement is limited by \l linearLimitZLower
85 and \l linearLimitZUpper.
86
87 \sa Motion
88*/
89
90/*!
91 \qmlproperty Motion D6Joint::twistMotion
92 \qmlenumeratorsfrom D6Joint::Motion
93 \since 6.13
94 \default D6Joint.Locked
95
96 This property holds the rotational constraint around the X axis (twist).
97 When set to \c D6Joint.Limited, the rotation is limited by \l twistLimitLower
98 and \l twistLimitUpper.
99
100 \sa Motion
101*/
102
103/*!
104 \qmlproperty Motion D6Joint::swingMotionY
105 \qmlenumeratorsfrom D6Joint::Motion
106 \since 6.13
107 \default D6Joint.Locked
108
109 This property holds the rotational constraint around the Y axis (swing Y).
110 When set to \c D6Joint.Limited, the rotation is limited by \l swingLimitAngleY.
111
112 \sa Motion
113*/
114
115/*!
116 \qmlproperty Motion D6Joint::swingMotionZ
117 \qmlenumeratorsfrom D6Joint::Motion
118 \since 6.13
119 \default D6Joint.Locked
120
121 This property holds the rotational constraint around the Z axis (swing Z).
122 When set to \c D6Joint.Limited, the rotation is limited by \l swingLimitAngleZ.
123
124 \sa Motion
125*/
126
127/*!
128 \qmlproperty real D6Joint::linearLimitXLower
129 \since 6.13
130 \default -1.0
131
132 This property holds the lower limit of movement along the X axis.
133 The value must be less than or equal to \l linearLimitXUpper.
134
135 Range: \c{(-\inf, \inf)}
136*/
137
138/*!
139 \qmlproperty real D6Joint::linearLimitXUpper
140 \since 6.13
141 \default 1.0
142
143 This property holds the upper limit of movement along the X axis.
144 The value must be greater than or equal to \l linearLimitXLower.
145
146 Range: \c{(-\inf, \inf)}
147*/
148
149/*!
150 \qmlproperty real D6Joint::linearLimitYLower
151 \since 6.13
152 \default -1.0
153
154 This property holds the lower limit of movement along the Y axis.
155 The value must be less than or equal to \l linearLimitYUpper.
156
157 Range: \c{(-\inf, \inf)}
158*/
159
160/*!
161 \qmlproperty real D6Joint::linearLimitYUpper
162 \since 6.13
163 \default 1.0
164
165 This property holds the upper limit of movement along the Y axis.
166 The value must be greater than or equal to \l linearLimitYLower.
167
168 Range: \c{(-\inf, \inf)}
169*/
170
171/*!
172 \qmlproperty real D6Joint::linearLimitZLower
173 \since 6.13
174 \default -1.0
175
176 This property holds the lower limit of movement along the Z axis.
177 The value must be less than or equal to \l linearLimitZUpper.
178
179 Range: \c{(-\inf, \inf)}
180*/
181
182/*!
183 \qmlproperty real D6Joint::linearLimitZUpper
184 \since 6.13
185 \default 1.0
186
187 This property holds the upper limit of movement along the Z axis.
188 The value must be greater than or equal to \l linearLimitZLower.
189
190 Range: \c{(-\inf, \inf)}
191*/
192
193/*!
194 \qmlproperty real D6Joint::twistLimitLower
195 \since 6.13
196 \default -pi/4
197
198 This property holds the lower angular limit (in radians) around the X axis.
199 The value must be less than or equal to \l twistLimitUpper.
200
201 Range: \c{(-2pi, 2pi)}
202*/
203
204/*!
205 \qmlproperty real D6Joint::twistLimitUpper
206 \since 6.13
207 \default pi/4
208
209 This property holds the upper angular limit (in radians) around the X axis.
210 The value must be greater than or equal to \l twistLimitLower.
211
212 Range: \c{(-2pi, 2pi)}
213*/
214
215/*!
216 \qmlproperty real D6Joint::swingLimitAngleY
217 \since 6.13
218 \default pi/4
219
220 This property holds the maximum swing angle limit (in radians) around the Y axis.
221
222 Range: \c{(0, pi)}
223*/
224
225/*!
226 \qmlproperty real D6Joint::swingLimitAngleZ
227 \since 6.13
228 \default pi/4
229
230 This property holds the maximum swing angle limit (in radians) around the Z axis.
231
232 Range: \c{(0, pi)}
233*/
234
235/*!
236 \qmlproperty real D6Joint::linearStiffness
237 \since 6.13
238 \default 0.0
239
240 This property holds the spring stiffness for linear motion constraints.
241 When set to a value greater than 0, linear limits act as soft constraints (springs).
242
243 Range: \c{[0, \inf)}
244*/
245
246/*!
247 \qmlproperty real D6Joint::linearDamping
248 \since 6.13
249 \default 0.0
250
251 This property holds the spring damping for linear motion constraints.
252
253 Range: \c{[0, \inf)}
254*/
255
256/*!
257 \qmlproperty real D6Joint::angularStiffness
258 \since 6.13
259 \default 0.0
260
261 This property holds the spring stiffness for angular motion constraints.
262 When set to a value greater than 0, angular limits act as soft constraints (springs).
263
264 Range: \c{[0, \inf)}
265*/
266
267/*!
268 \qmlproperty real D6Joint::angularDamping
269 \since 6.13
270 \default 0.0
271
272 This property holds the spring damping for angular motion constraints.
273
274 Range: \c{[0, \inf)}
275*/
276
277namespace {
278physx::PxD6Motion::Enum toPxEnum(QD6Joint::Motion val)
279{
280 switch (val) {
281 case QD6Joint::Motion::Locked: return physx::PxD6Motion::eLOCKED;
282 case QD6Joint::Motion::Limited: return physx::PxD6Motion::eLIMITED;
283 case QD6Joint::Motion::Free: return physx::PxD6Motion::eFREE;
284 }
285
286 Q_UNREACHABLE_RETURN(physx::PxD6Motion::eLOCKED);
287}
288}
289
290QD6Joint::QD6Joint() = default;
291
292QD6Joint::Motion QD6Joint::xMotion() const
293{
294 return m_xMotion;
295}
296
297void QD6Joint::setXMotion(Motion motion)
298{
299 if (m_xMotion == motion)
300 return;
301 m_xMotion = motion;
302 m_dirtyProperties = true;
303 emit xMotionChanged();
304}
305
306QD6Joint::Motion QD6Joint::yMotion() const
307{
308 return m_yMotion;
309}
310
311void QD6Joint::setYMotion(Motion motion)
312{
313 if (m_yMotion == motion)
314 return;
315 m_yMotion = motion;
316 m_dirtyProperties = true;
317 emit yMotionChanged();
318}
319
320QD6Joint::Motion QD6Joint::zMotion() const
321{
322 return m_zMotion;
323}
324
325void QD6Joint::setZMotion(Motion motion)
326{
327 if (m_zMotion == motion)
328 return;
329 m_zMotion = motion;
330 m_dirtyProperties = true;
331 emit zMotionChanged();
332}
333
334QD6Joint::Motion QD6Joint::twistMotion() const
335{
336 return m_twistMotion;
337}
338
339void QD6Joint::setTwistMotion(Motion motion)
340{
341 if (m_twistMotion == motion)
342 return;
343 m_twistMotion = motion;
344 m_dirtyProperties = true;
345 emit twistMotionChanged();
346}
347
348float QD6Joint::linearLimitXLower() const
349{
350 return m_linearLimitXLower;
351}
352
353void QD6Joint::setLinearLimitXLower(float limit)
354{
355 if (qFuzzyCompare(m_linearLimitXLower, limit))
356 return;
357 m_linearLimitXLower = limit;
358 m_dirtyProperties = true;
359 emit linearLimitXLowerChanged();
360}
361
362float QD6Joint::linearLimitXUpper() const
363{
364 return m_linearLimitXUpper;
365}
366
367void QD6Joint::setLinearLimitXUpper(float limit)
368{
369 if (qFuzzyCompare(m_linearLimitXUpper, limit))
370 return;
371 m_linearLimitXUpper = limit;
372 m_dirtyProperties = true;
373 emit linearLimitXUpperChanged();
374}
375
376float QD6Joint::linearLimitYLower() const
377{
378 return m_linearLimitYLower;
379}
380
381void QD6Joint::setLinearLimitYLower(float limit)
382{
383 if (qFuzzyCompare(m_linearLimitYLower, limit))
384 return;
385 m_linearLimitYLower = limit;
386 m_dirtyProperties = true;
387 emit linearLimitYLowerChanged();
388}
389
390float QD6Joint::linearLimitYUpper() const
391{
392 return m_linearLimitYUpper;
393}
394
395void QD6Joint::setLinearLimitYUpper(float limit)
396{
397 if (qFuzzyCompare(m_linearLimitYUpper, limit))
398 return;
399 m_linearLimitYUpper = limit;
400 m_dirtyProperties = true;
401 emit linearLimitYUpperChanged();
402}
403
404float QD6Joint::linearLimitZLower() const
405{
406 return m_linearLimitZLower;
407}
408
409void QD6Joint::setLinearLimitZLower(float limit)
410{
411 if (qFuzzyCompare(m_linearLimitZLower, limit))
412 return;
413 m_linearLimitZLower = limit;
414 m_dirtyProperties = true;
415 emit linearLimitZLowerChanged();
416}
417
418float QD6Joint::linearLimitZUpper() const
419{
420 return m_linearLimitZUpper;
421}
422
423void QD6Joint::setLinearLimitZUpper(float limit)
424{
425 if (qFuzzyCompare(m_linearLimitZUpper, limit))
426 return;
427 m_linearLimitZUpper = limit;
428 m_dirtyProperties = true;
429 emit linearLimitZUpperChanged();
430}
431
432float QD6Joint::twistLimitLower() const
433{
434 return m_twistLimitLower;
435}
436
437void QD6Joint::setTwistLimitLower(float limit)
438{
439 // PhysX requires the twist limit to be strictly within (-2*Pi, 2*Pi)
440 limit = qBound(-float(M_PI * 2) + 0.0001f, limit, float(M_PI * 2) - 0.0001f);
441
442 if (qFuzzyCompare(m_twistLimitLower, limit))
443 return;
444 m_twistLimitLower = limit;
445 m_dirtyProperties = true;
446 emit twistLimitLowerChanged();
447}
448
449float QD6Joint::twistLimitUpper() const
450{
451 return m_twistLimitUpper;
452}
453
454void QD6Joint::setTwistLimitUpper(float limit)
455{
456 // PhysX requires the twist limit to be strictly within (-2*Pi, 2*Pi)
457 limit = qBound(-float(M_PI * 2) + 0.0001f, limit, float(M_PI * 2) - 0.0001f);
458
459 if (qFuzzyCompare(m_twistLimitUpper, limit))
460 return;
461 m_twistLimitUpper = limit;
462 m_dirtyProperties = true;
463 emit twistLimitUpperChanged();
464}
465
466float QD6Joint::linearStiffness() const
467{
468 return m_linearStiffness;
469}
470
471void QD6Joint::setLinearStiffness(float stiffness)
472{
473 stiffness = qMax(0.f, stiffness);
474
475 if (qFuzzyCompare(m_linearStiffness, stiffness))
476 return;
477 m_linearStiffness = stiffness;
478 m_dirtyProperties = true;
479 emit linearStiffnessChanged();
480}
481
482float QD6Joint::linearDamping() const
483{
484 return m_linearDamping;
485}
486
487void QD6Joint::setLinearDamping(float damping)
488{
489 damping = qMax(0.f, damping);
490
491 if (qFuzzyCompare(m_linearDamping, damping))
492 return;
493 m_linearDamping = damping;
494 m_dirtyProperties = true;
495 emit linearDampingChanged();
496}
497
498float QD6Joint::angularStiffness() const
499{
500 return m_angularStiffness;
501}
502
503void QD6Joint::setAngularStiffness(float stiffness)
504{
505 stiffness = qMax(0.f, stiffness);
506 if (qFuzzyCompare(m_angularStiffness, stiffness))
507 return;
508 m_angularStiffness = stiffness;
509 m_dirtyProperties = true;
510 emit angularStiffnessChanged();
511}
512
513float QD6Joint::angularDamping() const
514{
515 return m_angularDamping;
516}
517
518void QD6Joint::setAngularDamping(float damping)
519{
520 damping = qMax(0.f, damping);
521 if (qFuzzyCompare(m_angularDamping, damping))
522 return;
523 m_angularDamping = damping;
524 m_dirtyProperties = true;
525 emit angularDampingChanged();
526}
527
528
529QD6Joint::Motion QD6Joint::swingMotionY() const
530{
531 return m_swingMotionY;
532}
533
534void QD6Joint::setSwingMotionY(Motion newSwingMotionY)
535{
536 if (m_swingMotionY == newSwingMotionY)
537 return;
538 m_swingMotionY = newSwingMotionY;
539 m_dirtyProperties = true;
540 emit swingMotionYChanged();
541}
542
543QD6Joint::Motion QD6Joint::swingMotionZ() const
544{
545 return m_swingMotionZ;
546}
547
548void QD6Joint::setSwingMotionZ(Motion newSwingMotionZ)
549{
550 if (m_swingMotionZ == newSwingMotionZ)
551 return;
552 m_swingMotionZ = newSwingMotionZ;
553 m_dirtyProperties = true;
554 emit swingMotionZChanged();
555}
556
557float QD6Joint::swingLimitAngleY() const
558{
559 return m_swingLimitAngleY;
560}
561
562void QD6Joint::setSwingLimitAngleY(float newSwingLimitAngleY)
563{
564 // Clamp swing angle between Math standard (0, Pi)
565 newSwingLimitAngleY = qBound(0.0001f, newSwingLimitAngleY, float(M_PI) - 0.0001f);
566
567 if (qFuzzyCompare(m_swingLimitAngleY, newSwingLimitAngleY))
568 return;
569 m_swingLimitAngleY = newSwingLimitAngleY;
570 m_dirtyProperties = true;
571 emit swingLimitAngleYChanged();
572}
573
574float QD6Joint::swingLimitAngleZ() const
575{
576 return m_swingLimitAngleZ;
577}
578
579void QD6Joint::setSwingLimitAngleZ(float newSwingLimitAngleZ)
580{
581 // Clamp swing angle between Math standard (0, Pi)
582 newSwingLimitAngleZ = qBound(0.0001f, newSwingLimitAngleZ, float(M_PI) - 0.0001f);
583
584 if (qFuzzyCompare(m_swingLimitAngleZ, newSwingLimitAngleZ))
585 return;
586 m_swingLimitAngleZ = newSwingLimitAngleZ;
587 m_dirtyProperties = true;
588 emit swingLimitAngleZChanged();
589}
590
591physx::PxJoint *QD6Joint::createPhysxJoint(physx::PxRigidActor *actorA,
592 physx::PxRigidActor *actorB,
593 const physx::PxTransform &trfA,
594 const physx::PxTransform &trfB)
595{
596 return physx::PxD6JointCreate(*StaticPhysXObjects::getReference().physics,
597 actorA, trfA, actorB, trfB);
598}
599
600void QD6Joint::setJointProperties()
601{
602 auto *d6Joint = static_cast<physx::PxD6Joint *>(m_joint);
603 if (!d6Joint)
604 return;
605
606 const physx::PxTolerancesScale scale =
607 StaticPhysXObjects::getReference().physics->getTolerancesScale();
608
609 d6Joint->setMotion(physx::PxD6Axis::eX, toPxEnum(m_xMotion));
610 d6Joint->setMotion(physx::PxD6Axis::eY, toPxEnum(m_yMotion));
611 d6Joint->setMotion(physx::PxD6Axis::eZ, toPxEnum(m_zMotion));
612 d6Joint->setMotion(physx::PxD6Axis::eTWIST, toPxEnum(m_twistMotion));
613 d6Joint->setMotion(physx::PxD6Axis::eSWING1, toPxEnum(m_swingMotionY));
614 d6Joint->setMotion(physx::PxD6Axis::eSWING2, toPxEnum(m_swingMotionZ));
615
616 if (m_xMotion == Motion::Limited) {
617 physx::PxJointLinearLimitPair limit(scale, qMin(m_linearLimitXLower, m_linearLimitXUpper),
618 qMax(m_linearLimitXLower, m_linearLimitXUpper));
619 limit.stiffness = m_linearStiffness; // Use linear stiffness for linear limits
620 limit.damping = m_linearDamping; // Use linear damping for linear limits
621 d6Joint->setLinearLimit(physx::PxD6Axis::eX, limit);
622 }
623
624 if (m_yMotion == Motion::Limited) {
625 physx::PxJointLinearLimitPair limit(scale, qMin(m_linearLimitYLower, m_linearLimitYUpper),
626 qMax(m_linearLimitYLower, m_linearLimitYUpper));
627 limit.stiffness = m_linearStiffness; // Use linear stiffness for linear limits
628 limit.damping = m_linearDamping; // Use linear damping for linear limits
629 d6Joint->setLinearLimit(physx::PxD6Axis::eY, limit);
630 }
631
632 if (m_zMotion == Motion::Limited) {
633 physx::PxJointLinearLimitPair limit(scale, qMin(m_linearLimitZLower, m_linearLimitZUpper),
634 qMax(m_linearLimitZLower, m_linearLimitZUpper));
635 limit.stiffness = m_linearStiffness; // Use linear stiffness for linear limits
636 limit.damping = m_linearDamping; // Use linear damping for linear limits
637 d6Joint->setLinearLimit(physx::PxD6Axis::eZ, limit);
638 }
639
640 if (m_twistMotion == Motion::Limited) {
641 physx::PxJointAngularLimitPair limit(qMin(m_twistLimitLower, m_twistLimitUpper),
642 qMax(m_twistLimitLower, m_twistLimitUpper));
643 limit.stiffness = m_angularStiffness; // Use angular stiffness for twist limit
644 limit.damping = m_angularDamping; // Use angular damping for twist limit
645 d6Joint->setTwistLimit(limit);
646 }
647
648 if (m_swingMotionY == Motion::Limited || m_swingMotionZ == Motion::Limited) {
649 // PhysX requires cone angles to be strictly within (0, Pi) range
650 const float angleY = qBound(0.0001f, m_swingLimitAngleY, float(M_PI) - 0.0001f);
651 const float angleZ = qBound(0.0001f, m_swingLimitAngleZ, float(M_PI) - 0.0001f);
652
653 physx::PxJointLimitCone limit(angleY, angleZ);
654 limit.stiffness = m_angularStiffness;
655 limit.damping = m_angularDamping;
656 d6Joint->setSwingLimit(limit);
657 }
658}
659
physx::PxD6Motion::Enum toPxEnum(QD6Joint::Motion val)
Definition qd6joint.cpp:278
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE