Qt 6.x
The Qt SDK
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
4#ifndef PHYSICSWORLD_H
5#define PHYSICSWORLD_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick3DPhysics/qtquick3dphysicsglobal.h>
19
20#include <QtCore/QLoggingCategory>
21#include <QtCore/QObject>
22#include <QtCore/QTimerEvent>
23#include <QtCore/QElapsedTimer>
24#include <QtGui/QVector3D>
25#include <QtQml/qqml.h>
26#include <QBasicTimer>
27
28#include <QtQuick3D/private/qquick3dviewport_p.h>
29
30namespace physx {
31class PxMaterial;
32class PxPhysics;
33class PxShape;
34class PxRigidDynamic;
35class PxRigidActor;
36class PxRigidStatic;
37class PxCooking;
38class PxControllerManager;
39}
40
42
44
47class QAbstractRigidBody;
49class QQuick3DModel;
51struct PhysXWorld;
52
54{
57 Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
58 Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged)
59 Q_PROPERTY(bool forceDebugDraw READ forceDebugDraw WRITE setForceDebugDraw NOTIFY
60 forceDebugDrawChanged)
61 Q_PROPERTY(bool enableCCD READ enableCCD WRITE setEnableCCD NOTIFY enableCCDChanged)
62 Q_PROPERTY(float typicalLength READ typicalLength WRITE setTypicalLength NOTIFY
63 typicalLengthChanged)
65 float typicalSpeed READ typicalSpeed WRITE setTypicalSpeed NOTIFY typicalSpeedChanged)
66 Q_PROPERTY(float defaultDensity READ defaultDensity WRITE setDefaultDensity NOTIFY
67 defaultDensityChanged)
68 Q_PROPERTY(QQuick3DNode *viewport READ viewport WRITE setViewport NOTIFY viewportChanged
69 REVISION(6, 5))
70 Q_PROPERTY(float minimumTimestep READ minimumTimestep WRITE setMinimumTimestep NOTIFY
71 minimumTimestepChanged REVISION(6, 5))
72 Q_PROPERTY(float maximumTimestep READ maximumTimestep WRITE setMaximumTimestep NOTIFY
73 maximumTimestepChanged REVISION(6, 5))
74 Q_PROPERTY(QQuick3DNode *scene READ scene WRITE setScene NOTIFY sceneChanged REVISION(6, 5))
75
76 QML_NAMED_ELEMENT(PhysicsWorld)
77
78public:
79 explicit QPhysicsWorld(QObject *parent = nullptr);
81
82 void classBegin() override;
83 void componentComplete() override;
84
85 QVector3D gravity() const;
86
87 bool running() const;
88 bool forceDebugDraw() const;
89 bool enableCCD() const;
90 float typicalLength() const;
91 float typicalSpeed() const;
92 float defaultDensity() const;
93 Q_REVISION(6, 5) float minimumTimestep() const;
94 Q_REVISION(6, 5) float maximumTimestep() const;
95
96 bool isNodeRemoved(QAbstractPhysicsNode *object);
97
98 static QPhysicsWorld *getWorld(QQuick3DNode *node);
99
100 static void registerNode(QAbstractPhysicsNode *physicsNode);
101 static void deregisterNode(QAbstractPhysicsNode *physicsNode);
102
104 void setHasIndividualDebugDraw();
105 physx::PxControllerManager *controllerManager();
107
108public slots:
109 void setGravity(QVector3D gravity);
110 void setRunning(bool running);
111 void setForceDebugDraw(bool forceDebugDraw);
112 void setEnableCCD(bool enableCCD);
113 void setTypicalLength(float typicalLength);
114 void setTypicalSpeed(float typicalSpeed);
115 void setDefaultDensity(float defaultDensity);
116 Q_REVISION(6, 5) void setViewport(QQuick3DNode *viewport);
117 Q_REVISION(6, 5) void setMinimumTimestep(float minTimestep);
118 Q_REVISION(6, 5) void setMaximumTimestep(float maxTimestep);
119 Q_REVISION(6, 5) void setScene(QQuick3DNode *newScene);
120
121signals:
122 void gravityChanged(QVector3D gravity);
123 void runningChanged(bool running);
124 void enableCCDChanged(bool enableCCD);
125 void forceDebugDrawChanged(bool forceDebugDraw);
126 void typicalLengthChanged(float typicalLength);
127 void typicalSpeedChanged(float typicalSpeed);
128 void defaultDensityChanged(float defaultDensity);
129 Q_REVISION(6, 5) void viewportChanged(QQuick3DNode *viewport);
130 Q_REVISION(6, 5) void minimumTimestepChanged(float minimumTimestep);
131 Q_REVISION(6, 5) void maximumTimestepChanged(float maxTimestep);
132 void simulateFrame(float minTimestep, float maxTimestep);
133 Q_REVISION(6, 5) void frameDone(float timestep);
134 Q_REVISION(6, 5) void sceneChanged();
135
136private:
137 void frameFinished(float deltaTime);
138 void initPhysics();
139 void cleanupRemovedNodes();
140 void updateDebugDraw();
141 void disableDebugDraw();
142 void matchOrphanNodes();
143 void findPhysicsNodes();
144
145 struct DebugModelHolder
146 {
147 QQuick3DModel *model = nullptr;
149
150 const QVector3D &halfExtents() const { return data; }
151 void setHalfExtents(const QVector3D &halfExtents) { data = halfExtents; }
152
153 float radius() const { return data.x(); }
154 void setRadius(float radius) { data.setX(radius); }
155
156 float heightScale() const { return data.x(); }
157 void setHeightScale(float heightScale) { data.setX(heightScale); }
158
159 float halfHeight() const { return data.y(); }
160 void setHalfHeight(float halfHeight) { data.setY(halfHeight); }
161
162 float rowScale() const { return data.y(); }
163 void setRowScale(float rowScale) { data.setY(rowScale); }
164
165 float columnScale() const { return data.z(); }
166 void setColumnScale(float columnScale) { data.setZ(columnScale); }
167 };
168
169 QList<QAbstractPhysXNode *> m_physXBodies;
170 QList<QAbstractPhysicsNode *> m_newPhysicsNodes;
172 m_collisionShapeDebugModels;
173 QSet<QAbstractPhysicsNode *> m_removedPhysicsNodes;
174 QMutex m_removedPhysicsNodesMutex;
175
176 QVector3D m_gravity = QVector3D(0.f, -981.f, 0.f);
177 float m_typicalLength = 100.f; // 100 cm
178 float m_typicalSpeed = 1000.f; // 1000 cm/s
179 float m_defaultDensity = 0.001f; // 1 g/cm^3
180 float m_minTimestep = 16.667f; // 60 fps
181 float m_maxTimestep = 33.333f; // 30 fps
182
183 bool m_running = true;
184 bool m_forceDebugDraw = false;
185 // For performance, used to keep track if we have indiviually enabled debug drawing for any
186 // collision shape
187 bool m_hasIndividualDebugDraw = false;
188 bool m_physicsInitialized = false;
189 bool m_enableCCD = false;
190
191 PhysXWorld *m_physx = nullptr;
192 QQuick3DNode *m_viewport = nullptr;
193 QVector<QQuick3DDefaultMaterial *> m_debugMaterials;
194
195 friend class QQuick3DPhysicsMesh; // TODO: better internal API
196 friend class QTriangleMeshShape; //####
197 friend class QHeightFieldShape;
200 friend class ControllerCallback;
201 static physx::PxPhysics *getPhysics();
202 static physx::PxCooking *getCooking();
203 QThread m_workerThread;
204 QQuick3DNode *m_scene = nullptr;
205};
206
208
209#endif // PHYSICSWORLD_H
\inmodule QtCore
Definition qhash.h:818
Definition qlist.h:74
\inmodule QtCore
Definition qmutex.h:285
\inmodule QtCore
Definition qobject.h:90
Q_REVISION(6, 5) float minimumTimestep() const
The QQmlParserStatus class provides updates on the QML parser state.
virtual void classBegin()=0
Invoked after class creation, but before any properties have been set.
virtual void componentComplete()=0
Invoked after the root component that caused this instantiation has completed construction.
Definition qset.h:18
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:670
Combined button and popup list for selecting options.
static bool m_running
static Q_CONSTINIT QBasicAtomicInt running
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define QML_NAMED_ELEMENT(NAME)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_REVISION(...)
#define Q_INTERFACES(x)
#define slots
#define signals
#define Q_QUICK3DPHYSICS_EXPORT
QSqlQueryModel * model
[16]
QGraphicsScene scene
[0]
view viewport() -> scroll(dx, dy, deviceRect)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent