1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qphysxstaticbody_p.h"
5
6#include "PxPhysics.h"
7#include "PxRigidActor.h"
8#include "PxRigidStatic.h"
9
10#include "qphysicsutils_p.h"
11#include "qphysicsworld_p.h"
12#include "qstaticrigidbody_p.h"
13#include "qstaticphysxobjects_p.h"
14
15QT_BEGIN_NAMESPACE
16
17static inline bool fuzzyEquals(const physx::PxTransform &a, const physx::PxTransform &b)
18{
19 return qFuzzyCompare(p1: a.p.x, p2: b.p.x) && qFuzzyCompare(p1: a.p.y, p2: b.p.y) && qFuzzyCompare(p1: a.p.z, p2: b.p.z)
20 && qFuzzyCompare(p1: a.q.x, p2: b.q.x) && qFuzzyCompare(p1: a.q.y, p2: b.q.y)
21 && qFuzzyCompare(p1: a.q.z, p2: b.q.z) && qFuzzyCompare(p1: a.q.w, p2: b.q.w);
22}
23
24QPhysXStaticBody::QPhysXStaticBody(QStaticRigidBody *frontEnd) : QPhysXRigidBody(frontEnd) { }
25
26DebugDrawBodyType QPhysXStaticBody::getDebugDrawBodyType()
27{
28 return DebugDrawBodyType::Static;
29}
30
31void QPhysXStaticBody::sync(float deltaTime, QHash<QQuick3DNode *, QMatrix4x4> &transformCache)
32{
33 auto *staticBody = static_cast<QStaticRigidBody *>(frontendNode);
34 const physx::PxTransform poseNew = QPhysicsUtils::toPhysXTransform(position: staticBody->scenePosition(),
35 rotation: staticBody->sceneRotation());
36 const physx::PxTransform poseOld = actor->getGlobalPose();
37
38 // For performance we only update static objects if they have been moved
39 if (!fuzzyEquals(a: poseNew, b: poseOld))
40 actor->setGlobalPose(pose: poseNew);
41 QPhysXActorBody::sync(deltaTime, transformCache);
42}
43
44void QPhysXStaticBody::createActor(QPhysXWorld * /*physX*/)
45{
46 auto &s_physx = StaticPhysXObjects::getReference();
47 const physx::PxTransform trf = QPhysicsUtils::toPhysXTransform(position: frontendNode->scenePosition(),
48 rotation: frontendNode->sceneRotation());
49 actor = s_physx.physics->createRigidStatic(pose: trf);
50}
51
52QT_END_NAMESPACE
53

source code of qtquick3dphysics/src/quick3dphysics/physxnode/qphysxstaticbody.cpp