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
qphysicsgravity_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 PHYSICSGRAVITY_H
6#define PHYSICSGRAVITY_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>
20#include <QtCore/qnumeric.h>
21#include <QtGui/qvector3d.h>
22
23#include <optional>
24
25QT_BEGIN_NAMESPACE
26
27class QAbstractPhysicsNode;
28
29// The gravity of a GravityField: the values an author sets and the
30// acceleration they come to at a point. Kept apart from the field so that the
31// backend and the world can carry it around and evaluate it without it.
32struct Q_QUICK3DPHYSICS_EXPORT QPhysicsGravity
33{
34 // Same order as GravityField.Mode.
35 enum class Mode { Directional, Point };
36
37 // What a gravity can act on. A static body can never be moved by one, and
38 // a trigger region has nothing to move.
39 enum class Target { None, DynamicBody, Character };
40 static Target targetOf(const QAbstractPhysicsNode *node);
41
42 // Each validates the value, warning in the name of `owner` and keeping the
43 // old value if it is unusable, and returns whether the value changed.
44 bool setDirection(const QVector3D &direction, const char *owner);
45 bool setGravity(float gravity, const char *owner);
46 bool setUnitDistance(float unitDistance, const char *owner);
47
48 // Whether the current mode reads a value, which is whether changing it is
49 // a reason to wake the bodies asleep under the gravity.
50 bool readsDirection() const { return mode == Mode::Directional; }
51 bool readsUnitDistance() const { return mode == Mode::Point; }
52 bool readsCenter() const { return mode == Mode::Point; }
53
54 // The acceleration at `position`, or nothing when there is no direction to
55 // aim at: a body (numerically) on the centre of a Point gravity without
56 // falloff, or at a non-finite position.
57 std::optional<QVector3D> accelerationAt(const QVector3D &position) const
58 {
59 if (mode == Mode::Directional)
60 return direction.normalized() * gravity;
61
62 const QVector3D toCenter = center - position;
63 const float distance = toCenter.length();
64 if (!qIsFinite(distance))
65 return std::nullopt;
66
67 if (unitDistance > 0.0f) {
68 // As inside a solid sphere of uniform density: linear, down to
69 // nothing at the centre, so that a body there is at rest rather
70 // than pulled back and forth across it at full strength. Written
71 // without dividing by the distance, so the centre itself needs no
72 // special case.
73 if (distance < unitDistance)
74 return toCenter * (gravity / unitDistance);
75
76 // Inverse square. Computed as a squared ratio, since squaring a
77 // large unitDistance on its own overflows.
78 const float ratio = unitDistance / distance;
79 return (toCenter / distance) * (gravity * ratio * ratio);
80 }
81
82 // No falloff: full strength everywhere, so there is no direction to
83 // aim at on the centre itself.
84 if (qFuzzyIsNull(distance))
85 return std::nullopt;
86 return (toCenter / distance) * gravity;
87 }
88
89 Mode mode = Mode::Directional;
90 QVector3D direction { 0.0f, -1.0f, 0.0f };
91 // The magnitude of PhysicsWorld's default gravity.
92 float gravity = 981.0f;
93 float unitDistance = 0.0f;
94 // What Point mode aims at.
95 QVector3D center;
96};
97
98QT_END_NAMESPACE
99
100#endif // PHYSICSGRAVITY_H
void apply(const QPhysicsGravity &gravity, QPhysicsGravity::Target target, QAbstractPhysicsNode *node, QAbstractPhysXNode *backend, bool wake)
void wake(QAbstractPhysicsNode *node, QAbstractPhysXNode *backend)
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE physx::PxRigidDynamic * simulatedActorOf(QAbstractPhysicsNode *node, QAbstractPhysXNode *backend)
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE