63class Q_QUICK3DPHYSICS_EXPORT
QPhysicsWorld :
public QObject,
public QQmlParserStatus
67 Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
68 Q_PROPERTY(
bool running READ running WRITE setRunning NOTIFY runningChanged)
69 Q_PROPERTY(
bool forceDebugDraw READ forceDebugDraw WRITE setForceDebugDraw NOTIFY
70 forceDebugDrawChanged)
71 Q_PROPERTY(
bool enableCCD READ enableCCD WRITE setEnableCCD NOTIFY enableCCDChanged)
72 Q_PROPERTY(
float typicalLength READ typicalLength WRITE setTypicalLength NOTIFY
75 float typicalSpeed READ typicalSpeed WRITE setTypicalSpeed NOTIFY typicalSpeedChanged)
76 Q_PROPERTY(
float defaultDensity READ defaultDensity WRITE setDefaultDensity NOTIFY
77 defaultDensityChanged)
78 Q_PROPERTY(QQuick3DNode *viewport READ viewport WRITE setViewport NOTIFY viewportChanged
80 Q_PROPERTY(
float minimumTimestep READ minimumTimestep WRITE setMinimumTimestep NOTIFY
81 minimumTimestepChanged REVISION(6, 5))
82 Q_PROPERTY(
float maximumTimestep READ maximumTimestep WRITE setMaximumTimestep NOTIFY
83 maximumTimestepChanged REVISION(6, 5))
84 Q_PROPERTY(QQuick3DNode *scene READ scene WRITE setScene NOTIFY sceneChanged REVISION(6, 5))
85 Q_PROPERTY(
int numThreads READ numThreads WRITE setNumThreads NOTIFY numThreadsChanged
87 Q_PROPERTY(
bool reportKinematicKinematicCollisions READ reportKinematicKinematicCollisions WRITE
88 setReportKinematicKinematicCollisions NOTIFY
89 reportKinematicKinematicCollisionsChanged FINAL REVISION(6, 7))
90 Q_PROPERTY(
bool reportStaticKinematicCollisions READ reportStaticKinematicCollisions WRITE
91 setReportStaticKinematicCollisions NOTIFY
92 reportStaticKinematicCollisionsChanged FINAL REVISION(6, 7))
94 Q_PROPERTY(QueryStructure staticQueryStructure READ staticQueryStructure WRITE
95 setStaticQueryStructure NOTIFY staticQueryStructureChanged FINAL REVISION(6, 13))
97 Q_PROPERTY(QueryStructure dynamicQueryStructure READ dynamicQueryStructure WRITE
98 setDynamicQueryStructure NOTIFY dynamicQueryStructureChanged FINAL REVISION(6, 13))
103 enum class QueryStructure {
110 explicit QPhysicsWorld(QObject *parent =
nullptr);
113 void classBegin() override;
114 void componentComplete() override;
116 QVector3D gravity()
const;
118 bool running()
const;
119 bool forceDebugDraw()
const;
120 bool enableCCD()
const;
121 float typicalLength()
const;
122 float typicalSpeed()
const;
123 float defaultDensity()
const;
124 Q_REVISION(6, 5)
float minimumTimestep()
const;
125 Q_REVISION(6, 5)
float maximumTimestep()
const;
127 bool isNodeRemoved(QAbstractPhysicsNode *object);
129 static QPhysicsWorld *getWorld(QQuick3DNode *node);
130 static QPhysicsWorld *getWorld(QPhysicsJoint *joint);
132 static void registerNode(QAbstractPhysicsNode *physicsNode);
133 static void deregisterNode(QAbstractPhysicsNode *physicsNode);
135 static void registerJoint(QPhysicsJoint *joint);
136 static void deregisterJoint(QPhysicsJoint *joint);
138 void registerContact(QAbstractPhysicsNode *sender, QAbstractPhysicsNode *receiver,
139 const QVector<QVector3D> &positions,
const QVector<QVector3D> &impulses,
140 const QVector<QVector3D> &normals);
142 Q_REVISION(6, 5) QQuick3DNode *viewport()
const;
143 void setHasIndividualDebugDraw();
144 physx::PxControllerManager *controllerManager();
147 Q_REVISION(6, 7)
bool reportKinematicKinematicCollisions()
const;
149 void setReportKinematicKinematicCollisions(
bool newReportKinematicKinematicCollisions);
150 Q_REVISION(6, 7)
bool reportStaticKinematicCollisions()
const;
152 void setReportStaticKinematicCollisions(
bool newReportStaticKinematicCollisions);
154 void cleanupRemovedJoints();
156 Q_REVISION(6, 13) QueryStructure staticQueryStructure()
const;
157 Q_REVISION(6, 13)
void setStaticQueryStructure(QueryStructure newStaticQueryStructure);
159 Q_REVISION(6, 13) QueryStructure dynamicQueryStructure()
const;
160 Q_REVISION(6, 13)
void setDynamicQueryStructure(QueryStructure newDynamicQueryStructure);
163 const QVector3D &direction,
165 bool includeStatic =
true,
166 bool includeDynamic =
true)
const;
169 const QVector3D &direction,
171 bool includeStatic =
true,
172 bool includeDynamic =
true)
const;
175 const QVector3D &direction,
177 bool includeStatic =
true,
178 bool includeDynamic =
true)
const;
181 const QVector3D &direction,
183 bool includeStatic =
true,
184 bool includeDynamic =
true)
const;
187 const QVector3D &direction,
189 bool includeStatic =
true,
190 bool includeDynamic =
true)
const;
192 Q_REVISION(6, 13)
Q_INVOKABLE QList<QQuick3DPhysicsLocationHit> multiSweepQuery(QAbstractCollisionShape *shape,
193 const QVector3D &direction,
195 bool includeStatic =
true,
196 bool includeDynamic =
true)
const;
199 bool includeStatic =
true,
200 bool includeDynamic =
true)
const;
203 bool includeStatic =
true,
204 bool includeDynamic =
true)
const;
207 void setGravity(QVector3D gravity);
208 void setRunning(
bool running);
209 void setForceDebugDraw(
bool forceDebugDraw);
210 void setEnableCCD(
bool enableCCD);
211 void setTypicalLength(
float typicalLength);
212 void setTypicalSpeed(
float typicalSpeed);
213 void setDefaultDensity(
float defaultDensity);
221 void gravityChanged(QVector3D gravity);
222 void runningChanged(
bool running);
223 void enableCCDChanged(
bool enableCCD);
224 void forceDebugDrawChanged(
bool forceDebugDraw);
225 void typicalLengthChanged(
float typicalLength);
226 void typicalSpeedChanged(
float typicalSpeed);
227 void defaultDensityChanged(
float defaultDensity);
228 Q_REVISION(6, 5)
void viewportChanged(QQuick3DNode *viewport);
229 Q_REVISION(6, 5)
void minimumTimestepChanged(
float minimumTimestep);
230 Q_REVISION(6, 5)
void maximumTimestepChanged(
float maxTimestep);
231 Q_REVISION(6, 5)
void frameDone(
float timestep);
234 Q_REVISION(6, 7)
void reportKinematicKinematicCollisionsChanged();
235 Q_REVISION(6, 7)
void reportStaticKinematicCollisionsChanged();
237 Q_REVISION(6, 13)
void staticQueryStructureChanged();
238 Q_REVISION(6, 13)
void dynamicQueryStructureChanged();
241 void simulateFrame();
242 void frameFinished(
float deltaTime);
243 void frameFinishedDesignStudio();
245 void invalidateTriggerOverlaps(QSpan<QAbstractPhysXNode *
const> rebuiltBodies);
246 void dropUnreportedTriggerOverlaps();
247 void cleanupRemovedNodes();
248 void updateDebugDraw();
249 void updateDebugDrawDesignStudio();
250 void setupDebugMaterials(QQuick3DNode *sceneNode);
251 void disableDebugDraw();
252 void matchOrphanNodes();
253 void matchOrphanJoints();
254 void findPhysicsNodes();
255 void emitContactCallbacks();
259 QAbstractPhysicsNode *sender =
nullptr;
260 QAbstractPhysicsNode *receiver =
nullptr;
261 QVector<QVector3D> positions;
262 QVector<QVector3D> impulses;
263 QVector<QVector3D> normals;
266 struct DebugModelHolder
268 QQuick3DModel *model =
nullptr;
269 QQuick3DGeometry *geometry =
nullptr;
273 void releaseMeshPointer();
275 const QVector3D &halfExtents()
const;
276 void setHalfExtents(
const QVector3D &halfExtents);
278 float radius()
const;
279 void setRadius(
float radius);
281 float heightScale()
const;
282 void setHeightScale(
float heightScale);
284 float halfHeight()
const;
285 void setHalfHeight(
float halfHeight);
287 float rowScale()
const;
288 void setRowScale(
float rowScale);
290 float columnScale()
const;
291 void setColumnScale(
float columnScale);
293 physx::PxConvexMesh *getConvexMesh();
294 void setConvexMesh(physx::PxConvexMesh *mesh);
296 physx::PxTriangleMesh *getTriangleMesh();
297 void setTriangleMesh(physx::PxTriangleMesh *mesh);
299 physx::PxHeightField *getHeightField();
300 void setHeightField(physx::PxHeightField *hf);
303 QList<QAbstractPhysXNode *> m_physXBodies;
304 QList<QPhysicsJoint *> m_joints;
305 QList<QAbstractPhysicsNode *> m_newPhysicsNodes;
306 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysicsNode *>, DebugModelHolder>
307 m_DesignStudioDebugModels;
308 QHash<QPair<QAbstractCollisionShape *, QAbstractPhysXNode *>, DebugModelHolder>
309 m_collisionShapeDebugModels;
310 QSet<QAbstractPhysicsNode *> m_removedPhysicsNodes;
311 QSet<physx::PxJoint *> m_removedJoints;
314 bool m_triggerOverlapsInvalidated =
false;
315 QList<BodyContact> m_registeredContacts;
317 QVector3D m_gravity = QVector3D(0.f, -981.f, 0.f);
318 float m_typicalLength = 100.f;
319 float m_typicalSpeed = 1000.f;
320 float m_defaultDensity = 0.001f;
321 float m_minTimestep = 1.0f;
322 float m_maxTimestep = 33.333f;
324 bool m_running =
true;
325 bool m_forceDebugDraw =
false;
328 bool m_hasIndividualDebugDraw =
false;
329 bool m_physicsInitialized =
false;
330 bool m_enableCCD =
false;
332 QPhysXWorld *m_physx =
nullptr;
333 QQuick3DNode *m_viewport =
nullptr;
334 QVector<QQuick3DPrincipledMaterial *> m_debugMaterials;
336 friend class QQuick3DPhysicsMesh;
337 friend class QTriangleMeshShape;
338 friend class QHeightFieldShape;
339 friend class QQuick3DPhysicsHeightField;
340 friend class SimulationEventCallback;
341 friend class ControllerCallback;
342 static physx::PxPhysics *getPhysics();
343 static const physx::PxCookingParams *getCookingParams();
344 FrameAnimator *m_frameAnimator =
nullptr;
345 QQuick3DNode *m_scene =
nullptr;
346 bool m_inDesignStudio =
false;
347 int m_numThreads = -1;
348 bool m_reportKinematicKinematicCollisions =
false;
349 bool m_reportStaticKinematicCollisions =
false;
350 QElapsedTimer m_timer;
351 float m_currTimeStep = 0.f;
352 QList<
float> m_frameTimings;
353 bool m_frameFetched =
false;
354 QueryStructure m_staticQueryStructure = QueryStructure::DynamicTree;
355 QueryStructure m_dynamicQueryStructure = QueryStructure::DynamicTree;