1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qabstractphysxnode_p.h"
5
6#include "qabstractphysicsnode_p.h"
7#include "qphysicsmaterial_p.h"
8#include "qstaticphysxobjects_p.h"
9
10#include "PxPhysics.h"
11#include "PxMaterial.h"
12#include "PxShape.h"
13
14#define PHYSX_RELEASE(x) \
15 if (x != nullptr) { \
16 x->release(); \
17 x = nullptr; \
18 }
19
20QT_BEGIN_NAMESPACE
21
22physx::PxMaterial *QAbstractPhysXNode::sDefaultMaterial = nullptr;
23
24QAbstractPhysXNode::QAbstractPhysXNode(QAbstractPhysicsNode *node) : frontendNode(node)
25{
26 Q_ASSERT(node->m_backendObject == nullptr);
27 node->m_backendObject = this;
28}
29
30QAbstractPhysXNode::~QAbstractPhysXNode() {
31 if (!frontendNode) {
32 Q_ASSERT(isRemoved);
33 return;
34 }
35
36 Q_ASSERT(frontendNode->m_backendObject == this);
37 frontendNode->m_backendObject = nullptr;
38}
39
40bool QAbstractPhysXNode::cleanupIfRemoved(QPhysXWorld *physX)
41{
42 if (isRemoved) {
43 cleanup(physX);
44 delete this;
45 return true;
46 }
47 return false;
48}
49
50void QAbstractPhysXNode::updateDefaultDensity(float) { }
51
52void QAbstractPhysXNode::createMaterial(QPhysXWorld *physX)
53{
54 createMaterialFromQtMaterial(physX, qtMaterial: nullptr);
55}
56
57void QAbstractPhysXNode::createMaterialFromQtMaterial(QPhysXWorld *, QPhysicsMaterial *qtMaterial)
58{
59 auto &s_physx = StaticPhysXObjects::getReference();
60
61 if (qtMaterial) {
62 material = s_physx.physics->createMaterial(staticFriction: qtMaterial->staticFriction(),
63 dynamicFriction: qtMaterial->dynamicFriction(),
64 restitution: qtMaterial->restitution());
65 } else {
66 if (!sDefaultMaterial) {
67 sDefaultMaterial = s_physx.physics->createMaterial(
68 staticFriction: QPhysicsMaterial::defaultStaticFriction,
69 dynamicFriction: QPhysicsMaterial::defaultDynamicFriction, restitution: QPhysicsMaterial::defaultRestitution);
70 }
71 material = sDefaultMaterial;
72 }
73}
74
75void QAbstractPhysXNode::markDirtyShapes() { }
76
77void QAbstractPhysXNode::rebuildDirtyShapes(QPhysicsWorld *, QPhysXWorld *) { }
78
79void QAbstractPhysXNode::updateFilters() { }
80
81void QAbstractPhysXNode::cleanup(QPhysXWorld *)
82{
83 for (auto *shape : shapes)
84 PHYSX_RELEASE(shape);
85 if (material != sDefaultMaterial)
86 PHYSX_RELEASE(material);
87}
88
89bool QAbstractPhysXNode::debugGeometryCapability()
90{
91 return false;
92}
93
94physx::PxTransform QAbstractPhysXNode::getGlobalPose()
95{
96 return {};
97}
98
99bool QAbstractPhysXNode::useTriggerFlag()
100{
101 return false;
102}
103
104DebugDrawBodyType QAbstractPhysXNode::getDebugDrawBodyType()
105{
106 return DebugDrawBodyType::Unknown;
107}
108
109bool QAbstractPhysXNode::shapesDirty() const
110{
111 return frontendNode && frontendNode->m_shapesDirty;
112}
113
114void QAbstractPhysXNode::setShapesDirty(bool dirty)
115{
116 frontendNode->m_shapesDirty = dirty;
117}
118
119bool QAbstractPhysXNode::filtersDirty() const
120{
121 return frontendNode && frontendNode->m_filtersDirty;
122}
123
124void QAbstractPhysXNode::setFiltersDirty(bool dirty)
125{
126 Q_ASSERT(frontendNode);
127 frontendNode->m_filtersDirty = dirty;
128}
129
130QT_END_NAMESPACE
131

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtquick3dphysics/src/quick3dphysics/physxnode/qabstractphysxnode.cpp