6#include "physxnode/qabstractphysxnode_p.h"
7#include "physxnode/qphysxactorbody_p.h"
13#include <extensions/PxJoint.h>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
41
42
43
44
45
46
49
50
51
52
53
54
57
58
59
60
61
62
65
66
67
68
69
70
73
74
75
76
77
78
81
82
83
84
85
86
88QPhysicsJoint::QPhysicsJoint()
90 QPhysicsWorld::registerJoint(
this);
93QPhysicsJoint::~QPhysicsJoint()
95 QPhysicsWorld::deregisterJoint(
this);
98QAbstractPhysicsBody *QPhysicsJoint::bodyA()
const
103void QPhysicsJoint::setBodyA(QAbstractPhysicsBody *newBodyA)
105 if (m_bodyA == newBodyA)
109 m_bodyA->disconnect(
this);
111 connect(newBodyA, &QAbstractPhysicsBody::destroyed,
this, &QPhysicsJoint::onBodyDestroyed);
114 m_needsRebuild =
true;
118QAbstractPhysicsBody *QPhysicsJoint::bodyB()
const
123void QPhysicsJoint::setBodyB(QAbstractPhysicsBody *newBodyB)
125 if (m_bodyB == newBodyB)
129 m_bodyB->disconnect(
this);
131 connect(newBodyB, &QAbstractPhysicsBody::destroyed,
this, &QPhysicsJoint::onBodyDestroyed);
134 m_needsRebuild =
true;
138QVector3D QPhysicsJoint::positionA()
const
143void QPhysicsJoint::setPositionA(
const QVector3D &newPositionA)
145 if (m_positionA == newPositionA)
147 m_positionA = newPositionA;
148 m_needsRebuild =
true;
149 emit positionAChanged();
152QVector3D QPhysicsJoint::positionB()
const
157void QPhysicsJoint::setPositionB(
const QVector3D &newPositionB)
159 if (m_positionB == newPositionB)
161 m_positionB = newPositionB;
162 m_needsRebuild =
true;
163 emit positionBChanged();
166QQuaternion QPhysicsJoint::orientationA()
const
168 return m_orientationA;
171void QPhysicsJoint::setOrientationA(
const QQuaternion &newOrientationA)
173 if (m_orientationA == newOrientationA)
175 m_orientationA = newOrientationA;
176 m_needsRebuild =
true;
177 emit orientationAChanged();
180QQuaternion QPhysicsJoint::orientationB()
const
182 return m_orientationB;
185void QPhysicsJoint::setOrientationB(
const QQuaternion &newOrientationB)
187 if (m_orientationB == newOrientationB)
189 m_orientationB = newOrientationB;
190 m_needsRebuild =
true;
191 emit orientationBChanged();
194physx::PxJoint *QPhysicsJoint::getPhysXBackend()
const
199void QPhysicsJoint::onBodyDestroyed(QObject *body)
203 body->disconnect(
this);
210void QPhysicsJoint::updatePhysXBackend()
212 if (!m_dirtyProperties && !m_needsRebuild)
215 if (m_joint && m_needsRebuild) {
220 if (!m_bodyA && !m_bodyB)
223 QPhysXActorBody *actorBodyA = m_bodyA && m_bodyA->m_backendObject
224 ? qobject_cast<QPhysXActorBody *>(m_bodyA->m_backendObject)
226 QPhysXActorBody *actorBodyB = m_bodyB && m_bodyB->m_backendObject
227 ? qobject_cast<QPhysXActorBody *>(m_bodyB->m_backendObject)
229 physx::PxRigidActor *actorA = actorBodyA ? actorBodyA->actor :
nullptr;
230 physx::PxRigidActor *actorB = actorBodyB ? actorBodyB->actor :
nullptr;
232 if (!actorA && !actorB)
235 Q_ASSERT(StaticPhysXObjects::getReference().physicsCreated);
236 Q_ASSERT(StaticPhysXObjects::getReference().physics);
239 const bool compatibleTypes =
240 (actorA && actorA->getType() == physx::PxActorType::Enum::eRIGID_DYNAMIC)
241 || (actorB && actorB->getType() == physx::PxActorType::Enum::eRIGID_DYNAMIC);
243 if (!compatibleTypes) {
244 qWarning() <<
"QPhysicsJoint: Incompatible body types used for joint.";
247 if (m_needsRebuild && compatibleTypes) {
248 auto trfA = physx::PxTransform(QPhysicsUtils::toPhysXType(m_positionA),
249 QPhysicsUtils::toPhysXType(m_orientationA));
250 auto trfB = physx::PxTransform(QPhysicsUtils::toPhysXType(m_positionB),
251 QPhysicsUtils::toPhysXType(m_orientationB));
252 if (!trfA.isSane()) {
253 qWarning() <<
"PhysicsJoint: positionA/orientationA is not finite, using identity "
255 trfA = physx::PxTransform(physx::PxIdentity);
257 if (!trfB.isSane()) {
258 qWarning() <<
"PhysicsJoint: positionB/orientationB is not finite, using identity "
260 trfB = physx::PxTransform(physx::PxIdentity);
262 m_joint = createPhysxJoint(actorA, actorB, trfA, trfB);
265 if (m_joint && (m_dirtyProperties || m_needsRebuild)) {
266 setJointProperties();
269 m_dirtyProperties =
false;
270 m_needsRebuild =
false;
#define QT_BEGIN_NAMESPACE