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