Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtquick3dphysics-queries.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquick3dphysics-queries.html
6
\title Qt Quick 3D Physics Scene Queries
7
\brief Overview of scene query mechanics, types, and best practices in Qt
8
Quick 3D Physics.
9
10
Scene queries allow applications to test spatial relationships and detect
11
collisions programmatically within a \l PhysicsWorld. They enable mechanics
12
such as line-of-sight checks, spatial probing, volume clearance testing, and
13
interactive picking without relying on standard physical contact callbacks.
14
15
\section1 Types of Scene Queries
16
17
Qt Quick 3D Physics provides three main query modes, each optimized for different
18
spatial operations:
19
20
\table
21
\header
22
\li Query Type
23
\li Operation
24
\li Typical Use Cases
25
\row
26
\li \b Raycast
27
\li Casts an infinitely thin line segment along a direction vector.
28
\li Line-of-sight checks, weapon ballistics, laser pointers, user object
29
picking.
30
\row
31
\li \b Sweep
32
\li Sweeps a 3D volume (\l CollisionShape) from its scene position along a linear
33
direction vector.
34
\li Character volume clearance checks, wall-sliding, predictive movement
35
checks.
36
\row
37
\li \b Overlap
38
\li Tests a stationary 3D volume (\l CollisionShape) at its current scene position.
39
\li Spawn-point occupation, area-of-effect (AoE) damage, proximity
40
detection.
41
\endtable
42
43
Each query mode is split into three performance variants depending on the detail
44
level required:
45
46
\list
47
\li \b {Test Queries} (\c testRaycastQuery, \c testSweepQuery,
48
\c testOverlapQuery):
49
Boolean checks that terminate immediately upon finding the first
50
intersection. They return no detailed hit data, making them the fastest
51
option for occlusion or clearance validation.
52
\li \b {Single Queries} (\c singleRaycastQuery, \c singleSweepQuery):
53
Return detailed information (\l locationHit, \l queryHit)
54
for the \b closest intersecting body.
55
\li \b {Multi Queries} (\c multiRaycastQuery, \c multiSweepQuery,
56
\c multiOverlapQuery):
57
Return a list of hit objects for all intersecting bodies along the query
58
path or volume.
59
\endlist
60
61
\section2 Casting a Ray on Every Frame
62
63
The query methods run once, when they are called. For a ray that has to be
64
checked on every frame, such as a ground probe, a suspension ray or a line of
65
sight, use a \l Raycaster instead. It is a node, so the ray follows its own
66
transform, and it is cast once per physics frame, before
67
\l {PhysicsWorld::frameDone}{frameDone}, with the closest hit kept in its
68
\l {Raycaster::hit}{hit} property for bindings to use. A Raycaster follows the
69
same rules as a single raycast query, and can also leave out the body it is
70
mounted on and any bodies listed in its
71
\l {Raycaster::excludedBodies}{excludedBodies}.
72
73
\section1 Query Mechanics and Key Nuances
74
75
When using scene queries, several system behaviors should be taken
76
into consideration.
77
78
\section2 Bodies Excluded from Queries
79
80
Trigger bodies, such as \l TriggerBody, are not physical colliders and never appear in
81
scene query results. Use an overlap query if you need the equivalent of a trigger volume
82
that you can poll on demand.
83
84
\section2 Filter Groups Do Not Apply
85
86
The \l {PhysicsNode::filterGroup} {filterGroup} and
87
\l {PhysicsNode::filterIgnoreGroups} {filterIgnoreGroups} properties control which bodies
88
collide with each other during simulation. They have no effect on scene queries: a query
89
evaluates every collider matching its \c includeStatic and \c includeDynamic arguments,
90
regardless of the filter groups involved. If a query needs to ignore specific bodies,
91
filter the results after the query returns. A \l Raycaster, which keeps only its closest
92
hit, takes the bodies to ignore in \l {Raycaster::excludedBodies}{excludedBodies} instead.
93
94
\section2 Query Shape Transforms
95
96
For \b Sweep and \b Overlap queries, the provided \l CollisionShape defines not only
97
the geometry and scale, but also the starting position and orientation for the query.
98
The shape's scene transform (including parent node transformations) is baked directly
99
into the query execution.
100
101
\section2 Asynchronous Frame Timing and Latency
102
103
The physics simulation steps concurrently alongside QML scene graph rendering.
104
Scene queries executed while a physics step is in flight reflect object
105
positions, poses, and bounding structures from the \e {last completed physics
106
frame}.
107
108
Consequently, queries triggered inside QML event handlers (such as \c onClicked
109
or a \c Timer) operate on the state of the last completed step. That is the same
110
state the scene's visual representation reflects, so a query result agrees with
111
what is currently rendered; it does not account for motion occurring during the
112
step being computed.
113
114
\section2 Triangle Mesh Multi-Hit Behavior
115
116
Multi-hit queries against complex triangle meshes or heightfield shapes yield a
117
single intersection result per mesh object (the closest entry point). Individual
118
triangles of a single mesh are not reported as separate hits.
119
120
Furthermore, rays or volume sweeps that originate from \e inside a mesh or exit
121
through back-facing surface triangles will not report hits for that mesh.
122
123
\section2 Result Ordering in Multi-Queries
124
125
Hit objects returned by multi-queries (\c multiRaycastQuery, \c multiSweepQuery,
126
\c multiOverlapQuery) are populated based on spatial tree traversal order.
127
\b {The results in the returned list are not guaranteed to be ordered by
128
distance.} If a distance-sorted list is required, applications should sort the
129
resulting list explicitly.
130
131
\section2 Minimum Translation Distance (MTD) in Initial Overlaps
132
133
By default, sweep queries handle initial overlaps with existing colliders using
134
Minimum Translation Distance (MTD). If a query shape already overlaps a body at its
135
starting position, the hit result reports a negative \c distance (representing the
136
penetration depth), along with the separation \c position and \c normal.
137
138
\section2 Character Controller Query Hits
139
140
When scene queries hit a \l CharacterController, the returned hit structure
141
(\l queryHit, or \l locationHit) will contain a valid \c body, but
142
its \c shape property will be \c null. This occurs because the underlying
143
character proxy shape does not maintain individual shape metadata. Applications
144
should check if \c shape is \c null before accessing shape-specific properties.
145
146
\section1 Filtering and Optimization Best Practices
147
148
To achieve optimal performance when using scene queries:
149
150
\list
151
\li \b {Use Test Queries First:} Use \c testRaycastQuery or \c testSweepQuery
152
when you only need a \c true / \c false answer (e.g., AI visibility
153
checks). They bypass allocation and computation of hit geometry.
154
\li \b {Filter Body Types:} Constrain query searches using the \c includeStatic
155
and \c includeDynamic parameters. If a raycast only needs to hit dynamic
156
objects, setting \c includeStatic to \c false reduces traversal overhead.
157
\li \b {Limit Distance:} Keep \c maxDistance as short as practical to minimize
158
the number of candidate bounds evaluated by the spatial acceleration
159
structures.
160
\endlist
161
162
\section1 Example
163
The \l {Qt Quick 3D Physics - Queries Example} shows how to get physical bodies from the scene.
164
165
\sa PhysicsWorld, locationHit, queryHit
166
*/
qtquick3dphysics
src
quick3dphysics
doc
src
qtquick3dphysics-queries.qdoc
Generated on
for Qt by
1.16.1