61class Q_QUICK3DPHYSICS_EXPORT
QPhysicsWorld :
public QObject,
public QQmlParserStatus
65 Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
66 Q_PROPERTY(
bool running READ running WRITE setRunning NOTIFY runningChanged)
67 Q_PROPERTY(
bool forceDebugDraw READ forceDebugDraw WRITE setForceDebugDraw NOTIFY
68 forceDebugDrawChanged)
69 Q_PROPERTY(
bool enableCCD READ enableCCD WRITE setEnableCCD NOTIFY enableCCDChanged)
70 Q_PROPERTY(
float typicalLength READ typicalLength WRITE setTypicalLength NOTIFY
73 float typicalSpeed READ typicalSpeed WRITE setTypicalSpeed NOTIFY typicalSpeedChanged)
74 Q_PROPERTY(
float defaultDensity READ defaultDensity WRITE setDefaultDensity NOTIFY
75 defaultDensityChanged)
76 Q_PROPERTY(QQuick3DNode *viewport READ viewport WRITE setViewport NOTIFY viewportChanged
78 Q_PROPERTY(
float minimumTimestep READ minimumTimestep WRITE setMinimumTimestep NOTIFY
79 minimumTimestepChanged REVISION(6, 5))
80 Q_PROPERTY(
float maximumTimestep READ maximumTimestep WRITE setMaximumTimestep NOTIFY
81 maximumTimestepChanged REVISION(6, 5))
82 Q_PROPERTY(QQuick3DNode *scene READ scene WRITE setScene NOTIFY sceneChanged REVISION(6, 5))
83 Q_PROPERTY(
int numThreads READ numThreads WRITE setNumThreads NOTIFY numThreadsChanged
85 Q_PROPERTY(
bool reportKinematicKinematicCollisions READ reportKinematicKinematicCollisions WRITE
86 setReportKinematicKinematicCollisions NOTIFY
87 reportKinematicKinematicCollisionsChanged FINAL REVISION(6, 7))
88 Q_PROPERTY(
bool reportStaticKinematicCollisions READ reportStaticKinematicCollisions WRITE
89 setReportStaticKinematicCollisions NOTIFY
90 reportStaticKinematicCollisionsChanged FINAL REVISION(6, 7))
92 Q_PROPERTY(QueryStructure staticQueryStructure READ staticQueryStructure WRITE
93 setStaticQueryStructure NOTIFY staticQueryStructureChanged FINAL REVISION(6, 13))
95 Q_PROPERTY(QueryStructure dynamicQueryStructure READ dynamicQueryStructure WRITE
96 setDynamicQueryStructure NOTIFY dynamicQueryStructureChanged FINAL REVISION(6, 13))
101 enum class QueryStructure {
108 explicit QPhysicsWorld(QObject *parent =
nullptr);
111 void classBegin() override;
112 void componentComplete() override;
114 QVector3D gravity()
const;
116 bool running()
const;
117 bool forceDebugDraw()
const;
118 bool enableCCD()
const;
119 float typicalLength()
const;
120 float typicalSpeed()
const;
121 float defaultDensity()
const;
122 Q_REVISION(6, 5)
float minimumTimestep()
const;
123 Q_REVISION(6, 5)
float maximumTimestep()
const;
125 bool isNodeRemoved(QAbstractPhysicsNode *object);
127 static QPhysicsWorld *getWorld(QQuick3DNode *node);
128 static QPhysicsWorld *getWorld(QPhysicsJoint *joint);
130 static void registerNode(QAbstractPhysicsNode *physicsNode);
131 static void deregisterNode(QAbstractPhysicsNode *physicsNode);
133 static void registerJoint(QPhysicsJoint *joint);
134 static void deregisterJoint(QPhysicsJoint *joint);
136 void registerContact(QAbstractPhysicsNode *sender, QAbstractPhysicsNode *receiver,
137 const QVector<QVector3D> &positions,
const QVector<QVector3D> &impulses,
138 const QVector<QVector3D> &normals);
140 Q_REVISION(6, 5) QQuick3DNode *viewport()
const;
141 void setHasIndividualDebugDraw();
142 physx::PxControllerManager *controllerManager();
145 Q_REVISION(6, 7)
bool reportKinematicKinematicCollisions()
const;
147 void setReportKinematicKinematicCollisions(
bool newReportKinematicKinematicCollisions);
148 Q_REVISION(6, 7)
bool reportStaticKinematicCollisions()
const;
150 void setReportStaticKinematicCollisions(
bool newReportStaticKinematicCollisions);
152 void cleanupRemovedJoints();
154 Q_REVISION(6, 13) QueryStructure staticQueryStructure()
const;
155 Q_REVISION(6, 13)
void setStaticQueryStructure(QueryStructure newStaticQueryStructure);
157 Q_REVISION(6, 13) QueryStructure dynamicQueryStructure()
const;
158 Q_REVISION(6, 13)
void setDynamicQueryStructure(QueryStructure newDynamicQueryStructure);
161 void setGravity(QVector3D gravity);
162 void setRunning(
bool running);
163 void setForceDebugDraw(
bool forceDebugDraw);
164 void setEnableCCD(
bool enableCCD);
165 void setTypicalLength(
float typicalLength);
166 void setTypicalSpeed(
float typicalSpeed);
167 void setDefaultDensity(
float defaultDensity);
175 void gravityChanged(QVector3D gravity);
176 void runningChanged(
bool running);
177 void enableCCDChanged(
bool enableCCD);
178 void forceDebugDrawChanged(
bool forceDebugDraw);
179 void typicalLengthChanged(
float typicalLength);
180 void typicalSpeedChanged(
float typicalSpeed);
181 void defaultDensityChanged(
float defaultDensity);
182 Q_REVISION(6, 5)
void viewportChanged(QQuick3DNode *viewport);
183 Q_REVISION(6, 5)
void minimumTimestepChanged(
float minimumTimestep);
184 Q_REVISION(6, 5)
void maximumTimestepChanged(
float maxTimestep);
185 Q_REVISION(6, 5)
void frameDone(
float timestep);
188 Q_REVISION(6, 7)
void reportKinematicKinematicCollisionsChanged();
189 Q_REVISION(6, 7)
void reportStaticKinematicCollisionsChanged();
192 Q_REVISION(6, 13)
void staticQueryStructureChanged();
193 Q_REVISION(6, 13)
void dynamicQueryStructureChanged();
196 void simulateFrame();
197 void frameFinished(
float deltaTime);
198 void frameFinishedDesignStudio();
200 void cleanupRemovedNodes();
201 void updateDebugDraw();
202 void updateDebugDrawDesignStudio();
203 void setupDebugMaterials(QQuick3DNode *sceneNode);
204 void disableDebugDraw();
205 void matchOrphanNodes();
206 void matchOrphanJoints();
207 void findPhysicsNodes();
208 void emitContactCallbacks();
212 QAbstractPhysicsNode *sender =
nullptr;
213 QAbstractPhysicsNode *receiver =
nullptr;
214 QVector<QVector3D> positions;
215 QVector<QVector3D> impulses;
216 QVector<QVector3D> normals;
219 struct DebugModelHolder
221 QQuick3DModel *model =
nullptr;
222 QQuick3DGeometry *geometry =
nullptr;
226 void releaseMeshPointer();
228 const QVector3D &halfExtents()
const;
229 void setHalfExtents(
const QVector3D &halfExtents);
231 float radius()
const;
232 void setRadius(
float radius);
234 float heightScale()
const;
235 void setHeightScale(
float heightScale);
237 float halfHeight()
const;
238 void setHalfHeight(
float halfHeight);
240 float rowScale()
const;
241 void setRowScale(
float rowScale);
243 float columnScale()
const;
244 void setColumnScale(
float columnScale);
246 physx::PxConvexMesh *getConvexMesh();
247 void setConvexMesh(physx::PxConvexMesh *mesh);
249 physx::PxTriangleMesh *getTriangleMesh();
250 void setTriangleMesh(physx::PxTriangleMesh *mesh);
252 physx::PxHeightField *getHeightField();
253 void setHeightField(physx::PxHeightField *hf);
256 QList<QAbstractPhysXNode *> m_physXBodies;
257 QList<QPhysicsJoint *> m_joints;
258 QList<QAbstractPhysicsNode *> m_newPhysicsNodes;
259 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysicsNode *>, DebugModelHolder>
260 m_DesignStudioDebugModels;
261 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysXNode *>, DebugModelHolder>
262 m_collisionShapeDebugModels;
263 QSet<QAbstractPhysicsNode *> m_removedPhysicsNodes;
264 QSet<physx::PxJoint *> m_removedJoints;
265 QMutex m_removedPhysicsNodesMutex;
266 QList<BodyContact> m_registeredContacts;
268 QVector3D m_gravity = QVector3D(0.f, -981.f, 0.f);
269 float m_typicalLength = 100.f;
270 float m_typicalSpeed = 1000.f;
271 float m_defaultDensity = 0.001f;
272 float m_minTimestep = 1.0f;
273 float m_maxTimestep = 33.333f;
275 bool m_running =
true;
276 bool m_forceDebugDraw =
false;
279 bool m_hasIndividualDebugDraw =
false;
280 bool m_physicsInitialized =
false;
281 bool m_enableCCD =
false;
283 QPhysXWorld *m_physx =
nullptr;
284 QQuick3DNode *m_viewport =
nullptr;
285 QVector<QQuick3DPrincipledMaterial *> m_debugMaterials;
287 friend class QQuick3DPhysicsMesh;
288 friend class QTriangleMeshShape;
289 friend class QHeightFieldShape;
290 friend class QQuick3DPhysicsHeightField;
291 friend class SimulationEventCallback;
292 friend class ControllerCallback;
293 static physx::PxPhysics *getPhysics();
294 static physx::PxCooking *getCooking();
295 FrameAnimator *m_frameAnimator =
nullptr;
296 QQuick3DNode *m_scene =
nullptr;
297 bool m_inDesignStudio =
false;
298 int m_numThreads = -1;
299 bool m_reportKinematicKinematicCollisions =
false;
300 bool m_reportStaticKinematicCollisions =
false;
301 QElapsedTimer m_timer;
302 float m_currTimeStep = 0.f;
303 QList<
float> m_frameTimings;
304 bool m_frameFetched =
false;
305 QueryStructure m_staticQueryStructure = QueryStructure::DynamicTree;
306 QueryStructure m_dynamicQueryStructure = QueryStructure::DynamicTree;