| 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 | |
| 30 | namespace physx { |
| 31 | class PxMaterial; |
| 32 | class PxPhysics; |
| 33 | class PxShape; |
| 34 | class PxRigidDynamic; |
| 35 | class PxRigidActor; |
| 36 | class PxRigidStatic; |
| 37 | class PxCooking; |
| 38 | class PxControllerManager; |
| 39 | class PxConvexMesh; |
| 40 | class PxTriangleMesh; |
| 41 | class PxHeightField; |
| 42 | } |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | Q_DECLARE_LOGGING_CATEGORY(lcQuick3dPhysics); |
| 47 | |
| 48 | class QAbstractPhysicsNode; |
| 49 | class QAbstractCollisionShape; |
| 50 | class QAbstractRigidBody; |
| 51 | class QAbstractPhysXNode; |
| 52 | class QQuick3DModel; |
| 53 | class QQuick3DGeometry; |
| 54 | class QQuick3DDefaultMaterial; |
| 55 | class QPhysXWorld; |
| 56 | |
| 57 | class Q_QUICK3DPHYSICS_EXPORT QPhysicsWorld : public QObject, public QQmlParserStatus |
| 58 | { |
| 59 | Q_OBJECT |
| 60 | Q_INTERFACES(QQmlParserStatus) |
| 61 | Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged) |
| 62 | Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged) |
| 63 | Q_PROPERTY(bool forceDebugDraw READ forceDebugDraw WRITE setForceDebugDraw NOTIFY |
| 64 | forceDebugDrawChanged) |
| 65 | Q_PROPERTY(bool enableCCD READ enableCCD WRITE setEnableCCD NOTIFY enableCCDChanged) |
| 66 | Q_PROPERTY(float typicalLength READ typicalLength WRITE setTypicalLength NOTIFY |
| 67 | typicalLengthChanged) |
| 68 | Q_PROPERTY( |
| 69 | float typicalSpeed READ typicalSpeed WRITE setTypicalSpeed NOTIFY typicalSpeedChanged) |
| 70 | Q_PROPERTY(float defaultDensity READ defaultDensity WRITE setDefaultDensity NOTIFY |
| 71 | defaultDensityChanged) |
| 72 | Q_PROPERTY(QQuick3DNode *viewport READ viewport WRITE setViewport NOTIFY viewportChanged |
| 73 | REVISION(6, 5)) |
| 74 | Q_PROPERTY(float minimumTimestep READ minimumTimestep WRITE setMinimumTimestep NOTIFY |
| 75 | minimumTimestepChanged REVISION(6, 5)) |
| 76 | Q_PROPERTY(float maximumTimestep READ maximumTimestep WRITE setMaximumTimestep NOTIFY |
| 77 | maximumTimestepChanged REVISION(6, 5)) |
| 78 | Q_PROPERTY(QQuick3DNode *scene READ scene WRITE setScene NOTIFY sceneChanged REVISION(6, 5)) |
| 79 | Q_PROPERTY(int numThreads READ numThreads WRITE setNumThreads NOTIFY numThreadsChanged |
| 80 | REVISION(6, 7)) |
| 81 | Q_PROPERTY(bool reportKinematicKinematicCollisions READ reportKinematicKinematicCollisions WRITE |
| 82 | setReportKinematicKinematicCollisions NOTIFY |
| 83 | reportKinematicKinematicCollisionsChanged FINAL REVISION(6, 7)) |
| 84 | Q_PROPERTY(bool reportStaticKinematicCollisions READ reportStaticKinematicCollisions WRITE |
| 85 | setReportStaticKinematicCollisions NOTIFY |
| 86 | reportStaticKinematicCollisionsChanged FINAL REVISION(6, 7)) |
| 87 | |
| 88 | QML_NAMED_ELEMENT(PhysicsWorld) |
| 89 | |
| 90 | public: |
| 91 | explicit QPhysicsWorld(QObject *parent = nullptr); |
| 92 | ~QPhysicsWorld(); |
| 93 | |
| 94 | void classBegin() override; |
| 95 | void componentComplete() override; |
| 96 | |
| 97 | QVector3D gravity() const; |
| 98 | |
| 99 | bool running() const; |
| 100 | bool forceDebugDraw() const; |
| 101 | bool enableCCD() const; |
| 102 | float typicalLength() const; |
| 103 | float typicalSpeed() const; |
| 104 | float defaultDensity() const; |
| 105 | Q_REVISION(6, 5) float minimumTimestep() const; |
| 106 | Q_REVISION(6, 5) float maximumTimestep() const; |
| 107 | |
| 108 | bool isNodeRemoved(QAbstractPhysicsNode *object); |
| 109 | |
| 110 | static QPhysicsWorld *getWorld(QQuick3DNode *node); |
| 111 | |
| 112 | static void registerNode(QAbstractPhysicsNode *physicsNode); |
| 113 | static void deregisterNode(QAbstractPhysicsNode *physicsNode); |
| 114 | |
| 115 | void registerContact(QAbstractPhysicsNode *sender, QAbstractPhysicsNode *receiver, |
| 116 | const QVector<QVector3D> &positions, const QVector<QVector3D> &impulses, |
| 117 | const QVector<QVector3D> &normals); |
| 118 | |
| 119 | Q_REVISION(6, 5) QQuick3DNode *viewport() const; |
| 120 | void setHasIndividualDebugDraw(); |
| 121 | physx::PxControllerManager *controllerManager(); |
| 122 | Q_REVISION(6, 5) QQuick3DNode *scene() const; |
| 123 | Q_REVISION(6, 7) int numThreads() const; |
| 124 | Q_REVISION(6, 7) bool reportKinematicKinematicCollisions() const; |
| 125 | Q_REVISION(6, 7) |
| 126 | void setReportKinematicKinematicCollisions(bool newReportKinematicKinematicCollisions); |
| 127 | Q_REVISION(6, 7) bool reportStaticKinematicCollisions() const; |
| 128 | Q_REVISION(6, 7) |
| 129 | void setReportStaticKinematicCollisions(bool newReportStaticKinematicCollisions); |
| 130 | |
| 131 | public slots: |
| 132 | void setGravity(QVector3D gravity); |
| 133 | void setRunning(bool running); |
| 134 | void setForceDebugDraw(bool forceDebugDraw); |
| 135 | void setEnableCCD(bool enableCCD); |
| 136 | void setTypicalLength(float typicalLength); |
| 137 | void setTypicalSpeed(float typicalSpeed); |
| 138 | void setDefaultDensity(float defaultDensity); |
| 139 | Q_REVISION(6, 5) void setViewport(QQuick3DNode *viewport); |
| 140 | Q_REVISION(6, 5) void setMinimumTimestep(float minTimestep); |
| 141 | Q_REVISION(6, 5) void setMaximumTimestep(float maxTimestep); |
| 142 | Q_REVISION(6, 5) void setScene(QQuick3DNode *newScene); |
| 143 | Q_REVISION(6, 7) void setNumThreads(int newNumThreads); |
| 144 | |
| 145 | signals: |
| 146 | void gravityChanged(QVector3D gravity); |
| 147 | void runningChanged(bool running); |
| 148 | void enableCCDChanged(bool enableCCD); |
| 149 | void forceDebugDrawChanged(bool forceDebugDraw); |
| 150 | void typicalLengthChanged(float typicalLength); |
| 151 | void typicalSpeedChanged(float typicalSpeed); |
| 152 | void defaultDensityChanged(float defaultDensity); |
| 153 | Q_REVISION(6, 5) void viewportChanged(QQuick3DNode *viewport); |
| 154 | Q_REVISION(6, 5) void minimumTimestepChanged(float minimumTimestep); |
| 155 | Q_REVISION(6, 5) void maximumTimestepChanged(float maxTimestep); |
| 156 | void simulateFrame(float minTimestep, float maxTimestep); |
| 157 | Q_REVISION(6, 5) void frameDone(float timestep); |
| 158 | Q_REVISION(6, 5) void sceneChanged(); |
| 159 | Q_REVISION(6, 7) void numThreadsChanged(); |
| 160 | Q_REVISION(6, 7) void reportKinematicKinematicCollisionsChanged(); |
| 161 | Q_REVISION(6, 7) void reportStaticKinematicCollisionsChanged(); |
| 162 | |
| 163 | private: |
| 164 | void frameFinished(float deltaTime); |
| 165 | void frameFinishedDesignStudio(); |
| 166 | void initPhysics(); |
| 167 | void cleanupRemovedNodes(); |
| 168 | void updateDebugDraw(); |
| 169 | void updateDebugDrawDesignStudio(); |
| 170 | void setupDebugMaterials(QQuick3DNode *sceneNode); |
| 171 | void disableDebugDraw(); |
| 172 | void matchOrphanNodes(); |
| 173 | void findPhysicsNodes(); |
| 174 | void emitContactCallbacks(); |
| 175 | |
| 176 | struct BodyContact |
| 177 | { |
| 178 | QAbstractPhysicsNode *sender = nullptr; |
| 179 | QAbstractPhysicsNode *receiver = nullptr; |
| 180 | QVector<QVector3D> positions; |
| 181 | QVector<QVector3D> impulses; |
| 182 | QVector<QVector3D> normals; |
| 183 | }; |
| 184 | |
| 185 | struct DebugModelHolder |
| 186 | { |
| 187 | QQuick3DModel *model = nullptr; |
| 188 | QQuick3DGeometry *geometry = nullptr; |
| 189 | QVector3D data; |
| 190 | void *ptr = nullptr; |
| 191 | |
| 192 | void releaseMeshPointer(); |
| 193 | |
| 194 | const QVector3D &halfExtents() const; |
| 195 | void setHalfExtents(const QVector3D &halfExtents); |
| 196 | |
| 197 | float radius() const; |
| 198 | void setRadius(float radius); |
| 199 | |
| 200 | float heightScale() const; |
| 201 | void setHeightScale(float heightScale); |
| 202 | |
| 203 | float halfHeight() const; |
| 204 | void setHalfHeight(float halfHeight); |
| 205 | |
| 206 | float rowScale() const; |
| 207 | void setRowScale(float rowScale); |
| 208 | |
| 209 | float columnScale() const; |
| 210 | void setColumnScale(float columnScale); |
| 211 | |
| 212 | physx::PxConvexMesh *getConvexMesh(); |
| 213 | void setConvexMesh(physx::PxConvexMesh *mesh); |
| 214 | |
| 215 | physx::PxTriangleMesh *getTriangleMesh(); |
| 216 | void setTriangleMesh(physx::PxTriangleMesh *mesh); |
| 217 | |
| 218 | physx::PxHeightField *getHeightField(); |
| 219 | void setHeightField(physx::PxHeightField *hf); |
| 220 | }; |
| 221 | |
| 222 | QList<QAbstractPhysXNode *> m_physXBodies; |
| 223 | QList<QAbstractPhysicsNode *> m_newPhysicsNodes; |
| 224 | QHash<QPair<QAbstractCollisionShape *, QAbstractPhysicsNode *>, DebugModelHolder> |
| 225 | m_DesignStudioDebugModels; |
| 226 | QHash<QPair<QAbstractCollisionShape *, QAbstractPhysXNode *>, DebugModelHolder> |
| 227 | m_collisionShapeDebugModels; |
| 228 | QSet<QAbstractPhysicsNode *> m_removedPhysicsNodes; |
| 229 | QMutex m_removedPhysicsNodesMutex; |
| 230 | QList<BodyContact> m_registeredContacts; |
| 231 | |
| 232 | QVector3D m_gravity = QVector3D(0.f, -981.f, 0.f); |
| 233 | float m_typicalLength = 100.f; // 100 cm |
| 234 | float m_typicalSpeed = 1000.f; // 1000 cm/s |
| 235 | float m_defaultDensity = 0.001f; // 1 g/cm^3 |
| 236 | float m_minTimestep = 16.667f; // 60 fps |
| 237 | float m_maxTimestep = 33.333f; // 30 fps |
| 238 | |
| 239 | bool m_running = true; |
| 240 | bool m_forceDebugDraw = false; |
| 241 | // For performance, used to keep track if we have indiviually enabled debug drawing for any |
| 242 | // collision shape |
| 243 | bool m_hasIndividualDebugDraw = false; |
| 244 | bool m_physicsInitialized = false; |
| 245 | bool m_enableCCD = false; |
| 246 | |
| 247 | QPhysXWorld *m_physx = nullptr; |
| 248 | QQuick3DNode *m_viewport = nullptr; |
| 249 | QVector<QQuick3DDefaultMaterial *> m_debugMaterials; |
| 250 | |
| 251 | friend class QQuick3DPhysicsMesh; // TODO: better internal API |
| 252 | friend class QTriangleMeshShape; //#### |
| 253 | friend class QHeightFieldShape; |
| 254 | friend class QQuick3DPhysicsHeightField; |
| 255 | friend class SimulationEventCallback; |
| 256 | friend class ControllerCallback; |
| 257 | static physx::PxPhysics *getPhysics(); |
| 258 | static physx::PxCooking *getCooking(); |
| 259 | QThread m_workerThread; |
| 260 | QQuick3DNode *m_scene = nullptr; |
| 261 | bool m_inDesignStudio = false; |
| 262 | int m_numThreads = -1; |
| 263 | bool m_reportKinematicKinematicCollisions = false; |
| 264 | bool m_reportStaticKinematicCollisions = false; |
| 265 | }; |
| 266 | |
| 267 | QT_END_NAMESPACE |
| 268 | |
| 269 | #endif // PHYSICSWORLD_H |
| 270 | |