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
qabstractphysxnode.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
6
10
11#include "PxPhysics.h"
12#include "PxMaterial.h"
13#include "PxShape.h"
14
15#define PHYSX_RELEASE(x)
16 if (x != nullptr) {
17 x->release();
18 x = nullptr;
19 }
20
22
23QAbstractPhysXNode::QAbstractPhysXNode(QAbstractPhysicsNode *node) : frontendNode(node)
24{
25 Q_ASSERT(node->m_backendObject == nullptr);
26 node->m_backendObject = this;
27}
28
30 if (!frontendNode) {
31 Q_ASSERT(isRemoved);
32 return;
33 }
34
35 Q_ASSERT(frontendNode->m_backendObject == this);
36 frontendNode->m_backendObject = nullptr;
37}
38
40{
41 if (isRemoved) {
42 cleanup(physX);
43 delete this;
44 return true;
45 }
46 return false;
47}
48
50
51static PhysicsMaterialProperties propertiesFor(const QPhysicsMaterial *qtMaterial)
52{
53 if (!qtMaterial) {
54 return { QPhysicsMaterial::defaultStaticFriction, QPhysicsMaterial::defaultDynamicFriction,
55 QPhysicsMaterial::defaultRestitution };
56 }
57 return { qtMaterial->staticFriction(), qtMaterial->dynamicFriction(),
58 qtMaterial->restitution() };
59}
60
61QPhysicsMaterial *QAbstractPhysXNode::qtMaterial() const
62{
63 return nullptr;
64}
65
67{
69 if (properties == materialProperties)
70 return false;
71
72 physx::PxMaterial *previousMaterial = material;
74 materialProperties = properties;
75
77 SharedPhysicsMaterial &shared = s_physx.materials[properties];
78 if (!shared.material) {
79 shared.material = s_physx.physics->createMaterial(
80 properties.staticFriction, properties.dynamicFriction, properties.restitution);
81 if (!shared.material) {
82 // PhysX allows at most 64K materials. Without a material the node gets no shapes,
83 // so it will not collide with anything.
84 qWarning() << "QtQuick3DPhysics: could not create physics material, the body will "
85 "not take part in collisions.";
86 s_physx.materials.remove(properties);
87 return material != previousMaterial;
88 }
89 }
90
91 ++shared.nodeCount;
92 material = shared.material;
93 return material != previousMaterial;
94}
95
97{
98 if (!material)
99 return;
100
101 auto &materials = StaticPhysXObjects::getReference().materials;
102 const auto it = materials.find(materialProperties);
103 if (it != materials.end() && it->material == material && --it->nodeCount == 0) {
104 // Any shape still referencing the material keeps it alive until it is rebuilt
105 it->material->release();
106 materials.erase(it);
107 }
108 material = nullptr;
110}
111
113
115
117
119{
120 for (auto *shape : std::as_const(shapes))
121 PHYSX_RELEASE(shape);
123 shapes.clear();
124}
125
127{
128 return false;
129}
130
132{
133 return {};
134}
135
137{
138 return false;
139}
140
145
147{
148 return frontendNode && frontendNode->m_shapesDirty;
149}
150
152{
153 frontendNode->m_shapesDirty = dirty;
154}
155
157{
158 return frontendNode && frontendNode->m_filtersDirty;
159}
160
162{
163 Q_ASSERT(frontendNode);
164 frontendNode->m_filtersDirty = dirty;
165}
166
virtual void updateDefaultDensity(float density)
QAbstractPhysicsNode * frontendNode
virtual void cleanup(QPhysXWorld *)
physx::PxMaterial * material
virtual void rebuildDirtyShapes(QPhysicsWorld *, QPhysXWorld *)
PhysicsMaterialProperties materialProperties
void setShapesDirty(bool dirty)
virtual bool debugGeometryCapability()
virtual physx::PxTransform getGlobalPose()
virtual void markDirtyShapes()
virtual DebugDrawBodyType getDebugDrawBodyType()
virtual QPhysicsMaterial * qtMaterial() const
void setFiltersDirty(bool dirty)
bool cleanupIfRemoved(QPhysXWorld *physX)
virtual bool useTriggerFlag()
#define PHYSX_RELEASE(x)
static PhysicsMaterialProperties propertiesFor(const QPhysicsMaterial *qtMaterial)
DebugDrawBodyType
#define QT_BEGIN_NAMESPACE
#define QT_END_NAMESPACE
friend bool operator==(const PhysicsMaterialProperties &lhs, const PhysicsMaterialProperties &rhs) noexcept
static constexpr PhysicsMaterialProperties none()
physx::PxMaterial * material
static StaticPhysXObjects & getReference()