Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qphysxgravity.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
5
#
include
"qphysxgravity_p.h"
6
7
#
include
"physxnode/qphysxactorbody_p.h"
8
#
include
"qcharactercontroller_p.h"
9
#
include
"qdynamicrigidbody_p.h"
10
#
include
"qphysicsutils_p.h"
11
12
#
include
"PxRigidDynamic.h"
13
14
QT_BEGIN_NAMESPACE
15
16
// The actor of a dynamic body that takes forces right now, or null.
17
static
physx
::
PxRigidDynamic
*
simulatedActorOf
(QAbstractPhysicsNode *node,
18
QAbstractPhysXNode
*backend)
19
{
20
auto
*body = qobject_cast<QDynamicRigidBody *>(node);
21
if
(!body || !backend)
22
return
nullptr
;
23
// eDISABLE_GRAVITY only takes the scene's gravity off the body, so a body
24
// that turned gravity off has to be skipped here.
25
if
(!body->gravityEnabled())
26
return
nullptr
;
27
auto
*actor =
static_cast
<QPhysXActorBody *>(backend)->actor;
28
if
(!actor)
29
return
nullptr
;
30
// Asked of the actor rather than the frontend: a body made dynamic, or put
31
// back in the simulation, is only updated in its own sync(), and PhysX
32
// rejects a force on an actor that is kinematic or not simulated.
33
auto
*dynamicActor =
static_cast
<physx::PxRigidDynamic *>(actor);
34
if
(dynamicActor->getRigidBodyFlags() & physx::PxRigidBodyFlag::eKINEMATIC)
35
return
nullptr
;
36
if
(dynamicActor->getActorFlags() & physx::PxActorFlag::eDISABLE_SIMULATION)
37
return
nullptr
;
38
return
dynamicActor;
39
}
40
41
void
QPhysXGravity
::
apply
(
const
QPhysicsGravity &gravity, QPhysicsGravity::Target target,
42
QAbstractPhysicsNode *node,
QAbstractPhysXNode
*backend,
bool
wake)
43
{
44
if
(!backend)
45
return
;
46
47
switch
(target) {
48
case
QPhysicsGravity::Target::None:
49
return
;
50
case
QPhysicsGravity::Target::Character: {
51
// A controller is not moved by forces: it integrates its gravity
52
// itself in getDisplacement(), during its sync() later this frame.
53
auto
*character =
static_cast
<QCharacterController *>(node);
54
if
(
const
auto
accel = gravity.accelerationAt(character->scenePosition()))
55
character->addEnvironmentGravity(*accel);
56
return
;
57
}
58
case
QPhysicsGravity::Target::DynamicBody: {
59
auto
*actor = simulatedActorOf(node, backend);
60
if
(!actor)
61
return
;
62
// A body at rest is allowed to sleep under a gravity, as under the
63
// world's, and is only woken when the gravity changes or arrives.
64
if
(actor->isSleeping()) {
65
if
(!wake)
66
return
;
67
actor->wakeUp();
68
}
69
// The simulated position, which the frontend only catches up with in
70
// the body's own sync().
71
const
QVector3D position = QPhysicsUtils::toQtType(actor->getGlobalPose().p);
72
if
(
const
auto
accel = gravity.accelerationAt(position))
73
actor->addForce(QPhysicsUtils::toPhysXType(*accel), physx::PxForceMode::eACCELERATION,
74
false
);
75
return
;
76
}
77
}
78
}
79
80
void
QPhysXGravity
::
wake
(QAbstractPhysicsNode *node,
QAbstractPhysXNode
*backend)
81
{
82
if
(
auto
*actor = simulatedActorOf(node, backend))
83
actor->wakeUp();
84
}
85
86
QT_END_NAMESPACE
QAbstractPhysXNode
Definition
qabstractphysxnode_p.h:58
QPhysXGravity
Definition
qphysxgravity_p.h:28
QPhysXGravity::apply
void apply(const QPhysicsGravity &gravity, QPhysicsGravity::Target target, QAbstractPhysicsNode *node, QAbstractPhysXNode *backend, bool wake)
Definition
qphysxgravity.cpp:41
QPhysXGravity::wake
void wake(QAbstractPhysicsNode *node, QAbstractPhysXNode *backend)
Definition
qphysxgravity.cpp:80
simulatedActorOf
static QT_BEGIN_NAMESPACE physx::PxRigidDynamic * simulatedActorOf(QAbstractPhysicsNode *node, QAbstractPhysXNode *backend)
Definition
qphysxgravity.cpp:17
QT_BEGIN_NAMESPACE
#define QT_BEGIN_NAMESPACE
Definition
qtconfigmacros.h:161
QT_END_NAMESPACE
#define QT_END_NAMESPACE
Definition
qtconfigmacros.h:162
qtquick3dphysics
src
quick3dphysics
physxnode
qphysxgravity.cpp
Generated on
for Qt by
1.18.0