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
qphysicsworld_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef PHYSICSWORLD_H
6#define PHYSICSWORLD_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DPhysics/qtquick3dphysicsglobal.h>
20
21#include <QtCore/QLoggingCategory>
22#include <QtCore/QObject>
23#include <QtCore/QPointer>
24#include <QtCore/QTimerEvent>
25#include <QtCore/QElapsedTimer>
26#include <QtCore/QSpan>
27#include <QtGui/QVector3D>
28#include <QtQml/qqml.h>
29#include <QBasicTimer>
30#include <QtQuick3DPhysics/private/qquick3dphysicslocationhit_p.h>
31#include <QtQuick3DPhysics/private/qfilterdata_p.h>
32
33#include <QtQuick3D/private/qquick3dviewport_p.h>
34
35namespace physx {
36class PxMaterial;
37class PxPhysics;
38class PxShape;
39class PxRigidDynamic;
40class PxRigidActor;
41class PxRigidStatic;
42struct PxCookingParams;
43class PxControllerManager;
44class PxConvexMesh;
45class PxTriangleMesh;
46class PxHeightField;
47class PxJoint;
48}
49
51
53
54class QAbstractPhysicsNode;
55class QAbstractCollisionShape;
56class QAbstractRigidBody;
58class QQuick3DModel;
59class QQuick3DGeometry;
60class QQuick3DPrincipledMaterial;
61class QPhysXWorld;
62class FrameAnimator;
63class QPhysicsJoint;
64class QRaycaster;
66
67class Q_QUICK3DPHYSICS_EXPORT QPhysicsWorld : public QObject, public QQmlParserStatus
68{
70 Q_INTERFACES(QQmlParserStatus)
71 Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
72 Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged)
73 Q_PROPERTY(bool forceDebugDraw READ forceDebugDraw WRITE setForceDebugDraw NOTIFY
74 forceDebugDrawChanged)
75 Q_PROPERTY(bool enableCCD READ enableCCD WRITE setEnableCCD NOTIFY enableCCDChanged)
76 Q_PROPERTY(float typicalLength READ typicalLength WRITE setTypicalLength NOTIFY
77 typicalLengthChanged)
79 float typicalSpeed READ typicalSpeed WRITE setTypicalSpeed NOTIFY typicalSpeedChanged)
80 Q_PROPERTY(float defaultDensity READ defaultDensity WRITE setDefaultDensity NOTIFY
81 defaultDensityChanged)
82 Q_PROPERTY(QQuick3DNode *viewport READ viewport WRITE setViewport NOTIFY viewportChanged
83 REVISION(6, 5))
84 Q_PROPERTY(float minimumTimestep READ minimumTimestep WRITE setMinimumTimestep NOTIFY
85 minimumTimestepChanged REVISION(6, 5))
86 Q_PROPERTY(float maximumTimestep READ maximumTimestep WRITE setMaximumTimestep NOTIFY
87 maximumTimestepChanged REVISION(6, 5))
88 Q_PROPERTY(QQuick3DNode *scene READ scene WRITE setScene NOTIFY sceneChanged REVISION(6, 5))
89 Q_PROPERTY(int numThreads READ numThreads WRITE setNumThreads NOTIFY numThreadsChanged
90 REVISION(6, 7))
91 Q_PROPERTY(bool reportKinematicKinematicCollisions READ reportKinematicKinematicCollisions WRITE
92 setReportKinematicKinematicCollisions NOTIFY
93 reportKinematicKinematicCollisionsChanged FINAL REVISION(6, 7))
94 Q_PROPERTY(bool reportStaticKinematicCollisions READ reportStaticKinematicCollisions WRITE
95 setReportStaticKinematicCollisions NOTIFY
96 reportStaticKinematicCollisionsChanged FINAL REVISION(6, 7))
98 Q_PROPERTY(QueryStructure staticQueryStructure READ staticQueryStructure WRITE
99 setStaticQueryStructure NOTIFY staticQueryStructureChanged FINAL REVISION(6, 13))
100
101 Q_PROPERTY(QueryStructure dynamicQueryStructure READ dynamicQueryStructure WRITE
102 setDynamicQueryStructure NOTIFY dynamicQueryStructureChanged FINAL REVISION(6, 13))
103
104 QML_NAMED_ELEMENT(PhysicsWorld)
105
106public:
107 enum class QueryStructure {
108 NoStructure, // eNONE
109 StaticTree, // eSTATIC_AABB_TREE
110 DynamicTree // eDYNAMIC_AABB_TREE
111 };
112 Q_ENUM(QueryStructure)
113
114 explicit QPhysicsWorld(QObject *parent = nullptr);
115 ~QPhysicsWorld();
117 void classBegin() override;
118 void componentComplete() override;
119
120 QVector3D gravity() const;
121
122 bool running() const;
123 bool forceDebugDraw() const;
124 bool enableCCD() const;
125 float typicalLength() const;
126 float typicalSpeed() const;
127 float defaultDensity() const;
128 Q_REVISION(6, 5) float minimumTimestep() const;
129 Q_REVISION(6, 5) float maximumTimestep() const;
130
131 bool isNodeRemoved(QAbstractPhysicsNode *object);
132
133 static QPhysicsWorld *getWorld(QQuick3DNode *node);
134 static QPhysicsWorld *getWorld(QPhysicsJoint *joint);
135
136 static void registerNode(QAbstractPhysicsNode *physicsNode);
137 static void deregisterNode(QAbstractPhysicsNode *physicsNode);
139 static void registerJoint(QPhysicsJoint *joint);
140 static void deregisterJoint(QPhysicsJoint *joint);
142 // For a field's backend as it is created. It is released again when its
143 // field is deregistered.
144 void registerGravityField(QPhysXGravityField *field);
145
146 void registerContact(QAbstractPhysicsNode *sender, QAbstractPhysicsNode *receiver,
147 const QVector<QVector3D> &positions, const QVector<QVector3D> &impulses,
148 const QVector<QVector3D> &normals);
149
150 Q_REVISION(6, 5) QQuick3DNode *viewport() const;
151 void setHasIndividualDebugDraw();
152 physx::PxControllerManager *controllerManager();
153 Q_REVISION(6, 5) QQuick3DNode *scene() const;
154 Q_REVISION(6, 7) int numThreads() const;
155 Q_REVISION(6, 7) bool reportKinematicKinematicCollisions() const;
157 void setReportKinematicKinematicCollisions(bool newReportKinematicKinematicCollisions);
158 Q_REVISION(6, 7) bool reportStaticKinematicCollisions() const;
160 void setReportStaticKinematicCollisions(bool newReportStaticKinematicCollisions);
162 void cleanupRemovedJoints();
163
164 Q_REVISION(6, 13) QueryStructure staticQueryStructure() const;
165 Q_REVISION(6, 13) void setStaticQueryStructure(QueryStructure newStaticQueryStructure);
166
167 Q_REVISION(6, 13) QueryStructure dynamicQueryStructure() const;
168 Q_REVISION(6, 13) void setDynamicQueryStructure(QueryStructure newDynamicQueryStructure);
169
170 Q_REVISION(6, 13) Q_INVOKABLE bool testRaycastQuery(const QVector3D &origin,
171 const QVector3D &direction,
172 float maxDistance,
173 bool includeStatic = true,
174 bool includeDynamic = true) const;
175
176 Q_REVISION(6, 13) Q_INVOKABLE QQuick3DPhysicsLocationHit singleRaycastQuery(const QVector3D &origin,
177 const QVector3D &direction,
178 float maxDistance,
179 bool includeStatic = true,
180 bool includeDynamic = true) const;
181
182 Q_REVISION(6, 13) Q_INVOKABLE QList<QQuick3DPhysicsLocationHit> multiRaycastQuery(const QVector3D &origin,
183 const QVector3D &direction,
184 float maxDistance,
185 bool includeStatic = true,
186 bool includeDynamic = true) const;
187
188 Q_REVISION(6, 13) Q_INVOKABLE bool testSweepQuery(QAbstractCollisionShape *shape,
189 const QVector3D &direction,
190 float maxDistance,
191 bool includeStatic = true,
192 bool includeDynamic = true) const;
193
194 Q_REVISION(6, 13) Q_INVOKABLE QQuick3DPhysicsLocationHit singleSweepQuery(QAbstractCollisionShape *shape,
195 const QVector3D &direction,
196 float maxDistance,
197 bool includeStatic = true,
198 bool includeDynamic = true) const;
199
200 Q_REVISION(6, 13) Q_INVOKABLE QList<QQuick3DPhysicsLocationHit> multiSweepQuery(QAbstractCollisionShape *shape,
201 const QVector3D &direction,
202 float maxDistance,
203 bool includeStatic = true,
204 bool includeDynamic = true) const;
205
206 Q_REVISION(6, 13) Q_INVOKABLE bool testOverlapQuery(QAbstractCollisionShape *shape,
207 bool includeStatic = true,
208 bool includeDynamic = true) const;
209
210 Q_REVISION(6, 13) Q_INVOKABLE QList<QQuick3DPhysicsQueryHit> multiOverlapQuery(QAbstractCollisionShape *shape,
211 bool includeStatic = true,
212 bool includeDynamic = true) const;
214public slots:
215 void setGravity(QVector3D gravity);
216 void setRunning(bool running);
217 void setForceDebugDraw(bool forceDebugDraw);
218 void setEnableCCD(bool enableCCD);
219 void setTypicalLength(float typicalLength);
220 void setTypicalSpeed(float typicalSpeed);
221 void setDefaultDensity(float defaultDensity);
222 Q_REVISION(6, 5) void setViewport(QQuick3DNode *viewport);
223 Q_REVISION(6, 5) void setMinimumTimestep(float minTimestep);
224 Q_REVISION(6, 5) void setMaximumTimestep(float maxTimestep);
225 Q_REVISION(6, 5) void setScene(QQuick3DNode *newScene);
226 Q_REVISION(6, 7) void setNumThreads(int newNumThreads);
229 void gravityChanged(QVector3D gravity);
230 void runningChanged(bool running);
231 void enableCCDChanged(bool enableCCD);
232 void forceDebugDrawChanged(bool forceDebugDraw);
233 void typicalLengthChanged(float typicalLength);
234 void typicalSpeedChanged(float typicalSpeed);
235 void defaultDensityChanged(float defaultDensity);
236 Q_REVISION(6, 5) void viewportChanged(QQuick3DNode *viewport);
237 Q_REVISION(6, 5) void minimumTimestepChanged(float minimumTimestep);
238 Q_REVISION(6, 5) void maximumTimestepChanged(float maxTimestep);
239 Q_REVISION(6, 5) void frameDone(float timestep);
240 Q_REVISION(6, 5) void sceneChanged();
241 Q_REVISION(6, 7) void numThreadsChanged();
242 Q_REVISION(6, 7) void reportKinematicKinematicCollisionsChanged();
243 Q_REVISION(6, 7) void reportStaticKinematicCollisionsChanged();
244
245 Q_REVISION(6, 13) void staticQueryStructureChanged();
246 Q_REVISION(6, 13) void dynamicQueryStructureChanged();
247
248private:
249 static void registerRaycaster(QRaycaster *raycaster);
250 static void deregisterRaycaster(QRaycaster *raycaster);
251
252 QQuick3DPhysicsLocationHit
253 closestRaycastHit(const QVector3D &origin, const QVector3D &direction, float maxDistance,
254 bool includeStatic, bool includeDynamic,
255 QSpan<const QAbstractPhysicsNode *const> excludedBodies) const;
256
257 void simulateFrame();
258 void frameFinished(float deltaTime);
259 void frameFinishedDesignStudio();
260 void initPhysics();
261 void invalidateTriggerOverlaps(QSpan<QAbstractPhysXNode *const> rebuiltBodies);
262 void dropUnreportedTriggerOverlaps();
263 void cleanupRemovedNodes();
264 void updateDebugDraw();
265 void updateDebugDrawDesignStudio();
266 void setupDebugMaterials(QQuick3DNode *sceneNode);
267 void disableDebugDraw();
268 void matchOrphanNodes();
269 void matchOrphanJoints();
270 void matchOrphanRaycasters();
271 void applyGravity();
272 void releaseGravityField(QAbstractPhysXNode *backend);
273 void findPhysicsNodes();
274 void emitContactCallbacks();
275
276 struct BodyContact
277 {
278 QAbstractPhysicsNode *sender = nullptr;
279 QAbstractPhysicsNode *receiver = nullptr;
280 QVector<QVector3D> positions;
281 QVector<QVector3D> impulses;
282 QVector<QVector3D> normals;
283 };
284
285 struct DebugModelHolder
286 {
287 QQuick3DModel *model = nullptr;
288 QQuick3DGeometry *geometry = nullptr;
289 QVector3D data;
290 void *ptr = nullptr;
291
292 void releaseMeshPointer();
293
294 const QVector3D &halfExtents() const;
295 void setHalfExtents(const QVector3D &halfExtents);
296
297 float radius() const;
298 void setRadius(float radius);
299
300 float heightScale() const;
301 void setHeightScale(float heightScale);
302
303 float halfHeight() const;
304 void setHalfHeight(float halfHeight);
305
306 float rowScale() const;
307 void setRowScale(float rowScale);
308
309 float columnScale() const;
310 void setColumnScale(float columnScale);
311
312 physx::PxConvexMesh *getConvexMesh();
313 void setConvexMesh(physx::PxConvexMesh *mesh);
314
315 physx::PxTriangleMesh *getTriangleMesh();
316 void setTriangleMesh(physx::PxTriangleMesh *mesh);
317
318 physx::PxHeightField *getHeightField();
319 void setHeightField(physx::PxHeightField *hf);
320 };
321
322 QList<QAbstractPhysXNode *> m_physXBodies;
323 QList<QPhysicsJoint *> m_joints;
324 QList<QRaycaster *> m_raycasters;
325 QList<QPhysXGravityField *> m_gravityFields;
326 // The filters through which fields without shapes acted on the scene,
327 // until they were destroyed, got shapes, or changed their filter groups:
328 // the bodies they let through are to be woken, since they have lost a
329 // gravity they may have been resting under.
330 QList<QFilterData> m_staleGravityFilters;
331 // What fields with shapes acted on until they were destroyed.
332 QList<QPointer<QAbstractPhysicsNode>> m_gravityWakes;
333 QList<QAbstractPhysicsNode *> m_newPhysicsNodes;
334 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysicsNode *>, DebugModelHolder>
335 m_DesignStudioDebugModels;
336 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysXNode *>, DebugModelHolder>
337 m_collisionShapeDebugModels;
338 QSet<QAbstractPhysicsNode *> m_removedPhysicsNodes;
339 QSet<physx::PxJoint *> m_removedJoints;
340 // Whether any trigger is holding shape pairs that are waiting to be reported
341 // again after the shapes on one side of them were replaced.
342 bool m_triggerOverlapsInvalidated = false;
343 QList<BodyContact> m_registeredContacts;
344
345 QVector3D m_gravity = QVector3D(0.f, -981.f, 0.f);
346 float m_typicalLength = 100.f; // 100 cm
347 float m_typicalSpeed = 1000.f; // 1000 cm/s
348 float m_defaultDensity = 0.001f; // 1 g/cm^3
349 float m_minTimestep = 1.0f; // 1000 fps
350 float m_maxTimestep = 33.333f; // 30 fps
351
352 bool m_running = true;
353 bool m_forceDebugDraw = false;
354 // For performance, used to keep track if we have indiviually enabled debug drawing for any
355 // collision shape
356 bool m_hasIndividualDebugDraw = false;
357 bool m_physicsInitialized = false;
358 bool m_enableCCD = false;
359
360 QPhysXWorld *m_physx = nullptr;
361 QQuick3DNode *m_viewport = nullptr;
362 QVector<QQuick3DPrincipledMaterial *> m_debugMaterials;
363
364 friend class QQuick3DPhysicsMesh; // TODO: better internal API
365 friend class QTriangleMeshShape; //####
366 friend class QHeightFieldShape;
367 friend class QQuick3DPhysicsHeightField;
368 friend class SimulationEventCallback;
369 friend class ControllerCallback;
370 static physx::PxPhysics *getPhysics();
371 static const physx::PxCookingParams *getCookingParams();
372 FrameAnimator *m_frameAnimator = nullptr;
373 QQuick3DNode *m_scene = nullptr;
374 bool m_inDesignStudio = false;
375 int m_numThreads = -1;
376 bool m_reportKinematicKinematicCollisions = false;
377 bool m_reportStaticKinematicCollisions = false;
378 QElapsedTimer m_timer;
379 float m_currTimeStep = 0.f;
380 QList<float> m_frameTimings;
381 bool m_frameFetched = false;
382 QueryStructure m_staticQueryStructure = QueryStructure::DynamicTree;
383 QueryStructure m_dynamicQueryStructure = QueryStructure::DynamicTree;
384
385 friend class QRaycaster;
386};
387
389
390#endif // PHYSICSWORLD_H
Q_ALWAYS_INLINE bool fuzzyEquals(const physx::PxTransform &a, const physx::PxTransform &b)
const QQuaternion kMinus90YawRotation
Q_ALWAYS_INLINE bool isFinite(const QVector3D &v)
Q_ALWAYS_INLINE bool isFilteredOut(const QFilterData &a, const QFilterData &b)
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQuick3dPhysics)
#define QML_NAMED_ELEMENT(NAME)
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE
#define Q_ENUM(x)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_REVISION(...)
#define Q_INVOKABLE
#define Q_INTERFACES(x)
#define slots
#define signals
#define emit
quint32 group
quint32 ignoreGroups