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
qphysxgravityfield.cpp
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
6
7#include "physxnode/qphysxgravity_p.h"
11
12#include "PxRigidActor.h"
13
14#include <utility>
15
17
18QPhysXGravityField::QPhysXGravityField(QGravityField *frontEnd) : QPhysXActorBody(frontEnd) { }
19
20void QPhysXGravityField::init(QPhysicsWorld *world, QPhysXWorld *physX)
21{
22 QPhysXActorBody::init(world, physX);
23 // The world walks its fields, rather than every body to find them, so
24 // that a scene without fields pays nothing for them.
25 world->registerGravityField(this);
26}
27
32
33static bool operator!=(const QFilterData &a, const QFilterData &b)
34{
35 return a.group != b.group || a.ignoreGroups != b.ignoreGroups;
36}
37
39QPhysXGravityField::applyGravity(QList<QFilterData> &staleFilters)
40{
41 auto *field = static_cast<QGravityField *>(frontendNode);
42
43 const bool sceneWide = field->isSceneWide();
44 const QFilterData filterData = field->filterData();
45 bool wakeAll = std::exchange(field->m_pendingWakeAll, false);
46
47 // A field with shapes needs no extra handling when its shapes or filter
48 // change: PhysX refilters the pairs and reports the bodies entering and
49 // leaving, and the field wakes them in response (bodyDidEnter() and
50 // bodyDidExit()). A field without shapes has no pairs, so what it stopped
51 // acting on is woken by the world, and what it now acts on by applying it.
52 if (m_applied && m_sceneWide && (!sceneWide || filterData != m_filterData)) {
53 staleFilters.append(m_filterData);
54 wakeAll = true;
55 }
56 if (m_applied && sceneWide && !m_sceneWide)
57 wakeAll = true;
58 m_applied = true;
59 m_sceneWide = sceneWide;
60 m_filterData = filterData;
61
62 // Queued from the trigger callback, where waking is not allowed.
63 const auto pendingWakes = std::exchange(field->m_pendingWakes, {});
64 for (const QPointer<QAbstractPhysicsNode> &node : pendingWakes) {
65 if (node)
66 QPhysXGravity::wake(node, backendOf(node));
67 }
68
69 // Centred on the pose the actor had in the last step, which for a field
70 // with shapes is the one that reported what is inside them, and which is
71 // also always finite.
72 QPhysicsGravity gravity = field->gravityState();
73 gravity.center = QPhysicsUtils::toQtType(actor->getGlobalPose().p);
74
75 if (sceneWide) {
76 SceneGravity scene;
77 scene.gravity = gravity;
78 scene.filterData = filterData;
79 scene.wake = wakeAll;
80 return scene;
81 }
82
83 const auto &tracked = field->collisions();
84 for (auto it = tracked.cbegin(), end = tracked.cend(); it != end; ++it) {
85 if (!it->reports)
86 continue;
87 QAbstractPhysicsNode *node = it.key();
88 QPhysXGravity::apply(gravity, QPhysicsGravity::targetOf(node), node, backendOf(node),
89 wakeAll);
90 }
91 return std::nullopt;
92}
93
94void QPhysXGravityField::fieldDestroyed(QGravityField *field)
95{
96 auto *backend = static_cast<QPhysXGravityField *>(backendOf(field));
97 if (!backend)
98 return;
99
100 backend->wakesOnRemoval = std::exchange(field->m_pendingWakes, {});
101 backend->wakesOnRemoval.append(field->trackedBodies());
102 if (backend->m_applied && backend->m_sceneWide)
103 backend->staleFilterOnRemoval = backend->m_filterData;
104}
105
106void QPhysXGravityField::sync(float /*deltaTime*/,
107 QHash<QQuick3DNode *, QMatrix4x4> & /*transformCache*/)
108{
109 // Not chained to QPhysXActorBody::sync(): like a trigger body, a field has
110 // no physicsMaterial.
111 auto *field = static_cast<QGravityField *>(frontendNode);
112 const physx::PxTransform pose =
113 QPhysicsUtils::toPhysXTransform(field->scenePosition(), field->sceneRotation());
114 if (!pose.isSane()) {
115 qWarning() << "GravityField: position/rotation is not finite, keeping previous pose.";
116 return;
117 }
118
119 // Only when it has moved, as for QPhysXTriggerBody, so that bodies inside
120 // a field standing still can sleep.
121 const physx::PxTransform previous = actor->getGlobalPose();
122 if (QPhysicsUtils::fuzzyEquals(pose, previous))
123 return;
124 actor->setGlobalPose(pose);
125
126 // In Point mode the position is the centre of the gravity, so moving it
127 // wakes what the field acts on. Turning it changes nothing, so the
128 // positions are compared on their own: a field parented to a spinning node
129 // would otherwise keep everything it acts on awake.
130 if (field->gravityState().readsCenter()
131 && !qFuzzyCompare(QPhysicsUtils::toQtType(pose.p), QPhysicsUtils::toQtType(previous.p))) {
132 field->m_pendingWakeAll = true;
133 }
134}
135
static QAbstractPhysXNode * backendOf(const QAbstractPhysicsNode *node)
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
std::optional< SceneGravity > applyGravity(QList< QFilterData > &staleFilters)
QPhysXGravityField(QGravityField *frontEnd)
DebugDrawBodyType getDebugDrawBodyType() override
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
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