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 "PxRigidStatic.h"
12#include "PxScene.h"
13#include "characterkinematic/PxController.h"
14#include "characterkinematic/PxControllerManager.h"
15
16#include "physxnode/qphysxworld_p.h"
20#include "qplaneshape_p.h"
22
23#include <QtGui/qquaternion.h>
24
25#define PHYSX_RELEASE(x)
26 if (x != nullptr) {
27 x->release();
28 x = nullptr;
29 }
30
32
33static physx::PxTransform getPhysXLocalTransform(const QQuick3DNode *node)
34{
35 // Modify transforms to make the PhysX shapes match the QtQuick3D conventions
36 if (qobject_cast<const QPlaneShape *>(node) != nullptr) {
37 // Rotate the plane to make it match the built-in rectangle
38 const QQuaternion rotation = QPhysicsUtils::kMinus90YawRotation * node->rotation();
39 return physx::PxTransform(QPhysicsUtils::toPhysXType(node->position()),
40 QPhysicsUtils::toPhysXType(rotation));
41 } else if (auto *hf = qobject_cast<const QHeightFieldShape *>(node)) {
42 // Shift the height field so it's centered at the origin
43 return physx::PxTransform(QPhysicsUtils::toPhysXType(node->position() + hf->hfOffset()),
44 QPhysicsUtils::toPhysXType(node->rotation()));
45 }
46
47 const QQuaternion &rotation = node->rotation();
48 const QVector3D &localPosition = node->position();
49 const QVector3D &scale = node->sceneScale();
50 return physx::PxTransform(QPhysicsUtils::toPhysXType(localPosition * scale),
51 QPhysicsUtils::toPhysXType(rotation));
52}
53
54QPhysXActorBody::QPhysXActorBody(QAbstractPhysicsNode *frontEnd) : QAbstractPhysXNode(frontEnd) { }
55
57{
58 if (actor) {
59 physX->scene->removeActor(*actor);
61 }
63}
64
65void QPhysXActorBody::init(QPhysicsWorld * /*world*/, QPhysXWorld *physX)
66{
67 Q_ASSERT(!actor);
68
70 createActor(physX);
71
72 actor->userData = reinterpret_cast<void *>(frontendNode);
73 physX->scene->addActor(*actor);
74 setShapesDirty(true);
75}
76
77void QPhysXActorBody::sync(float /*deltaTime*/,
78 QHash<QQuick3DNode *, QMatrix4x4> & /*transformCache*/)
79{
80 // The physicsMaterial property, and the properties of the material itself, can change while
81 // the simulation is running. Materials are shared between all nodes that need the same
82 // properties, so rather than modifying the material in place, the node is moved to the
83 // material with the new properties, and its shapes are pointed at it.
84 if (updateMaterial() && material) {
85 for (auto *shape : std::as_const(shapes))
86 shape->setMaterials(&material, 1);
87 }
88}
89
91{
92 if (!frontendNode || !actor)
93 return;
94
95 // Go through the shapes and look for a change in pose (rotation, position)
96 // TODO: it is likely cheaper to connect a signal for changes on the position and rotation
97 // property and mark the node dirty then.
98 if (!shapesDirty()) {
99 const auto &collisionShapes = frontendNode->getCollisionShapesList();
100 const auto &physXShapes = shapes;
101
102 const int len = collisionShapes.size();
103 if (physXShapes.size() != len) {
104 // This should not really happen but check it anyway
105 setShapesDirty(true);
106 } else {
107 for (int i = 0; i < len; i++) {
108 auto poseNew = getPhysXLocalTransform(collisionShapes[i]);
109 auto poseOld = physXShapes[i]->getLocalPose();
110
111 if (!QPhysicsUtils::fuzzyEquals(poseNew, poseOld)) {
112 setShapesDirty(true);
113 break;
114 }
115 }
116 }
117 }
118}
119
120void QPhysXActorBody::rebuildDirtyShapes(QPhysicsWorld * /*world*/, QPhysXWorld *physX)
121{
122 if (!shapesDirty())
123 return;
124 buildShapes(physX);
125 setShapesDirty(false);
126}
127
129{
130 if (actor)
131 actor->userData = nullptr;
133}
134
136{
138 physx::PxTransform trf = QPhysicsUtils::toPhysXTransform(frontendNode->scenePosition(),
139 frontendNode->sceneRotation());
140 if (!trf.isSane()) {
141 qWarning() << "PhysicsNode: position/rotation is not finite, using identity instead.";
142 trf = physx::PxTransform(physx::PxIdentity);
143 }
144 auto *dynamicActor = s_physx.physics->createRigidDynamic(trf);
145
146 // A trigger has to be a dynamic actor, since PhysX only reports a trigger
147 // pair when one of the two is, but it is not simulated and nothing reads
148 // the velocity gravity would give it. Without gravity it stays where it is
149 // put and can fall asleep, which is what stops its pairs being tested:
150 // PhysX skips a trigger pair's overlap test only when both of its actors
151 // are asleep (see Sc::TriggerInteraction).
152 if (useTriggerFlag())
153 dynamicActor->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY, true);
154
155 actor = dynamicActor;
156}
157
159{
160 return true;
161}
162
164{
165 return actor->getGlobalPose();
166}
167
169{
170 auto body = actor;
171 for (auto *shape : std::as_const(shapes)) {
172 body->detachShape(*shape);
173 PHYSX_RELEASE(shape);
174 }
175
176 // TODO: Only remove changed shapes?
177 shapes.clear();
178
179 for (const auto &collisionShape : frontendNode->getCollisionShapesList()) {
180 auto *geom = collisionShape->getPhysXGeometry();
181 if (!geom || !material)
182 continue;
183
184 auto &s_physx = StaticPhysXObjects::getReference();
185 // The shape is created exclusive to this actor: a shape that may be shared between
186 // actors cannot be modified once it is attached to one, and these shapes are modified
187 // while attached, for instance when their filter data changes.
188 auto physXShape = s_physx.physics->createShape(*geom, *material, true);
189 if (!physXShape) {
190 qWarning() << "QtQuick3DPhysics: could not create shape, invalid geometry.";
191 continue;
192 }
193
194 // Ties the PxShape back to the collision shape it was built for: a
195 // trigger pair is matched by those, which survive a rebuild replacing
196 // every PxShape of a node.
197 physXShape->userData = reinterpret_cast<void *>(collisionShape);
198
199 if (useTriggerFlag()) {
200 physXShape->setFlag(physx::PxShapeFlag::eSCENE_QUERY_SHAPE, false);
201 physXShape->setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, false);
202 physXShape->setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, true);
203 }
204
205 physXShape->setSimulationFilterData(QPhysicsUtils::toPhysXType(frontendNode->filterData()));
206
207 physx::PxTransform localPose = getPhysXLocalTransform(collisionShape);
208 if (!localPose.isSane()) {
209 qWarning() << "QtQuick3DPhysics: collision shape position/rotation is not finite, "
210 "using identity local pose instead.";
211 localPose = physx::PxTransform(physx::PxIdentity);
212 }
213 physXShape->setLocalPose(localPose);
214
215 // A shape PhysX will not take is not kept: left in 'shapes' it would make
216 // markDirtyShapes() find the body's shapes to be in order, so nothing would
217 // ever rebuild them, and the next rebuild would detach a shape that had never
218 // been attached.
219 if (!body->attachShape(*physXShape)) {
220 qWarning() << "QtQuick3DPhysics: could not attach collision shape to body, it will "
221 "not take part in the simulation.";
222 PHYSX_RELEASE(physXShape);
223 continue;
224 }
225
226 shapes.push_back(physXShape);
227 }
228
229 // Filters are always clean after building shapes
230 setFiltersDirty(false);
231}
232
234{
235 if (!filtersDirty())
236 return;
237
238 // Go through all shapes and set the filter group and mask.
239 // TODO: What about shared shapes on several actors?
240 const physx::PxFilterData filterData = QPhysicsUtils::toPhysXType(frontendNode->filterData());
241 for (auto &physXShape : shapes)
242 physXShape->setSimulationFilterData(filterData);
243
244 // PxController caches the static shapes near it and reuses that cache
245 // until it moves outside the cached bounds or a static shape is added,
246 // removed or moved. We have to manually invalidate the cache when the
247 // filter data changes; otherwise a controller next to this body would
248 // keep colliding with it (or passing through it) based on the old groups.
249 // Dynamic shapes aren't cached, so only statics need this.
250 if (actor && actor->is<physx::PxRigidStatic>() && physX && physX->controllerManager) {
251 auto *manager = physX->controllerManager;
252 for (physx::PxU32 i = 0; i < manager->getNbControllers(); ++i)
253 manager->getController(i)->invalidateCache();
254 }
255
256 setFiltersDirty(false);
257}
258
virtual void cleanup(QPhysXWorld *)
void setShapesDirty(bool dirty)
void setFiltersDirty(bool dirty)
virtual void detachFrontend()
virtual bool useTriggerFlag()
void updateFilters(QPhysXWorld *physX) override
void markDirtyShapes() override
void buildShapes(QPhysXWorld *physX)
void rebuildDirtyShapes(QPhysicsWorld *world, QPhysXWorld *physX) override
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
void detachFrontend() override
bool debugGeometryCapability() override
void cleanup(QPhysXWorld *physX) override
physx::PxRigidActor * actor
virtual void createActor(QPhysXWorld *physX)
void sync(float deltaTime, QHash< QQuick3DNode *, QMatrix4x4 > &transformCache) override
physx::PxTransform getGlobalPose() override
physx::PxScene * scene
physx::PxControllerManager * controllerManager
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:178
PxTransformT< float > PxTransform
#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()
physx::PxPhysics * physics