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
8Quick 3D Physics.
9
10Scene queries allow applications to test spatial relationships and detect
11collisions programmatically within a \l PhysicsWorld. They enable mechanics
12such as line-of-sight checks, spatial probing, volume clearance testing, and
13interactive picking without relying on standard physical contact callbacks.
14
15\section1 Types of Scene Queries
16
17Qt Quick 3D Physics provides three main query modes, each optimized for different
18spatial 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
43Each query mode is split into three performance variants depending on the detail
44level 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\section1 Query Mechanics and Key Nuances
62
63When using scene queries, several system behaviors should be taken
64into consideration.
65
66\section2 Bodies Excluded from Queries
67
68Trigger bodies, such as \l TriggerBody, are not physical colliders and never appear in
69scene query results. Use an overlap query if you need the equivalent of a trigger volume
70that you can poll on demand.
71
72\section2 Filter Groups Do Not Apply
73
74The \l {PhysicsNode::filterGroup} {filterGroup} and
75\l {PhysicsNode::filterIgnoreGroups} {filterIgnoreGroups} properties control which bodies
76collide with each other during simulation. They have no effect on scene queries: a query
77evaluates every collider matching its \c includeStatic and \c includeDynamic arguments,
78regardless of the filter groups involved. If a query needs to ignore specific bodies,
79filter the results after the query returns.
80
81\section2 Query Shape Transforms
82
83For \b Sweep and \b Overlap queries, the provided \l CollisionShape defines not only
84the geometry and scale, but also the starting position and orientation for the query.
85The shape's scene transform (including parent node transformations) is baked directly
86into the query execution.
87
88\section2 Asynchronous Frame Timing and Latency
89
90The physics simulation steps concurrently alongside QML scene graph rendering.
91Scene queries executed while a physics step is in flight reflect object
92positions, poses, and bounding structures from the \e {last completed physics
93frame}.
94
95Consequently, queries triggered inside QML event handlers (such as \c onClicked
96or a \c Timer) operate on the state of the last completed step. That is the same
97state the scene's visual representation reflects, so a query result agrees with
98what is currently rendered; it does not account for motion occurring during the
99step being computed.
100
101\section2 Triangle Mesh Multi-Hit Behavior
102
103Multi-hit queries against complex triangle meshes or heightfield shapes yield a
104single intersection result per mesh object (the closest entry point). Individual
105triangles of a single mesh are not reported as separate hits.
106
107Furthermore, rays or volume sweeps that originate from \e inside a mesh or exit
108through back-facing surface triangles will not report hits for that mesh.
109
110\section2 Result Ordering in Multi-Queries
111
112Hit objects returned by multi-queries (\c multiRaycastQuery, \c multiSweepQuery,
113\c multiOverlapQuery) are populated based on spatial tree traversal order.
114\b {The results in the returned list are not guaranteed to be ordered by
115distance.} If a distance-sorted list is required, applications should sort the
116resulting list explicitly.
117
118\section2 Minimum Translation Distance (MTD) in Initial Overlaps
119
120By default, sweep queries handle initial overlaps with existing colliders using
121Minimum Translation Distance (MTD). If a query shape already overlaps a body at its
122starting position, the hit result reports a negative \c distance (representing the
123penetration depth), along with the separation \c position and \c normal.
124
125\section2 Character Controller Query Hits
126
127When scene queries hit a \l CharacterController, the returned hit structure
128(\l queryHit, or \l locationHit) will contain a valid \c body, but
129its \c shape property will be \c null. This occurs because the underlying
130character proxy shape does not maintain individual shape metadata. Applications
131should check if \c shape is \c null before accessing shape-specific properties.
132
133\section1 Filtering and Optimization Best Practices
134
135To achieve optimal performance when using scene queries:
136
137\list
138\li \b {Use Test Queries First:} Use \c testRaycastQuery or \c testSweepQuery
139 when you only need a \c true / \c false answer (e.g., AI visibility
140 checks). They bypass allocation and computation of hit geometry.
141\li \b {Filter Body Types:} Constrain query searches using the \c includeStatic
142 and \c includeDynamic parameters. If a raycast only needs to hit dynamic
143 objects, setting \c includeStatic to \c false reduces traversal overhead.
144\li \b {Limit Distance:} Keep \c maxDistance as short as practical to minimize
145 the number of candidate bounds evaluated by the spatial acceleration
146 structures.
147\endlist
148
149\section1 Example
150The \l {Qt Quick 3D Physics - Queries Example} shows how to get physical bodies from the scene.
151
152\sa PhysicsWorld, locationHit, queryHit
153*/