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\section2 Casting a Ray on Every Frame
62
63The query methods run once, when they are called. For a ray that has to be
64checked on every frame, such as a ground probe, a suspension ray or a line of
65sight, use a \l Raycaster instead. It is a node, so the ray follows its own
66transform, 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
69same rules as a single raycast query, and can also leave out the body it is
70mounted on and any bodies listed in its
71\l {Raycaster::excludedBodies}{excludedBodies}.
72
73\section1 Query Mechanics and Key Nuances
74
75When using scene queries, several system behaviors should be taken
76into consideration.
77
78\section2 Bodies Excluded from Queries
79
80Trigger bodies, such as \l TriggerBody, are not physical colliders and never appear in
81scene query results. Use an overlap query if you need the equivalent of a trigger volume
82that you can poll on demand.
83
84\section2 Filter Groups Do Not Apply
85
86The \l {PhysicsNode::filterGroup} {filterGroup} and
87\l {PhysicsNode::filterIgnoreGroups} {filterIgnoreGroups} properties control which bodies
88collide with each other during simulation. They have no effect on scene queries: a query
89evaluates every collider matching its \c includeStatic and \c includeDynamic arguments,
90regardless of the filter groups involved. If a query needs to ignore specific bodies,
91filter the results after the query returns. A \l Raycaster, which keeps only its closest
92hit, takes the bodies to ignore in \l {Raycaster::excludedBodies}{excludedBodies} instead.
93
94\section2 Query Shape Transforms
95
96For \b Sweep and \b Overlap queries, the provided \l CollisionShape defines not only
97the geometry and scale, but also the starting position and orientation for the query.
98The shape's scene transform (including parent node transformations) is baked directly
99into the query execution.
100
101\section2 Asynchronous Frame Timing and Latency
102
103The physics simulation steps concurrently alongside QML scene graph rendering.
104Scene queries executed while a physics step is in flight reflect object
105positions, poses, and bounding structures from the \e {last completed physics
106frame}.
107
108Consequently, queries triggered inside QML event handlers (such as \c onClicked
109or a \c Timer) operate on the state of the last completed step. That is the same
110state the scene's visual representation reflects, so a query result agrees with
111what is currently rendered; it does not account for motion occurring during the
112step being computed.
113
114\section2 Triangle Mesh Multi-Hit Behavior
115
116Multi-hit queries against complex triangle meshes or heightfield shapes yield a
117single intersection result per mesh object (the closest entry point). Individual
118triangles of a single mesh are not reported as separate hits.
119
120Furthermore, rays or volume sweeps that originate from \e inside a mesh or exit
121through back-facing surface triangles will not report hits for that mesh.
122
123\section2 Result Ordering in Multi-Queries
124
125Hit 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
128distance.} If a distance-sorted list is required, applications should sort the
129resulting list explicitly.
130
131\section2 Minimum Translation Distance (MTD) in Initial Overlaps
132
133By default, sweep queries handle initial overlaps with existing colliders using
134Minimum Translation Distance (MTD). If a query shape already overlaps a body at its
135starting position, the hit result reports a negative \c distance (representing the
136penetration depth), along with the separation \c position and \c normal.
137
138\section2 Character Controller Query Hits
139
140When scene queries hit a \l CharacterController, the returned hit structure
141(\l queryHit, or \l locationHit) will contain a valid \c body, but
142its \c shape property will be \c null. This occurs because the underlying
143character proxy shape does not maintain individual shape metadata. Applications
144should check if \c shape is \c null before accessing shape-specific properties.
145
146\section1 Filtering and Optimization Best Practices
147
148To 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
163The \l {Qt Quick 3D Physics - Queries Example} shows how to get physical bodies from the scene.
164
165\sa PhysicsWorld, locationHit, queryHit
166*/