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
qgravityfield_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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 GRAVITYFIELD_H
6#define GRAVITYFIELD_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>
22
23#include <QtCore/QPointer>
24#include <QtGui/qvector3d.h>
25#include <QtQml/QQmlEngine>
26
28
30
31// A gravity of its own. With collision shapes it acts on the bodies inside
32// them, which PhysX finds as it does for a TriggerBody; with none it acts on
33// every body in the scene, which the world walks.
34class Q_QUICK3DPHYSICS_EXPORT QGravityField : public QAbstractTriggerRegion
35{
36 Q_OBJECT
37 Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged FINAL REVISION(6, 13))
38 Q_PROPERTY(QVector3D direction READ direction WRITE setDirection NOTIFY directionChanged
39 FINAL REVISION(6, 13))
40 Q_PROPERTY(float gravity READ gravity WRITE setGravity NOTIFY gravityChanged FINAL
41 REVISION(6, 13))
42 Q_PROPERTY(float unitDistance READ unitDistance WRITE setUnitDistance NOTIFY
43 unitDistanceChanged FINAL REVISION(6, 13))
44 QML_NAMED_ELEMENT(GravityField)
45 QML_ADDED_IN_VERSION(6, 13)
46public:
47 enum class Mode {
48 Directional,
49 Point,
50 };
51 Q_ENUM(Mode)
52
53 QGravityField();
54 ~QGravityField() override;
55
56 Q_REVISION(6, 13) Mode mode() const;
57 Q_REVISION(6, 13) void setMode(Mode mode);
58
59 Q_REVISION(6, 13) const QVector3D &direction() const;
60 Q_REVISION(6, 13) void setDirection(const QVector3D &direction);
61
62 Q_REVISION(6, 13) float gravity() const;
63 Q_REVISION(6, 13) void setGravity(float gravity);
64
65 Q_REVISION(6, 13) float unitDistance() const;
66 Q_REVISION(6, 13) void setUnitDistance(float unitDistance);
67
68 // The gravity as set. Its center is left for the backend to fill in from
69 // the pose of the field's actor.
70 const QPhysicsGravity &gravityState() const;
71
72 // Whether the field has no shapes, and so acts on the whole scene.
73 bool isSceneWide() const;
74
75 bool countsBody(const QAbstractPhysicsNode *body) const override;
76 QAbstractPhysXNode *createPhysXBackend() override;
78Q_SIGNALS:
79 Q_REVISION(6, 13) void modeChanged();
80 Q_REVISION(6, 13) void directionChanged();
81 Q_REVISION(6, 13) void gravityChanged();
82 Q_REVISION(6, 13) void unitDistanceChanged();
83
84protected:
85 void bodyDidEnter(QAbstractPhysicsNode *body) override;
86 void bodyDidExit(QAbstractPhysicsNode *body) override;
87
88private:
89 // Queues `body` to be woken by the backend, if it can sleep. Waking is not
90 // allowed from the trigger callback that bodyDidEnter() and bodyDidExit()
91 // run from.
92 void queueWake(QAbstractPhysicsNode *body);
93 // Every body inside the field's shapes that it acts on.
94 QList<QPointer<QAbstractPhysicsNode>> trackedBodies() const;
95
96 QPhysicsGravity m_gravity;
97 QList<QPointer<QAbstractPhysicsNode>> m_pendingWakes;
98 // Whether everything the field acts on is to be woken, because its gravity
99 // changed. True to begin with, since a field appearing is as much a reason
100 // to move as one changing.
101 bool m_pendingWakeAll = true;
102
103 friend class QPhysXGravityField;
104};
105
107
108#endif // GRAVITYFIELD_H
static QAbstractPhysXNode * backendOf(const QAbstractPhysicsNode *node)
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
QList< QPointer< QAbstractPhysicsNode > > wakesOnRemoval
std::optional< SceneGravity > applyGravity(QList< QFilterData > &staleFilters)
QPhysXGravityField(QGravityField *frontEnd)
DebugDrawBodyType getDebugDrawBodyType() override
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
bool useTriggerFlag() override
std::optional< QFilterData > staleFilterOnRemoval
void sync(float deltaTime, QHash< QQuick3DNode *, QMatrix4x4 > &transformCache) override
static void fieldDestroyed(QGravityField *field)
void apply(const QPhysicsGravity &gravity, QPhysicsGravity::Target target, QAbstractPhysicsNode *node, QAbstractPhysXNode *backend, bool wake)
Q_ALWAYS_INLINE bool fuzzyEquals(const physx::PxTransform &a, const physx::PxTransform &b)
PxTransformT< float > PxTransform
DebugDrawBodyType
static bool operator!=(const QFilterData &a, const QFilterData &b)
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE