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 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | physx::PxMaterial *QAbstractPhysXNode::sDefaultMaterial = nullptr; |
23 | |
24 | QAbstractPhysXNode::QAbstractPhysXNode(QAbstractPhysicsNode *node) : frontendNode(node) |
25 | { |
26 | Q_ASSERT(node->m_backendObject == nullptr); |
27 | node->m_backendObject = this; |
28 | } |
29 | |
30 | QAbstractPhysXNode::~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 | |
40 | bool QAbstractPhysXNode::cleanupIfRemoved(QPhysXWorld *physX) |
41 | { |
42 | if (isRemoved) { |
43 | cleanup(physX); |
44 | delete this; |
45 | return true; |
46 | } |
47 | return false; |
48 | } |
49 | |
50 | void QAbstractPhysXNode::updateDefaultDensity(float) { } |
51 | |
52 | void QAbstractPhysXNode::createMaterial(QPhysXWorld *physX) |
53 | { |
54 | createMaterialFromQtMaterial(physX, qtMaterial: nullptr); |
55 | } |
56 | |
57 | void 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 | |
75 | void QAbstractPhysXNode::markDirtyShapes() { } |
76 | |
77 | void QAbstractPhysXNode::rebuildDirtyShapes(QPhysicsWorld *, QPhysXWorld *) { } |
78 | |
79 | void QAbstractPhysXNode::updateFilters() { } |
80 | |
81 | void QAbstractPhysXNode::cleanup(QPhysXWorld *) |
82 | { |
83 | for (auto *shape : shapes) |
84 | PHYSX_RELEASE(shape); |
85 | if (material != sDefaultMaterial) |
86 | PHYSX_RELEASE(material); |
87 | } |
88 | |
89 | bool QAbstractPhysXNode::debugGeometryCapability() |
90 | { |
91 | return false; |
92 | } |
93 | |
94 | physx::PxTransform QAbstractPhysXNode::getGlobalPose() |
95 | { |
96 | return {}; |
97 | } |
98 | |
99 | bool QAbstractPhysXNode::useTriggerFlag() |
100 | { |
101 | return false; |
102 | } |
103 | |
104 | DebugDrawBodyType QAbstractPhysXNode::getDebugDrawBodyType() |
105 | { |
106 | return DebugDrawBodyType::Unknown; |
107 | } |
108 | |
109 | bool QAbstractPhysXNode::shapesDirty() const |
110 | { |
111 | return frontendNode && frontendNode->m_shapesDirty; |
112 | } |
113 | |
114 | void QAbstractPhysXNode::setShapesDirty(bool dirty) |
115 | { |
116 | frontendNode->m_shapesDirty = dirty; |
117 | } |
118 | |
119 | bool QAbstractPhysXNode::filtersDirty() const |
120 | { |
121 | return frontendNode && frontendNode->m_filtersDirty; |
122 | } |
123 | |
124 | void QAbstractPhysXNode::setFiltersDirty(bool dirty) |
125 | { |
126 | Q_ASSERT(frontendNode); |
127 | frontendNode->m_filtersDirty = dirty; |
128 | } |
129 | |
130 | QT_END_NAMESPACE |
131 | |