Qt 6.x
The Qt SDK
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
88{
90}
91
93{
94 for (auto shape : std::as_const(m_collisionShapes))
95 shape->disconnect(this);
97}
98
100{
102 this, nullptr, QAbstractPhysicsNode::qmlAppendShape,
103 QAbstractPhysicsNode::qmlShapeCount, QAbstractPhysicsNode::qmlShapeAt,
104 QAbstractPhysicsNode::qmlClearShapes);
105}
106
108{
109 return m_collisionShapes;
110}
111
113{
114 const auto pos = transform.p;
115 const auto rotation = transform.q;
116 const auto qtPosition = QVector3D(pos.x, pos.y, pos.z);
117 const auto qtRotation = QQuaternion(rotation.w, rotation.x, rotation.y, rotation.z);
118
119 // Get this nodes parent transform
120 const QQuick3DNode *parentNode = static_cast<QQuick3DNode *>(parentItem());
121
122 if (!parentNode) {
123 // then it is the same space
124 setRotation(qtRotation);
125 setPosition(qtPosition);
126 } else {
128 const auto relativeRotation = parentNode->sceneRotation().inverted() * qtRotation;
129 setRotation(relativeRotation);
130 }
131}
132
134{
135 return m_sendContactReports;
136}
137
139{
140 if (m_sendContactReports == sendContactReports)
141 return;
142
143 m_sendContactReports = sendContactReports;
144 emit sendContactReportsChanged(m_sendContactReports);
145}
146
148{
149 return m_receiveContactReports;
150}
151
153{
154 if (m_receiveContactReports == receiveContactReports)
155 return;
156
157 m_receiveContactReports = receiveContactReports;
158 emit receiveContactReportsChanged(m_receiveContactReports);
159}
160
162{
163 return m_sendTriggerReports;
164}
165
166void QAbstractPhysicsNode::setSendTriggerReports(bool sendTriggerReports)
167{
168 if (m_sendTriggerReports == sendTriggerReports)
169 return;
170
171 m_sendTriggerReports = sendTriggerReports;
172 emit sendTriggerReportsChanged(m_sendTriggerReports);
173}
174
176{
177 return m_receiveTriggerReports;
178}
179
180void QAbstractPhysicsNode::setReceiveTriggerReports(bool receiveTriggerReports)
181{
182 if (m_receiveTriggerReports == receiveTriggerReports)
183 return;
184
185 m_receiveTriggerReports = receiveTriggerReports;
186 emit receiveTriggerReportsChanged(m_receiveTriggerReports);
187}
188
191 const QVector<QVector3D> &impulses,
192 const QVector<QVector3D> &normals)
193{
194 emit bodyContact(body, positions, impulses, normals);
195}
196
197void QAbstractPhysicsNode::onShapeDestroyed(QObject *object)
198{
199 m_collisionShapes.removeAll(static_cast<QAbstractCollisionShape *>(object));
200}
201
202void QAbstractPhysicsNode::onShapeNeedsRebuild(QObject * /*object*/)
203{
204 m_shapesDirty = true;
205}
206
207void QAbstractPhysicsNode::qmlAppendShape(QQmlListProperty<QAbstractCollisionShape> *list,
209{
210 if (shape == nullptr)
211 return;
212 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
213 self->m_collisionShapes.push_back(shape);
214 self->m_hasStaticShapes = self->m_hasStaticShapes || shape->isStaticShape();
215
216 if (shape->parentItem() == nullptr) {
217 // If the material has no parent, check if it has a hierarchical parent that's a
218 // QQuick3DObject and re-parent it to that, e.g., inline materials
219 QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(shape->parent());
220 if (parentItem) {
221 shape->setParentItem(parentItem);
222 } else { // If no valid parent was found, make sure the material refs our scene manager
223 const auto &scenManager = QQuick3DObjectPrivate::get(self)->sceneManager;
224 if (scenManager)
225 QQuick3DObjectPrivate::refSceneManager(shape, *scenManager);
226 // else: If there's no scene manager, defer until one is set, see itemChange()
227 }
228 }
229
230 // Make sure materials are removed when destroyed
232 &QAbstractPhysicsNode::onShapeDestroyed);
233
234 // Connect to rebuild signal
236 &QAbstractPhysicsNode::onShapeNeedsRebuild);
237}
238
240QAbstractPhysicsNode::qmlShapeAt(QQmlListProperty<QAbstractCollisionShape> *list, qsizetype index)
241{
242 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
243 return self->m_collisionShapes.at(index);
244}
245
246qsizetype QAbstractPhysicsNode::qmlShapeCount(QQmlListProperty<QAbstractCollisionShape> *list)
247{
248 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
249 return self->m_collisionShapes.count();
250}
251
252void QAbstractPhysicsNode::qmlClearShapes(QQmlListProperty<QAbstractCollisionShape> *list)
253{
254 QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
255 for (const auto &shape : std::as_const(self->m_collisionShapes)) {
256 if (shape->parentItem() == nullptr)
258 }
259 self->m_hasStaticShapes = false;
260 for (auto shape : std::as_const(self->m_collisionShapes))
261 shape->disconnect(self);
262 self->m_collisionShapes.clear();
263}
264
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
Definition qlist.h:74
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
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 QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
The QQuaternion class represents a quaternion consisting of a vector and scalar.
Definition qquaternion.h:21
float z() const
Returns the z coordinate of this quaternion's vector.
float x() const
Returns the x coordinate of this quaternion's vector.
float y() const
Returns the y coordinate of this quaternion's vector.
QQuaternion inverted() const
void setRotation(const QQuaternion &rotation)
Q_INVOKABLE QVector3D mapPositionFromScene(const QVector3D &scenePosition) const
\qmlmethod vector3d QtQuick3D::Node::mapPositionFromScene(vector3d scenePosition)
void setPosition(const QVector3D &position)
QQuick3DNode * parentNode() const
QQuaternion rotation
QQuaternion sceneRotation
QPointer< QQuick3DSceneManager > sceneManager
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:1107
void push_back(QChar c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.h:868
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1079
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4732
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:57
static const QCssKnownValue positions[NumKnownPositionModes - 1]
GLuint index
[2]
GLuint GLenum GLenum transform
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:70
QList< int > list
[14]
myObject disconnect()
[26]