Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qabstractphysicsnode.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5#include <QtQuick3D/private/qquick3dobject_p.h>
6#include <foundation/PxTransform.h>
7
8#include "qphysicsworld_p.h"
10
123
125{
126 for (auto shape : std::as_const(m_collisionShapes))
127 shape->disconnect(this);
129}
130
131QQmlListProperty<QAbstractCollisionShape> QAbstractPhysicsNode::collisionShapes()
132{
133 return QQmlListProperty<QAbstractCollisionShape>(
134 this, nullptr, QAbstractPhysicsNode::qmlAppendShape,
135 QAbstractPhysicsNode::qmlShapeCount, QAbstractPhysicsNode::qmlShapeAt,
136 QAbstractPhysicsNode::qmlClearShapes);
137}
138
139const QVector<QAbstractCollisionShape *> &QAbstractPhysicsNode::getCollisionShapesList() const
140{
141 return m_collisionShapes;
142}
143
145{
146 const auto pos = transform.p;
147 const auto rotation = transform.q;
148 const auto qtPosition = QVector3D(pos.x, pos.y, pos.z);
149 const auto qtRotation = QQuaternion(rotation.w, rotation.x, rotation.y, rotation.z);
150
151 // Get this nodes parent transform
152 const QQuick3DNode *parentNode = static_cast<QQuick3DNode *>(parentItem());
153
154 if (!parentNode) {
155 // then it is the same space
156 setRotation(qtRotation);
157 setPosition(qtPosition);
158 } else {
160 const auto relativeRotation = parentNode->sceneRotation().inverted() * qtRotation;
161 setRotation(relativeRotation);
162 }
163}
164
166{
167 return m_sendContactReports;
168}
169
171{
172 if (m_sendContactReports == sendContactReports)
173 return;
174
175 m_sendContactReports = sendContactReports;
176 emit sendContactReportsChanged(m_sendContactReports);
177}
178
180{
181 return m_receiveContactReports;
182}
183
185{
186 if (m_receiveContactReports == receiveContactReports)
187 return;
188
189 m_receiveContactReports = receiveContactReports;
190 emit receiveContactReportsChanged(m_receiveContactReports);
191}
192
194{
195 return m_sendTriggerReports;
196}
197
198void QAbstractPhysicsNode::setSendTriggerReports(bool sendTriggerReports)
199{
200 if (m_sendTriggerReports == sendTriggerReports)
201 return;
202
203 m_sendTriggerReports = sendTriggerReports;
204 emit sendTriggerReportsChanged(m_sendTriggerReports);
205}
206
208{
209 return m_receiveTriggerReports;
210}
211
212void QAbstractPhysicsNode::setReceiveTriggerReports(bool receiveTriggerReports)
213{
214 if (m_receiveTriggerReports == receiveTriggerReports)
215 return;
216
217 m_receiveTriggerReports = receiveTriggerReports;
218 emit receiveTriggerReportsChanged(m_receiveTriggerReports);
219}
220
222 const QVector<QVector3D> &positions,
223 const QVector<QVector3D> &impulses,
224 const QVector<QVector3D> &normals)
225{
226 emit bodyContact(body, positions, impulses, normals);
227}
228
229void QAbstractPhysicsNode::onShapeDestroyed(QObject *object)
230{
231 m_collisionShapes.removeAll(static_cast<QAbstractCollisionShape *>(object));
232}
233
234void QAbstractPhysicsNode::onShapeNeedsRebuild(QObject * /*object*/)
235{
236 m_shapesDirty = true;
237}
238
239void QAbstractPhysicsNode::qmlAppendShape(QQmlListProperty<QAbstractCollisionShape> *list,
241{
242 if (shape == nullptr)
243 return;
244 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
245 self->m_collisionShapes.push_back(shape);
246 self->m_hasStaticShapes = self->m_hasStaticShapes || shape->isStaticShape();
247
248 if (shape->parentItem() == nullptr) {
249 // If the material has no parent, check if it has a hierarchical parent that's a
250 // QQuick3DObject and re-parent it to that, e.g., inline materials
251 QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(shape->parent());
252 if (parentItem) {
253 shape->setParentItem(parentItem);
254 } else { // If no valid parent was found, make sure the material refs our scene manager
255 const auto &scenManager = QQuick3DObjectPrivate::get(self)->sceneManager;
256 if (scenManager)
257 QQuick3DObjectPrivate::refSceneManager(shape, *scenManager);
258 // else: If there's no scene manager, defer until one is set, see itemChange()
259 }
260 }
261
262 // Make sure materials are removed when destroyed
264 &QAbstractPhysicsNode::onShapeDestroyed);
265
266 // Connect to rebuild signal
268 &QAbstractPhysicsNode::onShapeNeedsRebuild);
269}
270
272QAbstractPhysicsNode::qmlShapeAt(QQmlListProperty<QAbstractCollisionShape> *list, qsizetype index)
273{
274 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
275 return self->m_collisionShapes.at(index);
276}
277
278qsizetype QAbstractPhysicsNode::qmlShapeCount(QQmlListProperty<QAbstractCollisionShape> *list)
279{
280 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
281 return self->m_collisionShapes.count();
282}
283
284void QAbstractPhysicsNode::qmlClearShapes(QQmlListProperty<QAbstractCollisionShape> *list)
285{
286 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
287 for (const auto &shape : std::as_const(self->m_collisionShapes)) {
288 if (shape->parentItem() == nullptr)
289 QQuick3DObjectPrivate::get(shape)->derefSceneManager();
290 }
291 self->m_hasStaticShapes = false;
292 for (auto shape : std::as_const(self->m_collisionShapes))
293 shape->disconnect(self);
294 self->m_collisionShapes.clear();
295}
296
298{
299 return m_filterGroup;
300}
301
302void QAbstractPhysicsNode::setfilterGroup(int newfilterGroup)
303{
304 if (m_filterGroup == newfilterGroup)
305 return;
306 m_filterGroup = newfilterGroup;
307 m_filtersDirty = true;
308 emit filterGroupChanged();
309}
310
312{
313 return m_filterIgnoreGroups;
314}
315
316void QAbstractPhysicsNode::setFilterIgnoreGroups(int newFilterIgnoreGroups)
317{
318 if (m_filterIgnoreGroups == newFilterIgnoreGroups)
319 return;
320 m_filterIgnoreGroups = newFilterIgnoreGroups;
321 m_filtersDirty = true;
322 emit filterIgnoreGroupsChanged();
323}
324
void needsRebuild(QObject *)
virtual bool isStaticShape() const =0
void sendContactReportsChanged(float sendContactReports)
const QVector< QAbstractCollisionShape * > & getCollisionShapesList() const
void updateFromPhysicsTransform(const physx::PxTransform &transform)
QAbstractPhysicsNode()
\qmltype PhysicsNode \inherits Node \inqmlmodule QtQuick3D.Physics
void bodyContact(QAbstractPhysicsNode *body, const QVector< QVector3D > &positions, const QVector< QVector3D > &impulses, const QVector< QVector3D > &normals)
void setSendContactReports(bool sendContactReports)
void setReceiveContactReports(bool receiveContactReports)
void receiveContactReportsChanged(float receiveContactReports)
void registerContact(QAbstractPhysicsNode *body, const QVector< QVector3D > &positions, const QVector< QVector3D > &impulses, const QVector< QVector3D > &normals)
QQmlListProperty< QAbstractCollisionShape > collisionShapes
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
static void registerNode(QAbstractPhysicsNode *physicsNode)
static void deregisterNode(QAbstractPhysicsNode *physicsNode)
The QQuaternion class represents a quaternion consisting of a vector and scalar.
void setRotation(const QQuaternion &rotation)
Q_INVOKABLE QVector3D mapPositionFromScene(const QVector3D &scenePosition) const
\qmlmethod vector3d QtQuick3D::Node::mapPositionFromScene(vector3d scenePosition)
QQuick3DNode * parentNode() const
QQuaternion rotation
QQuaternion sceneRotation
void refSceneManager(QQuick3DSceneManager &)
static QQuick3DObjectPrivate * get(QQuick3DObject *item)
\qmltype Object3D \inqmlmodule QtQuick3D \instantiates QQuick3DObject \inherits QtObject
QQuick3DObject * parent
\qmlproperty Object3D QtQuick3D::Object3D::parent This property holds the parent of the Object3D in a...
void setParentItem(QQuick3DObject *parentItem)
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
void push_back(QChar c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.h:957
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1226
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4833
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
Combined button and popup list for selecting options.
QString self
Definition language.cpp:58
static const QCssKnownValue positions[NumKnownPositionModes - 1]
n void setPosition(void) \n\
GLuint index
[2]
GLuint GLenum GLenum transform
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:165
QList< int > list
[14]
myObject disconnect()
[26]