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
qphysxactorbody.cpp
Go to the documentation of this file.
1
// Copyright (C) 2023 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
"qphysxactorbody_p.h"
6
7
#
include
"PxMaterial.h"
8
#
include
"PxPhysics.h"
9
#
include
"PxRigidDynamic.h"
10
#
include
"PxRigidActor.h"
11
#
include
"PxRigidStatic.h"
12
#
include
"PxScene.h"
13
#
include
"characterkinematic/PxController.h"
14
#
include
"characterkinematic/PxControllerManager.h"
15
16
#
include
"physxnode/qphysxworld_p.h"
17
#
include
"qabstractphysicsbody_p.h"
18
#
include
"qheightfieldshape_p.h"
19
#
include
"qphysicsutils_p.h"
20
#
include
"qplaneshape_p.h"
21
#
include
"qstaticphysxobjects_p.h"
22
23
#
include
<
QtGui
/
qquaternion
.
h
>
24
25
#
define
PHYSX_RELEASE
(
x
)
26
if
(
x
!=
nullptr
)
{
27
x
->
release
(
)
;
28
x
=
nullptr
;
29
}
30
31
QT_BEGIN_NAMESPACE
32
33
static
physx
::
PxTransform
getPhysXLocalTransform
(
const
QQuick3DNode *node)
34
{
35
// Modify transforms to make the PhysX shapes match the QtQuick3D conventions
36
if
(qobject_cast<
const
QPlaneShape *>(node) !=
nullptr
) {
37
// Rotate the plane to make it match the built-in rectangle
38
const
QQuaternion rotation = QPhysicsUtils::kMinus90YawRotation * node->rotation();
39
return
physx::PxTransform(QPhysicsUtils::toPhysXType(node->position()),
40
QPhysicsUtils::toPhysXType(rotation));
41
}
else
if
(
auto
*hf = qobject_cast<
const
QHeightFieldShape *>(node)) {
42
// Shift the height field so it's centered at the origin
43
return
physx::PxTransform(QPhysicsUtils::toPhysXType(node->position() + hf->hfOffset()),
44
QPhysicsUtils::toPhysXType(node->rotation()));
45
}
46
47
const
QQuaternion &rotation = node->rotation();
48
const
QVector3D
&localPosition = node->position();
49
const
QVector3D
&scale = node->sceneScale();
50
return
physx::PxTransform(QPhysicsUtils::toPhysXType(localPosition * scale),
51
QPhysicsUtils::toPhysXType(rotation));
52
}
53
54
QPhysXActorBody
::QPhysXActorBody(QAbstractPhysicsNode *frontEnd) :
QAbstractPhysXNode
(frontEnd) { }
55
56
void
QPhysXActorBody
::
cleanup
(
QPhysXWorld
*physX)
57
{
58
if
(
actor
) {
59
physX
->
scene
->removeActor(*
actor
);
60
PHYSX_RELEASE
(
actor
);
61
}
62
QAbstractPhysXNode
::
cleanup
(physX);
63
}
64
65
void
QPhysXActorBody
::
init
(QPhysicsWorld *
/*world*/
,
QPhysXWorld
*physX)
66
{
67
Q_ASSERT(!
actor
);
68
69
updateMaterial
();
70
createActor
(
physX
)
;
71
72
actor
->userData =
reinterpret_cast
<
void
*>(frontendNode);
73
physX
->
scene
->addActor(*
actor
);
74
setShapesDirty
(
true
);
75
}
76
77
void
QPhysXActorBody
::
sync
(
float
/*deltaTime*/
,
78
QHash<QQuick3DNode *, QMatrix4x4> &
/*transformCache*/
)
79
{
80
// The physicsMaterial property, and the properties of the material itself, can change while
81
// the simulation is running. Materials are shared between all nodes that need the same
82
// properties, so rather than modifying the material in place, the node is moved to the
83
// material with the new properties, and its shapes are pointed at it.
84
if
(updateMaterial() && material) {
85
for
(
auto
*shape : std::as_const(shapes))
86
shape->setMaterials(&material, 1);
87
}
88
}
89
90
void
QPhysXActorBody
::
markDirtyShapes
()
91
{
92
if
(!frontendNode || !actor)
93
return
;
94
95
// Go through the shapes and look for a change in pose (rotation, position)
96
// TODO: it is likely cheaper to connect a signal for changes on the position and rotation
97
// property and mark the node dirty then.
98
if
(!
shapesDirty
()) {
99
const
auto
&collisionShapes = frontendNode->getCollisionShapesList();
100
const
auto
&physXShapes = shapes;
101
102
const
int
len = collisionShapes.size();
103
if
(physXShapes.size() != len) {
104
// This should not really happen but check it anyway
105
setShapesDirty
(
true
);
106
}
else
{
107
for
(
int
i = 0; i < len; i++) {
108
auto
poseNew = getPhysXLocalTransform(collisionShapes[i]);
109
auto
poseOld = physXShapes[i]->getLocalPose();
110
111
if
(!QPhysicsUtils::fuzzyEquals(poseNew, poseOld)) {
112
setShapesDirty
(
true
);
113
break
;
114
}
115
}
116
}
117
}
118
}
119
120
void
QPhysXActorBody
::
rebuildDirtyShapes
(QPhysicsWorld *
/*world*/
,
QPhysXWorld
*physX)
121
{
122
if
(!
shapesDirty
())
123
return
;
124
buildShapes
(
physX
)
;
125
setShapesDirty
(
false
);
126
}
127
128
void
QPhysXActorBody
::
detachFrontend
()
129
{
130
if
(
actor
)
131
actor
->userData =
nullptr
;
132
QAbstractPhysXNode
::
detachFrontend
();
133
}
134
135
void
QPhysXActorBody
::
createActor
(
QPhysXWorld
*
/*physX*/
)
136
{
137
auto
&s_physx =
StaticPhysXObjects
::
getReference
(
)
;
138
physx
::
PxTransform
trf = QPhysicsUtils::toPhysXTransform(frontendNode->scenePosition(),
139
frontendNode->sceneRotation());
140
if
(!trf.isSane()) {
141
qWarning() <<
"PhysicsNode: position/rotation is not finite, using identity instead."
;
142
trf = physx::PxTransform(physx::PxIdentity);
143
}
144
auto
*dynamicActor = s_physx
.
physics
->createRigidDynamic(trf);
145
146
// A trigger has to be a dynamic actor, since PhysX only reports a trigger
147
// pair when one of the two is, but it is not simulated and nothing reads
148
// the velocity gravity would give it. Without gravity it stays where it is
149
// put and can fall asleep, which is what stops its pairs being tested:
150
// PhysX skips a trigger pair's overlap test only when both of its actors
151
// are asleep (see Sc::TriggerInteraction).
152
if
(
useTriggerFlag
())
153
dynamicActor->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY,
true
);
154
155
actor
= dynamicActor;
156
}
157
158
bool
QPhysXActorBody
::
debugGeometryCapability
()
159
{
160
return
true
;
161
}
162
163
physx
::
PxTransform
QPhysXActorBody
::
getGlobalPose
()
164
{
165
return
actor
->getGlobalPose();
166
}
167
168
void
QPhysXActorBody
::
buildShapes
(
QPhysXWorld
*
/*physX*/
)
169
{
170
auto
body =
actor
;
171
for
(
auto
*shape : std::as_const(shapes)) {
172
body->detachShape(*shape);
173
PHYSX_RELEASE
(shape);
174
}
175
176
// TODO: Only remove changed shapes?
177
shapes.clear();
178
179
for
(
const
auto
&collisionShape : frontendNode->getCollisionShapesList()) {
180
auto
*geom = collisionShape->getPhysXGeometry();
181
if
(!geom || !material)
182
continue
;
183
184
auto
&s_physx = StaticPhysXObjects::getReference();
185
// The shape is created exclusive to this actor: a shape that may be shared between
186
// actors cannot be modified once it is attached to one, and these shapes are modified
187
// while attached, for instance when their filter data changes.
188
auto
physXShape = s_physx.physics->createShape(*geom, *material,
true
);
189
if
(!physXShape) {
190
qWarning() <<
"QtQuick3DPhysics: could not create shape, invalid geometry."
;
191
continue
;
192
}
193
194
// Ties the PxShape back to the collision shape it was built for: a
195
// trigger pair is matched by those, which survive a rebuild replacing
196
// every PxShape of a node.
197
physXShape->userData =
reinterpret_cast
<
void
*>(collisionShape);
198
199
if
(useTriggerFlag()) {
200
physXShape->setFlag(physx::PxShapeFlag::eSCENE_QUERY_SHAPE,
false
);
201
physXShape->setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE,
false
);
202
physXShape->setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE,
true
);
203
}
204
205
physXShape->setSimulationFilterData(QPhysicsUtils::toPhysXType(frontendNode->filterData()));
206
207
physx::PxTransform localPose = getPhysXLocalTransform(collisionShape);
208
if
(!localPose.isSane()) {
209
qWarning() <<
"QtQuick3DPhysics: collision shape position/rotation is not finite, "
210
"using identity local pose instead."
;
211
localPose = physx::PxTransform(physx::PxIdentity);
212
}
213
physXShape->setLocalPose(localPose);
214
215
// A shape PhysX will not take is not kept: left in 'shapes' it would make
216
// markDirtyShapes() find the body's shapes to be in order, so nothing would
217
// ever rebuild them, and the next rebuild would detach a shape that had never
218
// been attached.
219
if
(!body->attachShape(*physXShape)) {
220
qWarning() <<
"QtQuick3DPhysics: could not attach collision shape to body, it will "
221
"not take part in the simulation."
;
222
PHYSX_RELEASE
(physXShape);
223
continue
;
224
}
225
226
shapes.push_back(physXShape);
227
}
228
229
// Filters are always clean after building shapes
230
setFiltersDirty
(
false
);
231
}
232
233
void
QPhysXActorBody
::
updateFilters
(
QPhysXWorld
*physX)
234
{
235
if
(!
filtersDirty
())
236
return
;
237
238
// Go through all shapes and set the filter group and mask.
239
// TODO: What about shared shapes on several actors?
240
const
physx::PxFilterData filterData = QPhysicsUtils::toPhysXType(frontendNode->filterData());
241
for
(
auto
&physXShape : shapes)
242
physXShape->setSimulationFilterData(filterData);
243
244
// PxController caches the static shapes near it and reuses that cache
245
// until it moves outside the cached bounds or a static shape is added,
246
// removed or moved. We have to manually invalidate the cache when the
247
// filter data changes; otherwise a controller next to this body would
248
// keep colliding with it (or passing through it) based on the old groups.
249
// Dynamic shapes aren't cached, so only statics need this.
250
if
(actor && actor->is<physx::PxRigidStatic>() && physX && physX->controllerManager) {
251
auto
*manager = physX
->
controllerManager
;
252
for
(physx::PxU32 i = 0; i < manager->getNbControllers(); ++i)
253
manager->getController(i)->invalidateCache();
254
}
255
256
setFiltersDirty
(
false
);
257
}
258
259
QT_END_NAMESPACE
QAbstractPhysXNode
Definition
qabstractphysxnode_p.h:58
QAbstractPhysXNode::filtersDirty
bool filtersDirty() const
Definition
qabstractphysxnode.cpp:156
QAbstractPhysXNode::cleanup
virtual void cleanup(QPhysXWorld *)
Definition
qabstractphysxnode.cpp:118
QAbstractPhysXNode::shapesDirty
bool shapesDirty() const
Definition
qabstractphysxnode.cpp:146
QAbstractPhysXNode::setShapesDirty
void setShapesDirty(bool dirty)
Definition
qabstractphysxnode.cpp:151
QAbstractPhysXNode::updateMaterial
bool updateMaterial()
Definition
qabstractphysxnode.cpp:66
QAbstractPhysXNode::setFiltersDirty
void setFiltersDirty(bool dirty)
Definition
qabstractphysxnode.cpp:161
QAbstractPhysXNode::detachFrontend
virtual void detachFrontend()
Definition
qabstractphysxnode.cpp:167
QAbstractPhysXNode::useTriggerFlag
virtual bool useTriggerFlag()
Definition
qabstractphysxnode.cpp:136
QPhysXActorBody
Definition
qphysxactorbody_p.h:29
QPhysXActorBody::updateFilters
void updateFilters(QPhysXWorld *physX) override
Definition
qphysxactorbody.cpp:233
QPhysXActorBody::markDirtyShapes
void markDirtyShapes() override
Definition
qphysxactorbody.cpp:90
QPhysXActorBody::buildShapes
void buildShapes(QPhysXWorld *physX)
Definition
qphysxactorbody.cpp:168
QPhysXActorBody::rebuildDirtyShapes
void rebuildDirtyShapes(QPhysicsWorld *world, QPhysXWorld *physX) override
Definition
qphysxactorbody.cpp:120
QPhysXActorBody::init
void init(QPhysicsWorld *world, QPhysXWorld *physX) override
Definition
qphysxactorbody.cpp:65
QPhysXActorBody::detachFrontend
void detachFrontend() override
Definition
qphysxactorbody.cpp:128
QPhysXActorBody::debugGeometryCapability
bool debugGeometryCapability() override
Definition
qphysxactorbody.cpp:158
QPhysXActorBody::cleanup
void cleanup(QPhysXWorld *physX) override
Definition
qphysxactorbody.cpp:56
QPhysXActorBody::actor
physx::PxRigidActor * actor
Definition
qphysxactorbody_p.h:46
QPhysXActorBody::createActor
virtual void createActor(QPhysXWorld *physX)
Definition
qphysxactorbody.cpp:135
QPhysXActorBody::sync
void sync(float deltaTime, QHash< QQuick3DNode *, QMatrix4x4 > &transformCache) override
Definition
qphysxactorbody.cpp:77
QPhysXActorBody::getGlobalPose
physx::PxTransform getGlobalPose() override
Definition
qphysxactorbody.cpp:163
QPhysXWorld
Definition
qphysxworld_p.h:33
QPhysXWorld::scene
physx::PxScene * scene
Definition
qphysxworld_p.h:43
QPhysXWorld::controllerManager
physx::PxControllerManager * controllerManager
Definition
qphysxworld_p.h:41
QVector3D
The QVector3D class represents a vector or vertex in 3D space.
Definition
qvectornd.h:178
physx
Definition
qphysxactorbody_p.h:22
physx::PxTransform
PxTransformT< float > PxTransform
Definition
qabstractphysicsnode_p.h:29
PHYSX_RELEASE
#define PHYSX_RELEASE(x)
Definition
qabstractphysxnode.cpp:15
getPhysXLocalTransform
static QT_BEGIN_NAMESPACE physx::PxTransform getPhysXLocalTransform(const QQuick3DNode *node)
Definition
qphysxactorbody.cpp:33
QT_BEGIN_NAMESPACE
#define QT_BEGIN_NAMESPACE
Definition
qtconfigmacros.h:161
QT_END_NAMESPACE
#define QT_END_NAMESPACE
Definition
qtconfigmacros.h:162
StaticPhysXObjects
Definition
qstaticphysxobjects_p.h:71
StaticPhysXObjects::getReference
static StaticPhysXObjects & getReference()
Definition
qstaticphysxobjects.cpp:14
StaticPhysXObjects::physics
physx::PxPhysics * physics
Definition
qstaticphysxobjects_p.h:75
qtquick3dphysics
src
quick3dphysics
physxnode
qphysxactorbody.cpp
Generated on
for Qt by
1.18.0