5#include <QtQuick3D/private/qquick3dobject_p.h>
6#include <foundation/PxTransform.h>
10#include <QtGui/qquaternion.h>
15
16
17
18
19
20
21
22
23
26
27
28
29
30
31
32
35
36
37
38
39
40
43
44
45
46
47
48
49
52
53
54
55
56
57
60
61
62
63
64
65
68
69
70
71
72
73
74
75
76
79
80
81
82
83
84
85
86
87
90
91
92
93
94
95
96
97
98
99
100
101
102
105
106
107
108
109
110
111
114
115
116
117
118
119
120
122QAbstractPhysicsNode::QAbstractPhysicsNode()
124 QPhysicsWorld::registerNode(
this);
127QAbstractPhysicsNode::~QAbstractPhysicsNode()
129 for (
auto shape : std::as_const(m_collisionShapes))
130 shape->disconnect(
this);
131 QPhysicsWorld::deregisterNode(
this);
134QQmlListProperty<QAbstractCollisionShape> QAbstractPhysicsNode::collisionShapes()
136 return QQmlListProperty<QAbstractCollisionShape>(
137 this,
nullptr, QAbstractPhysicsNode::qmlAppendShape,
138 QAbstractPhysicsNode::qmlShapeCount, QAbstractPhysicsNode::qmlShapeAt,
139 QAbstractPhysicsNode::qmlClearShapes);
142const QVector<QAbstractCollisionShape *> &QAbstractPhysicsNode::getCollisionShapesList()
const
144 return m_collisionShapes;
147void QAbstractPhysicsNode::updateFromPhysicsTransform(
const physx::PxTransform &transform)
149 const auto pos = transform.p;
150 const auto rotation = transform.q;
151 const auto qtPosition = QVector3D(pos.x, pos.y, pos.z);
152 const auto qtRotation = QQuaternion(rotation.w, rotation.x, rotation.y, rotation.z);
155 const QQuick3DNode *parentNode =
static_cast<QQuick3DNode *>(parentItem());
159 setRotation(qtRotation);
160 setPosition(qtPosition);
162 setPosition(parentNode->mapPositionFromScene(qtPosition));
163 const auto relativeRotation = parentNode->sceneRotation().inverted() * qtRotation;
164 setRotation(relativeRotation);
168bool QAbstractPhysicsNode::sendContactReports()
const
170 return m_sendContactReports;
173void QAbstractPhysicsNode::setSendContactReports(
bool sendContactReports)
175 if (m_sendContactReports == sendContactReports)
178 m_sendContactReports = sendContactReports;
179 emit sendContactReportsChanged(m_sendContactReports);
182bool QAbstractPhysicsNode::receiveContactReports()
const
184 return m_receiveContactReports;
187void QAbstractPhysicsNode::setReceiveContactReports(
bool receiveContactReports)
189 if (m_receiveContactReports == receiveContactReports)
192 m_receiveContactReports = receiveContactReports;
193 emit receiveContactReportsChanged(m_receiveContactReports);
196bool QAbstractPhysicsNode::sendTriggerReports()
const
198 return m_sendTriggerReports;
201void QAbstractPhysicsNode::setSendTriggerReports(
bool sendTriggerReports)
203 if (m_sendTriggerReports == sendTriggerReports)
206 m_sendTriggerReports = sendTriggerReports;
207 emit sendTriggerReportsChanged(m_sendTriggerReports);
210bool QAbstractPhysicsNode::receiveTriggerReports()
const
212 return m_receiveTriggerReports;
215void QAbstractPhysicsNode::setReceiveTriggerReports(
bool receiveTriggerReports)
217 if (m_receiveTriggerReports == receiveTriggerReports)
220 m_receiveTriggerReports = receiveTriggerReports;
221 emit receiveTriggerReportsChanged(m_receiveTriggerReports);
224void QAbstractPhysicsNode::registerContact(QAbstractPhysicsNode *body,
225 const QVector<QVector3D> &positions,
226 const QVector<QVector3D> &impulses,
227 const QVector<QVector3D> &normals)
229 emit bodyContact(body, positions, impulses, normals);
232void QAbstractPhysicsNode::onShapeDestroyed(QObject *object)
234 m_collisionShapes.removeAll(
static_cast<QAbstractCollisionShape *>(object));
237void QAbstractPhysicsNode::onShapeNeedsRebuild(QObject * )
239 m_shapesDirty =
true;
242void QAbstractPhysicsNode::qmlAppendShape(QQmlListProperty<QAbstractCollisionShape> *list,
243 QAbstractCollisionShape *shape)
245 if (shape ==
nullptr)
247 QAbstractPhysicsNode *self =
static_cast<QAbstractPhysicsNode *>(list->object);
248 self->m_collisionShapes.push_back(shape);
249 self->m_hasStaticShapes = self->m_hasStaticShapes || shape->isStaticShape();
251 if (shape->parentItem() ==
nullptr) {
254 QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(shape->parent());
256 shape->setParentItem(parentItem);
258 const auto &scenManager = QQuick3DObjectPrivate::get(self)->sceneManager;
260 QQuick3DObjectPrivate::refSceneManager(shape, *scenManager);
266 connect(shape, &QAbstractCollisionShape::destroyed, self,
267 &QAbstractPhysicsNode::onShapeDestroyed);
270 connect(shape, &QAbstractCollisionShape::needsRebuild, self,
271 &QAbstractPhysicsNode::onShapeNeedsRebuild);
274QAbstractCollisionShape *
275QAbstractPhysicsNode::qmlShapeAt(QQmlListProperty<QAbstractCollisionShape> *list, qsizetype index)
277 QAbstractPhysicsNode *self =
static_cast<QAbstractPhysicsNode *>(list->object);
278 return self->m_collisionShapes.at(index);
281qsizetype QAbstractPhysicsNode::qmlShapeCount(QQmlListProperty<QAbstractCollisionShape> *list)
283 QAbstractPhysicsNode *self =
static_cast<QAbstractPhysicsNode *>(list->object);
284 return self->m_collisionShapes.count();
287void QAbstractPhysicsNode::qmlClearShapes(QQmlListProperty<QAbstractCollisionShape> *list)
289 QAbstractPhysicsNode *self =
static_cast<QAbstractPhysicsNode *>(list->object);
290 for (
const auto &shape : std::as_const(self->m_collisionShapes)) {
291 if (shape->parentItem() ==
nullptr)
292 QQuick3DObjectPrivate::get(shape)->derefSceneManager();
294 self->m_hasStaticShapes =
false;
295 for (
auto shape : std::as_const(self->m_collisionShapes))
296 shape->disconnect(self);
297 self->m_collisionShapes.clear();
300int QAbstractPhysicsNode::filterGroup()
const
302 return m_filterGroup;
305void QAbstractPhysicsNode::setfilterGroup(
int newfilterGroup)
307 if (m_filterGroup == newfilterGroup)
309 m_filterGroup = newfilterGroup;
310 m_filtersDirty =
true;
311 emit filterGroupChanged();
314int QAbstractPhysicsNode::filterIgnoreGroups()
const
316 return m_filterIgnoreGroups;
319void QAbstractPhysicsNode::setFilterIgnoreGroups(
int newFilterIgnoreGroups)
321 if (m_filterIgnoreGroups == newFilterIgnoreGroups)
323 m_filterIgnoreGroups = newFilterIgnoreGroups;
324 m_filtersDirty =
true;
325 emit filterIgnoreGroupsChanged();