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
qdistancejoint.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
5
6#include "physxnode/qphysxactorbody_p.h"
9
10#include <extensions/PxDistanceJoint.h>
11
12#include <foundation/PxSimpleTypes.h>
13
15
16/*!
17 \qmltype DistanceJoint
18 \inqmlmodule QtQuick3D.Physics
19 \since 6.12
20 \inherits FlexibleJoint
21 \brief A distance joint.
22
23 A distance joint that will keep the origins of the joint within the
24 distance range specified by \l{DistanceJoint::}{minDistance} and
25 \l{DistanceJoint::}{maxDistance}.
26
27 \sa {FixedJoint}
28 \sa {PrismaticJoint}
29 \sa {RevoluteJoint}
30 \sa {SphericalJoint}
31 \sa {D6Joint}
32*/
33
34/*!
35 \qmlproperty real DistanceJoint::minDistance
36 \since 6.12
37 \default 0.0
38
39 The minimum distance of the joint constraint. The constraint is only
40 enforced when this value is greater than \c 0.0.
41
42 Range: \c{[0, inf]}
43
44 \sa maxDistance
45*/
46
47/*!
48 \qmlproperty real DistanceJoint::maxDistance
49 \since 6.12
50 \default 0.0
51
52 The maximum distance of the joint constraint. The constraint is only
53 enforced when this value is greater than \c 0.0.
54
55 Range: \c{[0, inf]}
56
57 \sa minDistance
58*/
59
60float QDistanceJoint::minDistance() const
61{
62 return m_minDistance;
63}
64
65void QDistanceJoint::setMinDistance(float newMinDistance)
66{
67 newMinDistance = qBound(0.0f, newMinDistance, PX_MAX_F32);
68 if (qFuzzyCompare(m_minDistance, newMinDistance))
69 return;
70 m_minDistance = newMinDistance;
71 m_dirtyProperties = true;
72 emit minDistanceChanged();
73}
74
75float QDistanceJoint::maxDistance() const
76{
77 return m_maxDistance;
78}
79
80void QDistanceJoint::setMaxDistance(float newMaxDistance)
81{
82 newMaxDistance = qBound(0.0f, newMaxDistance, PX_MAX_F32);
83 if (qFuzzyCompare(m_maxDistance, newMaxDistance))
84 return;
85 m_maxDistance = newMaxDistance;
86 m_dirtyProperties = true;
87 emit maxDistanceChanged();
88}
89
90physx::PxJoint *QDistanceJoint::createPhysxJoint(physx::PxRigidActor *actorA,
91 physx::PxRigidActor *actorB,
92 const physx::PxTransform &trfA,
93 const physx::PxTransform &trfB)
94{
95 return physx::PxDistanceJointCreate(*StaticPhysXObjects::getReference().physics, actorA, trfA,
96 actorB, trfB);
97}
98
99void QDistanceJoint::setJointProperties()
100{
101 physx::PxDistanceJoint *joint = static_cast<physx::PxDistanceJoint *>(m_joint);
102
103 float minDistance = m_minDistance;
104 float maxDistance = m_maxDistance;
105
106 // PhysX doesn't enforce minDistance <= maxDistance itself, but violating
107 // it produces a constraint that can never be satisfied. Only reorder when
108 // both limits are actually active - a value left at the 0 "disabled"
109 // default must never be treated as "smaller than" an actively-configured
110 // limit on the other side.
111 if (m_minDistance > 0 && m_maxDistance > 0) {
112 minDistance = qMin(m_minDistance, m_maxDistance);
113 maxDistance = qMax(m_minDistance, m_maxDistance);
114 }
115
116 joint->setMinDistance(minDistance);
117 joint->setMaxDistance(maxDistance);
118 joint->setDistanceJointFlag(physx::PxDistanceJointFlag::eMIN_DISTANCE_ENABLED, minDistance > 0);
119 joint->setDistanceJointFlag(physx::PxDistanceJointFlag::eMAX_DISTANCE_ENABLED, maxDistance > 0);
120
121 joint->setStiffness(m_stiffness);
122 joint->setDamping(m_damping);
123
124 const bool enableSpring = (m_stiffness > 0.0f) || (m_damping > 0.0f);
125 joint->setDistanceJointFlag(physx::PxDistanceJointFlag::eSPRING_ENABLED, enableSpring);
126}
127
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE
#define emit