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 | node->m_backendObject = this; |
27 | } |
28 | |
29 | QAbstractPhysXNode::~QAbstractPhysXNode() = default; |
30 | |
31 | bool QAbstractPhysXNode::cleanupIfRemoved(QPhysXWorld *physX) |
32 | { |
33 | if (isRemoved) { |
34 | cleanup(physX); |
35 | delete this; |
36 | return true; |
37 | } |
38 | return false; |
39 | } |
40 | |
41 | void QAbstractPhysXNode::updateDefaultDensity(float) { } |
42 | |
43 | void QAbstractPhysXNode::createMaterial(QPhysXWorld *physX) |
44 | { |
45 | createMaterialFromQtMaterial(physX, qtMaterial: nullptr); |
46 | } |
47 | |
48 | void QAbstractPhysXNode::createMaterialFromQtMaterial(QPhysXWorld *, QPhysicsMaterial *qtMaterial) |
49 | { |
50 | auto &s_physx = StaticPhysXObjects::getReference(); |
51 | |
52 | if (qtMaterial) { |
53 | material = s_physx.physics->createMaterial(staticFriction: qtMaterial->staticFriction(), |
54 | dynamicFriction: qtMaterial->dynamicFriction(), |
55 | restitution: qtMaterial->restitution()); |
56 | } else { |
57 | if (!sDefaultMaterial) { |
58 | sDefaultMaterial = s_physx.physics->createMaterial( |
59 | staticFriction: QPhysicsMaterial::defaultStaticFriction, |
60 | dynamicFriction: QPhysicsMaterial::defaultDynamicFriction, restitution: QPhysicsMaterial::defaultRestitution); |
61 | } |
62 | material = sDefaultMaterial; |
63 | } |
64 | } |
65 | |
66 | void QAbstractPhysXNode::markDirtyShapes() { } |
67 | |
68 | void QAbstractPhysXNode::rebuildDirtyShapes(QPhysicsWorld *, QPhysXWorld *) { } |
69 | |
70 | void QAbstractPhysXNode::cleanup(QPhysXWorld *) |
71 | { |
72 | for (auto *shape : shapes) |
73 | PHYSX_RELEASE(shape); |
74 | if (material != sDefaultMaterial) |
75 | PHYSX_RELEASE(material); |
76 | } |
77 | |
78 | bool QAbstractPhysXNode::debugGeometryCapability() |
79 | { |
80 | return false; |
81 | } |
82 | |
83 | physx::PxTransform QAbstractPhysXNode::getGlobalPose() |
84 | { |
85 | return {}; |
86 | } |
87 | |
88 | bool QAbstractPhysXNode::useTriggerFlag() |
89 | { |
90 | return false; |
91 | } |
92 | |
93 | DebugDrawBodyType QAbstractPhysXNode::getDebugDrawBodyType() |
94 | { |
95 | return DebugDrawBodyType::Unknown; |
96 | } |
97 | |
98 | bool QAbstractPhysXNode::shapesDirty() const |
99 | { |
100 | return frontendNode && frontendNode->m_shapesDirty; |
101 | } |
102 | |
103 | void QAbstractPhysXNode::setShapesDirty(bool dirty) |
104 | { |
105 | frontendNode->m_shapesDirty = dirty; |
106 | } |
107 | |
108 | QT_END_NAMESPACE |
109 |