aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3dphysics/qabstractphysicsnode.cpp
blob: b19b81cedba0ff9fab7b6c1de27471939b354626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qabstractphysicsnode_p.h"
#include <QtQuick3D/private/qquick3dobject_p.h>
#include <foundation/PxTransform.h>

#include "qphysicsworld_p.h"
QT_BEGIN_NAMESPACE

/*!
    \qmltype PhysicsNode
    \inherits Node
    \inqmlmodule QtQuick3D.Physics
    \since 6.4
    \brief Base type for all objects in the physics scene.

    PhysicsNode is the base type for all the objects that take part in the physics simulation. These
    objects have a position in three-dimensional space and a geometrical shape.
*/

/*!
    \qmlproperty list<CollisionShape> PhysicsNode::collisionShapes

    This property contains the list of collision shapes. These shapes will be combined and act as a
    single rigid body when interacting with other bodies.

    \sa {Qt Quick 3D Physics Shapes and Bodies}{Shapes and Bodies overview documentation}
*/

/*!
    \qmlproperty bool PhysicsNode::sendContactReports
    This property determines whether this body will send contact reports when colliding with other
    bodies.

    Default value: \c{false}
*/

/*!
    \qmlproperty bool PhysicsNode::receiveContactReports
    This property determines whether this body will receive contact reports when colliding with
    other bodies. If activated, this means that the bodyContact signal will be emitted on a
    collision with a body that has sendContactReports set to true.

    Default value: \c{false}
*/

/*!
    \qmlproperty bool PhysicsNode::sendTriggerReports
    This property determines whether this body will send reports when entering or leaving a trigger
    body.

    Default value: \c{false}
*/

/*!
    \qmlproperty bool PhysicsNode::receiveTriggerReports
    This property determines whether this body will receive reports when entering or leaving a
    trigger body.

    Default value: \c{false}
*/

/*!
    \qmlproperty int PhysicsNode::filterGroup
    This property determines what filter group this body is part of.

    Default value is \c 0.

    Range: \c{[0, 32]}

    \sa PhysicsNode::filterIgnoreGroups
*/

/*!
    \qmlproperty int PhysicsNode::filterIgnoreGroups
    This property determines what groups this body should filter out collisions with.

    \note This number is interpreted as a bitmask, meaning that if bit \c i is set then collisions
    with \l filterGroup number \c i will be filtered. For instance, to filter groups \c{1}, \c{3}
    and \c{4} then set the value to \c{0b11010}.

    \sa PhysicsNode::filterGroup
*/

/*!
    \qmlsignal PhysicsNode::bodyContact(PhysicsNode *body, list<vector3D> positions,
   list<vector3D> impulses, list<vector3D> normals)

    This signal is emitted when there is a collision between a dynamic body and any
    other body. The \l {PhysicsNode::} {receiveContactReports} in this body and \l {PhysicsNode::}
    {sendContactReports} in the colliding body need to be set to true. The parameters \a body, \a
    positions, \a impulses and \a normals contain the other body, position, impulse force and normal
    for each contact point at the same index.

    \sa CharacterController::shapeHit
    \sa PhysicsWorld::reportKinematicKinematicCollisions
    \sa PhysicsWorld::reportStaticKinematicCollisions
*/

/*!
    \qmlsignal PhysicsNode::enteredTriggerBody(TriggerBody *body)

    This signal is emitted when this body enters the specified trigger \a body.

    \note Only emitted when receiveTriggerReports is \c true
    \sa receiveTriggerReports exitedTriggerBody
*/

/*!
    \qmlsignal PhysicsNode::exitedTriggerBody(TriggerBody *body)

    This signal is emitted when this body exits the specified trigger \a body.

    \note Only emitted when receiveTriggerReports is \c true
    \sa receiveTriggerReports enteredTriggerBody
*/

QAbstractPhysicsNode::QAbstractPhysicsNode()
{
    QPhysicsWorld::registerNode(this);
}

QAbstractPhysicsNode::~QAbstractPhysicsNode()
{
    for (auto shape : std::as_const(m_collisionShapes))
        shape->disconnect(this);
    QPhysicsWorld::deregisterNode(this);
}

QQmlListProperty<QAbstractCollisionShape> QAbstractPhysicsNode::collisionShapes()
{
    return QQmlListProperty<QAbstractCollisionShape>(
            this, nullptr, QAbstractPhysicsNode::qmlAppendShape,
            QAbstractPhysicsNode::qmlShapeCount, QAbstractPhysicsNode::qmlShapeAt,
            QAbstractPhysicsNode::qmlClearShapes);
}

const QVector<QAbstractCollisionShape *> &QAbstractPhysicsNode::getCollisionShapesList() const
{
    return m_collisionShapes;
}

void QAbstractPhysicsNode::updateFromPhysicsTransform(const physx::PxTransform &transform)
{
    const auto pos = transform.p;
    const auto rotation = transform.q;
    const auto qtPosition = QVector3D(pos.x, pos.y, pos.z);
    const auto qtRotation = QQuaternion(rotation.w, rotation.x, rotation.y, rotation.z);

    // Get this nodes parent transform
    const QQuick3DNode *parentNode = static_cast<QQuick3DNode *>(parentItem());

    if (!parentNode) {
        // then it is the same space
        setRotation(qtRotation);
        setPosition(qtPosition);
    } else {
        setPosition(parentNode->mapPositionFromScene(qtPosition));
        const auto relativeRotation = parentNode->sceneRotation().inverted() * qtRotation;
        setRotation(relativeRotation);
    }
}

bool QAbstractPhysicsNode::sendContactReports() const
{
    return m_sendContactReports;
}

void QAbstractPhysicsNode::setSendContactReports(bool sendContactReports)
{
    if (m_sendContactReports == sendContactReports)
        return;

    m_sendContactReports = sendContactReports;
    emit sendContactReportsChanged(m_sendContactReports);
}

bool QAbstractPhysicsNode::receiveContactReports() const
{
    return m_receiveContactReports;
}

void QAbstractPhysicsNode::setReceiveContactReports(bool receiveContactReports)
{
    if (m_receiveContactReports == receiveContactReports)
        return;

    m_receiveContactReports = receiveContactReports;
    emit receiveContactReportsChanged(m_receiveContactReports);
}

bool QAbstractPhysicsNode::sendTriggerReports() const
{
    return m_sendTriggerReports;
}

void QAbstractPhysicsNode::setSendTriggerReports(bool sendTriggerReports)
{
    if (m_sendTriggerReports == sendTriggerReports)
        return;

    m_sendTriggerReports = sendTriggerReports;
    emit sendTriggerReportsChanged(m_sendTriggerReports);
}

bool QAbstractPhysicsNode::receiveTriggerReports() const
{
    return m_receiveTriggerReports;
}

void QAbstractPhysicsNode::setReceiveTriggerReports(bool receiveTriggerReports)
{
    if (m_receiveTriggerReports == receiveTriggerReports)
        return;

    m_receiveTriggerReports = receiveTriggerReports;
    emit receiveTriggerReportsChanged(m_receiveTriggerReports);
}

void QAbstractPhysicsNode::registerContact(QAbstractPhysicsNode *body,
                                           const QVector<QVector3D> &positions,
                                           const QVector<QVector3D> &impulses,
                                           const QVector<QVector3D> &normals)
{
    emit bodyContact(body, positions, impulses, normals);
}

void QAbstractPhysicsNode::onShapeDestroyed(QObject *object)
{
    m_collisionShapes.removeAll(static_cast<QAbstractCollisionShape *>(object));
}

void QAbstractPhysicsNode::onShapeNeedsRebuild(QObject * /*object*/)
{
    m_shapesDirty = true;
}

void QAbstractPhysicsNode::qmlAppendShape(QQmlListProperty<QAbstractCollisionShape> *list,
                                          QAbstractCollisionShape *shape)
{
    if (shape == nullptr)
        return;
    QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
    self->m_collisionShapes.push_back(shape);
    self->m_hasStaticShapes = self->m_hasStaticShapes || shape->isStaticShape();

    if (shape->parentItem() == nullptr) {
        // If the material has no parent, check if it has a hierarchical parent that's a
        // QQuick3DObject and re-parent it to that, e.g., inline materials
        QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(shape->parent());
        if (parentItem) {
            shape->setParentItem(parentItem);
        } else { // If no valid parent was found, make sure the material refs our scene manager
            const auto &scenManager = QQuick3DObjectPrivate::get(self)->sceneManager;
            if (scenManager)
                QQuick3DObjectPrivate::refSceneManager(shape, *scenManager);
            // else: If there's no scene manager, defer until one is set, see itemChange()
        }
    }

    // Make sure materials are removed when destroyed
    connect(shape, &QAbstractCollisionShape::destroyed, self,
            &QAbstractPhysicsNode::onShapeDestroyed);

    // Connect to rebuild signal
    connect(shape, &QAbstractCollisionShape::needsRebuild, self,
            &QAbstractPhysicsNode::onShapeNeedsRebuild);
}

QAbstractCollisionShape *
QAbstractPhysicsNode::qmlShapeAt(QQmlListProperty<QAbstractCollisionShape> *list, qsizetype index)
{
    QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
    return self->m_collisionShapes.at(index);
}

qsizetype QAbstractPhysicsNode::qmlShapeCount(QQmlListProperty<QAbstractCollisionShape> *list)
{
    QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
    return self->m_collisionShapes.count();
}

void QAbstractPhysicsNode::qmlClearShapes(QQmlListProperty<QAbstractCollisionShape> *list)
{
    QAbstractPhysicsNode *self = static_cast<QAbstractPhysicsNode *>(list->object);
    for (const auto &shape : std::as_const(self->m_collisionShapes)) {
        if (shape->parentItem() == nullptr)
            QQuick3DObjectPrivate::get(shape)->derefSceneManager();
    }
    self->m_hasStaticShapes = false;
    for (auto shape : std::as_const(self->m_collisionShapes))
        shape->disconnect(self);
    self->m_collisionShapes.clear();
}

int QAbstractPhysicsNode::filterGroup() const
{
    return m_filterGroup;
}

void QAbstractPhysicsNode::setfilterGroup(int newfilterGroup)
{
    if (m_filterGroup == newfilterGroup)
        return;
    m_filterGroup = newfilterGroup;
    m_filtersDirty = true;
    emit filterGroupChanged();
}

int QAbstractPhysicsNode::filterIgnoreGroups() const
{
    return m_filterIgnoreGroups;
}

void QAbstractPhysicsNode::setFilterIgnoreGroups(int newFilterIgnoreGroups)
{
    if (m_filterIgnoreGroups == newFilterIgnoreGroups)
        return;
    m_filterIgnoreGroups = newFilterIgnoreGroups;
    m_filtersDirty = true;
    emit filterIgnoreGroupsChanged();
}

QT_END_NAMESPACE