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
qphysxactorbody.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include "PxMaterial.h"
8#include "PxPhysics.h"
9#include "PxRigidDynamic.h"
10#include "PxRigidActor.h"
11#include "PxScene.h"
12
13#include "physxnode/qphysxworld_p.h"
17#include "qplaneshape_p.h"
19
20#include <QtGui/qquaternion.h>
21
22#define PHYSX_RELEASE(x)
23 if (x != nullptr) {
24 x->release();
25 x = nullptr;
26 }
27
29
30static physx::PxTransform getPhysXLocalTransform(const QQuick3DNode *node)
31{
32 // Modify transforms to make the PhysX shapes match the QtQuick3D conventions
33 if (qobject_cast<const QPlaneShape *>(node) != nullptr) {
34 // Rotate the plane to make it match the built-in rectangle
35 const QQuaternion rotation = QPhysicsUtils::kMinus90YawRotation * node->rotation();
36 return physx::PxTransform(QPhysicsUtils::toPhysXType(node->position()),
37 QPhysicsUtils::toPhysXType(rotation));
38 } else if (auto *hf = qobject_cast<const QHeightFieldShape *>(node)) {
39 // Shift the height field so it's centered at the origin
40 return physx::PxTransform(QPhysicsUtils::toPhysXType(node->position() + hf->hfOffset()),
41 QPhysicsUtils::toPhysXType(node->rotation()));
42 }
43
44 const QQuaternion &rotation = node->rotation();
45 const QVector3D &localPosition = node->position();
46 const QVector3D &scale = node->sceneScale();
47 return physx::PxTransform(QPhysicsUtils::toPhysXType(localPosition * scale),
48 QPhysicsUtils::toPhysXType(rotation));
49}
50
51QPhysXActorBody::QPhysXActorBody(QAbstractPhysicsNode *frontEnd) : QAbstractPhysXNode(frontEnd) { }
52
54{
55 if (actor) {
56 physX->scene->removeActor(*actor);
58 }
60}
61
62void QPhysXActorBody::init(QPhysicsWorld * /*world*/, QPhysXWorld *physX)
63{
64 Q_ASSERT(!actor);
65
66 createMaterial(physX);
67 createActor(physX);
68
69 actor->userData = reinterpret_cast<void *>(frontendNode);
70 physX->scene->addActor(*actor);
71 setShapesDirty(true);
72}
73
74void QPhysXActorBody::sync(float /*deltaTime*/,
75 QHash<QQuick3DNode *, QMatrix4x4> & /*transformCache*/)
76{
77 auto *body = static_cast<QAbstractPhysicsBody *>(frontendNode);
78 if (QPhysicsMaterial *qtMaterial = body->physicsMaterial()) {
79 const float staticFriction = qtMaterial->staticFriction();
80 const float dynamicFriction = qtMaterial->dynamicFriction();
81 const float restitution = qtMaterial->restitution();
82 if (material->getStaticFriction() != staticFriction)
83 material->setStaticFriction(staticFriction);
84 if (material->getDynamicFriction() != dynamicFriction)
85 material->setDynamicFriction(dynamicFriction);
86 if (material->getRestitution() != restitution)
87 material->setRestitution(restitution);
88 }
89}
90
92{
93 if (!frontendNode || !actor)
94 return;
95
96 // Go through the shapes and look for a change in pose (rotation, position)
97 // TODO: it is likely cheaper to connect a signal for changes on the position and rotation
98 // property and mark the node dirty then.
99 if (!shapesDirty()) {
100 const auto &collisionShapes = frontendNode->getCollisionShapesList();
101 const auto &physXShapes = shapes;
102
103 const int len = collisionShapes.size();
104 if (physXShapes.size() != len) {
105 // This should not really happen but check it anyway
106 setShapesDirty(true);
107 } else {
108 for (int i = 0; i < len; i++) {
109 auto poseNew = getPhysXLocalTransform(collisionShapes[i]);
110 auto poseOld = physXShapes[i]->getLocalPose();
111
112 if (!QPhysicsUtils::fuzzyEquals(poseNew, poseOld)) {
113 setShapesDirty(true);
114 break;
115 }
116 }
117 }
118 }
119}
120
121void QPhysXActorBody::rebuildDirtyShapes(QPhysicsWorld * /*world*/, QPhysXWorld *physX)
122{
123 if (!shapesDirty())
124 return;
125 buildShapes(physX);
126 setShapesDirty(false);
127}
128
130{
132 const physx::PxTransform trf = QPhysicsUtils::toPhysXTransform(frontendNode->scenePosition(),
133 frontendNode->sceneRotation());
134 actor = s_physx.physics->createRigidDynamic(trf);
135}
136
138{
139 return true;
140}
141
143{
144 return actor->getGlobalPose();
145}
146
148{
149 auto body = actor;
150 for (auto *shape : shapes) {
151 body->detachShape(*shape);
152 PHYSX_RELEASE(shape);
153 }
154
155 // TODO: Only remove changed shapes?
156 shapes.clear();
157
158 for (const auto &collisionShape : frontendNode->getCollisionShapesList()) {
159 // TODO: shapes can be shared between multiple actors.
160 // Do we need to create new ones for every body?
161 auto *geom = collisionShape->getPhysXGeometry();
162 if (!geom || !material)
163 continue;
164
165 auto &s_physx = StaticPhysXObjects::getReference();
166 auto physXShape = s_physx.physics->createShape(*geom, *material);
167
168 if (useTriggerFlag()) {
169 physXShape->setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, false);
170 physXShape->setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, true);
171 }
172
173 { // Setup filtering
174 physx::PxFilterData filterData;
175 filterData.word0 = frontendNode->filterGroup();
176 filterData.word1 = frontendNode->filterIgnoreGroups();
177 physXShape->setSimulationFilterData(filterData);
178 }
179
180 shapes.push_back(physXShape);
181 physXShape->setLocalPose(getPhysXLocalTransform(collisionShape));
182 body->attachShape(*physXShape);
183 }
184
185 // Filters are always clean after building shapes
186 setFiltersDirty(false);
187}
188
190{
191 if (!filtersDirty())
192 return;
193
194 // Go through all shapes and set the filter group and mask.
195 // TODO: What about shared shapes on several actors?
196 for (auto &physXShape : shapes) {
197 physx::PxFilterData filterData;
198 filterData.word0 = frontendNode->filterGroup();
199 filterData.word1 = frontendNode->filterIgnoreGroups();
200 physXShape->setSimulationFilterData(filterData);
201 }
202
203 setFiltersDirty(false);
204}
205
virtual void cleanup(QPhysXWorld *)
QAbstractPhysXNode(QAbstractPhysicsNode *node)
void setShapesDirty(bool dirty)
void setFiltersDirty(bool dirty)
virtual void createMaterial(QPhysXWorld *physX)
void markDirtyShapes() override
void buildShapes(QPhysXWorld *physX)
void rebuildDirtyShapes(QPhysicsWorld *world, QPhysXWorld *physX) override
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
bool debugGeometryCapability() override
QPhysXActorBody(QAbstractPhysicsNode *frontEnd)
void cleanup(QPhysXWorld *physX) override
physx::PxRigidActor * actor
virtual void createActor(QPhysXWorld *physX)
void sync(float deltaTime, QHash< QQuick3DNode *, QMatrix4x4 > &transformCache) override
void updateFilters() override
physx::PxTransform getGlobalPose() override
physx::PxScene * scene
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:172
#define PHYSX_RELEASE(x)
static QT_BEGIN_NAMESPACE physx::PxTransform getPhysXLocalTransform(const QQuick3DNode *node)
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE
static StaticPhysXObjects & getReference()