Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qabstracttriggerregion.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qabstracttriggerregion_p.h"
6
7
#
include
<
QtCore
/
QPointer
>
8
#
include
<
QtCore
/
QVarLengthArray
>
9
10
QT_BEGIN_NAMESPACE
11
12
QAbstractTriggerRegion::QAbstractTriggerRegion() =
default
;
13
14
void
QAbstractTriggerRegion::registerCollision(QAbstractPhysicsNode *collision, ShapePair shapes)
15
{
16
// PhysX reports one pair per pair of shapes, so a body with several shapes
17
// inside the region, or a region with several shapes around one body, is
18
// reported once per pair. Keeping the pairs apart means that one of them no
19
// longer overlapping does not report the whole body as having left, and that
20
// the same pair reported again is the same overlap.
21
const
auto
it = m_collisions.find(collision);
22
if
(it != m_collisions.end()) {
23
// Another pair of a body already inside, or one of its pairs reported
24
// again after a rebuild, which is what says it still overlaps. A body
25
// stays followed for as long as it is inside, whatever it asks for in
26
// the meantime, or the report it is owed on the way out never comes.
27
it->pairs.insert(shapes);
28
it->unreported.remove(shapes);
29
return
;
30
}
31
32
// What a body asks for is read once, as it enters, so that the reports on
33
// the way out are the ones that were made on the way in. One that asks for
34
// neither is followed all the same, or it would be asked again every time
35
// its shapes are rebuilt and could be reported as entering without having
36
// moved.
37
const
bool
reports = countsBody(collision);
38
const
bool
receives = reportsToBodies() && collision->receiveTriggerReports();
39
40
Overlaps &overlaps = m_collisions[collision];
41
overlaps.pairs.insert(shapes);
42
overlaps.reports = reports;
43
overlaps.receives = receives;
44
if
(reports)
45
++m_reportedCount;
46
47
// Nothing takes a body off this list when it is deleted while still inside,
48
// and a field dereferences what is on it every frame.
49
connect(collision, &QObject::destroyed,
this
, &QAbstractTriggerRegion::dropDestroyedBody,
50
Qt::UniqueConnection);
51
reportEntered(collision, reports, receives);
52
}
53
54
void
QAbstractTriggerRegion::deregisterCollision(QAbstractPhysicsNode *collision, ShapePair shapes)
55
{
56
const
auto
it = m_collisions.find(collision);
57
if
(it == m_collisions.end())
58
return
;
59
60
it->pairs.remove(shapes);
61
it->unreported.remove(shapes);
62
if
(!it->pairs.isEmpty())
63
return
;
64
65
const
bool
reports = it->reports;
66
const
bool
receives = it->receives;
67
if
(reports)
68
--m_reportedCount;
69
m_collisions.erase(it);
70
disconnect(collision, &QObject::destroyed,
this
, &QAbstractTriggerRegion::dropDestroyedBody);
71
reportExited(collision, reports, receives);
72
}
73
74
// Both take what to report by value and hold the body and this region weakly,
75
// since the first report can leave the rest with a body that is gone, a region
76
// that is gone, and an entry taken out from under a reference into the list.
77
// That is reachable from dropUnreportedOverlaps(), which runs from the frame and
78
// not from a callback holding the world's lock
79
80
void
QAbstractTriggerRegion::reportEntered(QAbstractPhysicsNode *body,
bool
reports,
bool
receives)
81
{
82
const
QPointer<QAbstractTriggerRegion> self(
this
);
83
const
QPointer<QAbstractPhysicsNode> weakBody(body);
84
85
if
(reports) {
86
bodyDidEnter(body);
87
// Off the list again is a handler having deleted the body it was just
88
// handed, which counted it out and reported that itself.
89
if
(!self.isNull() && m_collisions.contains(body))
90
emit collisionCountChanged();
91
}
92
if
(receives && !self.isNull() && !weakBody.isNull())
93
emit body->enteredTriggerBody(
this
);
94
}
95
96
void
QAbstractTriggerRegion::reportExited(QAbstractPhysicsNode *body,
bool
reports,
bool
receives)
97
{
98
const
QPointer<QAbstractTriggerRegion> self(
this
);
99
const
QPointer<QAbstractPhysicsNode> weakBody(body);
100
101
if
(reports) {
102
bodyDidExit(body);
103
if
(!self.isNull())
104
emit collisionCountChanged();
105
}
106
if
(receives && !self.isNull() && !weakBody.isNull())
107
emit body->exitedTriggerBody(
this
);
108
}
109
110
void
QAbstractTriggerRegion::invalidateOverlaps(QAbstractPhysicsNode *node)
111
{
112
// Every shape of node has just been replaced by a new one. PhysX does report
113
// the pairs of the shapes it took away, but as pairs of a shape that is
114
// already gone, which cannot be told apart and is ignored. So the pairs of
115
// those shapes are held on to until the next round of reports, which says
116
// which of them the new shapes are in: overlapping shapes are reported as
117
// found again, and what is not reported again is no longer overlapping.
118
//
119
// Holding on to them, rather than dropping them and letting the reports
120
// build the list up again, is what keeps a body inside a region from being
121
// reported as leaving and entering again every time its shapes are rebuilt,
122
// which is once per frame if any of them is animated.
123
if
(node ==
this
) {
124
for
(Overlaps &overlaps : m_collisions)
125
overlaps.unreported = overlaps.pairs;
126
return
;
127
}
128
129
const
auto
it = m_collisions.find(node);
130
if
(it != m_collisions.end())
131
it->unreported = it->pairs;
132
}
133
134
void
QAbstractTriggerRegion::dropUnreportedOverlaps()
135
{
136
struct
PendingExit
137
{
138
QPointer<QAbstractPhysicsNode> body;
139
bool
reports =
false
;
140
bool
receives =
false
;
141
};
142
QVarLengthArray<PendingExit, 4> pending;
143
144
for
(
auto
it = m_collisions.begin(); it != m_collisions.end();) {
145
if
(it->unreported.isEmpty()) {
146
++it;
147
continue
;
148
}
149
150
it->pairs.subtract(it->unreported);
151
it->unreported.clear();
152
153
if
(it->pairs.isEmpty()) {
154
QAbstractPhysicsNode *body = it.key();
155
disconnect(body, &QObject::destroyed,
this
, &QAbstractTriggerRegion::dropDestroyedBody);
156
if
(it->reports)
157
--m_reportedCount;
158
pending.append({ body, it->reports, it->receives });
159
it = m_collisions.erase(it);
160
}
else
{
161
++it;
162
}
163
}
164
165
// Reported once the list is settled, since a handler is free to register or
166
// deregister a collision on this region. A handler is also free to delete
167
// one of the bodies still waiting to be reported, or this region: one that
168
// no longer exists is not passed to a handler, the same way a body
169
// destroyed while inside the region is not, and a region that no longer
170
// exists reports nothing further.
171
//
172
// Several bodies can leave at once here, and all of them are out of the
173
// list before any of them is reported, so collisionCount is what it will
174
// be for the whole round rather than counting down through it.
175
const
QPointer<QAbstractTriggerRegion> self(
this
);
176
177
for
(
const
PendingExit &exit : pending) {
178
if
(self.isNull())
179
return
;
180
181
if
(!exit.body.isNull())
182
reportExited(exit.body.data(), exit.reports, exit.receives);
183
else
if
(exit.reports)
184
emit collisionCountChanged();
185
}
186
}
187
188
void
QAbstractTriggerRegion::dropDestroyedBody(QObject *body)
189
{
190
// Runs from ~QObject, so only the QObject part of the body is left: it is
191
// compared as a QObject and never passed on. No bodyDidExit() or
192
// exitedTriggerBody() either, since those would pass a half destroyed body
193
// to a handler, or read from one.
194
for
(
auto
it = m_collisions.cbegin(); it != m_collisions.cend(); ++it) {
195
if
(
static_cast
<QObject *>(it.key()) == body) {
196
const
bool
wasCounted = it->reports;
197
if
(wasCounted)
198
--m_reportedCount;
199
m_collisions.erase(it);
200
if
(wasCounted)
201
emit collisionCountChanged();
202
return
;
203
}
204
}
205
}
206
207
bool
QAbstractTriggerRegion::countsBody(
const
QAbstractPhysicsNode *body)
const
208
{
209
return
body->sendTriggerReports();
210
}
211
212
bool
QAbstractTriggerRegion::reportsToBodies()
const
213
{
214
return
false
;
215
}
216
217
int
QAbstractTriggerRegion::collisionCount()
const
218
{
219
return
m_reportedCount;
220
}
221
222
void
QAbstractTriggerRegion::bodyDidEnter(QAbstractPhysicsNode *) { }
223
224
void
QAbstractTriggerRegion::bodyDidExit(QAbstractPhysicsNode *) { }
225
226
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtquick3dphysics
src
quick3dphysics
qabstracttriggerregion.cpp
Generated on
for Qt by
1.18.0