avian3d/spatial_query/mod.rs
1//! Functionality for performing ray casts, shape casts, and other spatial queries.
2//!
3//! Spatial queries query the world for geometric information about [`Collider`s](Collider)
4//! and various types of intersections. Currently, four types of spatial queries are supported:
5//!
6//! - [Raycasts](#raycasting)
7//! - [Shapecasts](#shapecasting)
8//! - [Point projection](#point-projection)
9//! - [Intersection tests](#intersection-tests)
10//!
11//! All spatial queries can be done using the various methods provided by the [`SpatialQuery`] system parameter.
12//!
13//! Raycasting and shapecasting can also be done with a component-based approach using the [`RayCaster`] and
14//! [`ShapeCaster`] components. They enable performing casts every frame in a way that is often more convenient
15//! than the normal [`SpatialQuery`] methods. See their documentation for more information.
16//!
17//! # Raycasting
18//!
19//! **Raycasting** is a spatial query that finds intersections between colliders and a half-line. This can be used for
20//! a variety of things like getting information about the environment for character controllers and AI,
21//! and even rendering using ray tracing.
22//!
23//! For each hit during raycasting, the hit entity, a distance, and a normal will be stored in [`RayHitData`].
24//! The distance is the distance from the ray origin to the point of intersection, indicating how far the ray travelled.
25//!
26//! There are two ways to perform raycasts.
27//!
28//! 1. For simple raycasts, use the [`RayCaster`] component. It returns the results of the raycast
29//! in the [`RayHits`] component every frame. It uses local coordinates, so it will automatically follow the entity
30//! it's attached to or its parent.
31//! 2. When you need more control or don't want to cast every frame, use the raycasting methods provided by
32//! [`SpatialQuery`], like [`cast_ray`](SpatialQuery::cast_ray), [`ray_hits`](SpatialQuery::ray_hits) or
33//! [`ray_hits_callback`](SpatialQuery::ray_hits_callback).
34//!
35//! See the documentation of the components and methods for more information.
36//!
37//! A simple example using the component-based method looks like this:
38//!
39//! ```
40//! # #[cfg(feature = "2d")]
41//! # use avian2d::prelude::*;
42//! # #[cfg(feature = "3d")]
43//! use avian3d::prelude::*;
44//! use bevy::prelude::*;
45//!
46//! # #[cfg(all(feature = "3d", feature = "f32"))]
47//! fn setup(mut commands: Commands) {
48//! // Spawn a ray caster at the center with the rays travelling right
49//! commands.spawn(RayCaster::new(Vec3::ZERO, Dir3::X));
50//! // ...spawn colliders and other things
51//! }
52//!
53//! # #[cfg(all(feature = "3d", feature = "f32"))]
54//! fn print_hits(query: Query<(&RayCaster, &RayHits)>) {
55//! for (ray, hits) in &query {
56//! // For the faster iterator that isn't sorted, use `.iter()`
57//! for hit in hits.iter_sorted() {
58//! println!(
59//! "Hit entity {} at {} with normal {}",
60//! hit.entity,
61//! ray.get_global_point(hit.distance),
62//! hit.normal,
63//! );
64//! }
65//! }
66//! }
67//! ```
68//!
69//! To specify which colliders should be considered in the query, use a [spatial query filter](`SpatialQueryFilter`).
70//!
71//! # Shapecasting
72//!
73//! **Shapecasting** or **sweep testing** is a spatial query that finds intersections between colliders and a shape
74//! that is travelling along a half-line. It is very similar to [raycasting](#raycasting), but instead of a "point"
75//! we have an entire shape travelling along a half-line. One use case is determining how far an object can move
76//! before it hits the environment.
77//!
78//! For each hit during shapecasting, the hit entity, a distance, two world-space points of intersection and two world-space
79//! normals will be stored in [`ShapeHitData`]. The distance refers to how far the shape travelled before the initial hit.
80//!
81//! There are two ways to perform shapecasts.
82//!
83//! 1. For simple shapecasts, use the [`ShapeCaster`] component. It returns the results of the shapecast
84//! in the [`ShapeHits`] component every frame. It uses local coordinates, so it will automatically follow the entity
85//! it's attached to or its parent.
86//! 2. When you need more control or don't want to cast every frame, use the shapecasting methods provided by
87//! [`SpatialQuery`], like [`cast_shape`](SpatialQuery::cast_shape), [`shape_hits`](SpatialQuery::shape_hits) or
88//! [`shape_hits_callback`](SpatialQuery::shape_hits_callback).
89//!
90//! See the documentation of the components and methods for more information.
91//!
92//! A simple example using the component-based method looks like this:
93//!
94//! ```
95//! # #[cfg(feature = "2d")]
96//! # use avian2d::prelude::*;
97//! # #[cfg(feature = "3d")]
98//! use avian3d::prelude::*;
99//! use bevy::prelude::*;
100//!
101//! # #[cfg(all(feature = "3d", feature = "f32"))]
102//! fn setup(mut commands: Commands) {
103//! // Spawn a shape caster with a sphere shape at the center travelling right
104//! commands.spawn(ShapeCaster::new(
105//! Collider::sphere(0.5), // Shape
106//! Vec3::ZERO, // Origin
107//! Quat::default(), // Shape rotation
108//! Dir3::X // Direction
109//! ));
110//! // ...spawn colliders and other things
111//! }
112//!
113//! fn print_hits(query: Query<(&ShapeCaster, &ShapeHits)>) {
114//! for (shape_caster, hits) in &query {
115//! for hit in hits.iter() {
116//! println!("Hit entity {}", hit.entity);
117//! }
118//! }
119//! }
120//! ```
121//!
122//! To specify which colliders should be considered in the query, use a [spatial query filter](`SpatialQueryFilter`).
123//!
124//! # Point projection
125//!
126//! **Point projection** is a spatial query that projects a point on the closest collider. It returns the collider's
127//! entity, the projected point, and whether the point is inside of the collider.
128//!
129//! Point projection can be done with the [`project_point`](SpatialQuery::project_point) method of the [`SpatialQuery`]
130//! system parameter. See its documentation for more information.
131//!
132//! To specify which colliders should be considered in the query, use a [spatial query filter](`SpatialQueryFilter`).
133//!
134//! # Intersection tests
135//!
136//! **Intersection tests** are spatial queries that return the entities of colliders that are intersecting a given
137//! shape or area.
138//!
139//! There are three types of intersection tests. They are all methods of the [`SpatialQuery`] system parameter,
140//! and they all have callback variants that call a given callback on each intersection.
141//!
142//! - [`point_intersections`](SpatialQuery::point_intersections): Finds all entities with a collider that contains
143//! the given point.
144//! - [`aabb_intersections_with_aabb`](SpatialQuery::aabb_intersections_with_aabb):
145//! Finds all entities with a [`ColliderAabb`] that is intersecting the given [`ColliderAabb`].
146//! - [`shape_intersections`](SpatialQuery::shape_intersections): Finds all entities with a [collider](Collider)
147//! that is intersecting the given shape.
148//!
149//! See the documentation of the components and methods for more information.
150//!
151//! To specify which colliders should be considered in the query, use a [spatial query filter](`SpatialQueryFilter`).
152
153mod query_filter;
154mod ray_caster;
155#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
156mod shape_caster;
157#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
158mod system_param;
159
160mod diagnostics;
161pub use diagnostics::SpatialQueryDiagnostics;
162
163pub use query_filter::*;
164pub use ray_caster::*;
165#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
166pub use shape_caster::*;
167#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
168pub use system_param::*;
169
170use crate::prelude::*;
171use bevy::{
172 ecs::{intern::Interned, schedule::ScheduleLabel},
173 prelude::*,
174};
175
176/// Handles component-based [spatial queries](spatial_query) like [raycasting](spatial_query#raycasting)
177/// and [shapecasting](spatial_query#shapecasting) with [`RayCaster`] and [`ShapeCaster`].
178pub struct SpatialQueryPlugin {
179 schedule: Interned<dyn ScheduleLabel>,
180}
181
182impl SpatialQueryPlugin {
183 /// Creates a [`SpatialQueryPlugin`] with the schedule that is used for running the [`PhysicsSchedule`].
184 ///
185 /// The default schedule is `FixedPostUpdate`.
186 pub fn new(schedule: impl ScheduleLabel) -> Self {
187 Self {
188 schedule: schedule.intern(),
189 }
190 }
191}
192
193impl Default for SpatialQueryPlugin {
194 fn default() -> Self {
195 Self::new(FixedPostUpdate)
196 }
197}
198
199impl Plugin for SpatialQueryPlugin {
200 fn build(&self, app: &mut App) {
201 app.configure_sets(
202 self.schedule,
203 SpatialQuerySystems.after(TransformSystems::Propagate),
204 );
205
206 app.add_systems(
207 self.schedule,
208 (
209 update_ray_caster_positions,
210 #[cfg(all(
211 feature = "default-collider",
212 any(feature = "parry-f32", feature = "parry-f64")
213 ))]
214 (update_shape_caster_positions, raycast, shapecast).chain(),
215 )
216 .chain()
217 .in_set(SpatialQuerySystems),
218 );
219 }
220
221 fn finish(&self, app: &mut App) {
222 // Register timer diagnostics for spatial queries.
223 app.register_physics_diagnostics::<SpatialQueryDiagnostics>();
224 }
225}
226
227/// Responsible for updating spatial query components like [`RayCaster`] and [`ShapeCaster`].
228///
229/// See [`SpatialQueryPlugin`].
230#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
231pub struct SpatialQuerySystems;
232
233type RayCasterPositionQueryComponents = (
234 &'static mut RayCaster,
235 Option<&'static Position>,
236 Option<&'static Rotation>,
237 Option<&'static ChildOf>,
238 Option<&'static GlobalTransform>,
239);
240
241#[allow(clippy::type_complexity)]
242fn update_ray_caster_positions(
243 mut rays: Query<RayCasterPositionQueryComponents>,
244 parents: Query<
245 (
246 Option<&Position>,
247 Option<&Rotation>,
248 Option<&GlobalTransform>,
249 ),
250 With<Children>,
251 >,
252) {
253 for (mut ray, position, rotation, parent, transform) in &mut rays {
254 let origin = ray.origin;
255 let direction = ray.direction;
256
257 let global_position = position.copied().or(transform.map(Position::from));
258 let global_rotation = rotation.copied().or(transform.map(Rotation::from));
259
260 if let Some(global_position) = global_position {
261 ray.set_global_origin(global_position.0 + rotation.map_or(origin, |rot| rot * origin));
262 } else if parent.is_none() {
263 ray.set_global_origin(origin);
264 }
265
266 if let Some(global_rotation) = global_rotation {
267 let global_direction = global_rotation * ray.direction;
268 ray.set_global_direction(global_direction);
269 } else if parent.is_none() {
270 ray.set_global_direction(direction);
271 }
272
273 if let Some(Ok((parent_position, parent_rotation, parent_transform))) =
274 parent.map(|&ChildOf(parent)| parents.get(parent))
275 {
276 let parent_position = parent_position
277 .copied()
278 .or(parent_transform.map(Position::from));
279 let parent_rotation = parent_rotation
280 .copied()
281 .or(parent_transform.map(Rotation::from));
282
283 // Apply parent transformations
284 if global_position.is_none()
285 && let Some(position) = parent_position
286 {
287 let rotation = global_rotation.unwrap_or(parent_rotation.unwrap_or_default());
288 ray.set_global_origin(position.0 + rotation * origin);
289 }
290 if global_rotation.is_none()
291 && let Some(rotation) = parent_rotation
292 {
293 let global_direction = rotation * ray.direction;
294 ray.set_global_direction(global_direction);
295 }
296 }
297 }
298}
299
300#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
301type ShapeCasterPositionQueryComponents = (
302 &'static mut ShapeCaster,
303 Option<&'static Position>,
304 Option<&'static Rotation>,
305 Option<&'static ChildOf>,
306 Option<&'static GlobalTransform>,
307);
308
309#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
310#[allow(clippy::type_complexity)]
311fn update_shape_caster_positions(
312 mut shape_casters: Query<ShapeCasterPositionQueryComponents>,
313 parents: Query<
314 (
315 Option<&Position>,
316 Option<&Rotation>,
317 Option<&GlobalTransform>,
318 ),
319 With<Children>,
320 >,
321) {
322 for (mut shape_caster, position, rotation, parent, transform) in &mut shape_casters {
323 let origin = shape_caster.origin;
324 let shape_rotation = shape_caster.shape_rotation;
325 let direction = shape_caster.direction;
326
327 let global_position = position.copied().or(transform.map(Position::from));
328 let global_rotation = rotation.copied().or(transform.map(Rotation::from));
329
330 if let Some(global_position) = global_position {
331 shape_caster
332 .set_global_origin(global_position.0 + rotation.map_or(origin, |rot| rot * origin));
333 } else if parent.is_none() {
334 shape_caster.set_global_origin(origin);
335 }
336
337 if let Some(global_rotation) = global_rotation {
338 let global_direction = global_rotation * shape_caster.direction;
339 shape_caster.set_global_direction(global_direction);
340 #[cfg(feature = "2d")]
341 {
342 shape_caster
343 .set_global_shape_rotation(shape_rotation + global_rotation.as_radians());
344 }
345 #[cfg(feature = "3d")]
346 {
347 shape_caster.set_global_shape_rotation(shape_rotation * global_rotation.0);
348 }
349 } else if parent.is_none() {
350 shape_caster.set_global_direction(direction);
351 #[cfg(feature = "2d")]
352 {
353 shape_caster.set_global_shape_rotation(shape_rotation);
354 }
355 #[cfg(feature = "3d")]
356 {
357 shape_caster.set_global_shape_rotation(shape_rotation);
358 }
359 }
360
361 if let Some(Ok((parent_position, parent_rotation, parent_transform))) =
362 parent.map(|&ChildOf(parent)| parents.get(parent))
363 {
364 let parent_position = parent_position
365 .copied()
366 .or(parent_transform.map(Position::from));
367 let parent_rotation = parent_rotation
368 .copied()
369 .or(parent_transform.map(Rotation::from));
370
371 // Apply parent transformations
372 if global_position.is_none()
373 && let Some(position) = parent_position
374 {
375 let rotation = global_rotation.unwrap_or(parent_rotation.unwrap_or_default());
376 shape_caster.set_global_origin(position.0 + rotation * origin);
377 }
378 if global_rotation.is_none()
379 && let Some(rotation) = parent_rotation
380 {
381 let global_direction = rotation * shape_caster.direction;
382 shape_caster.set_global_direction(global_direction);
383 #[cfg(feature = "2d")]
384 {
385 shape_caster.set_global_shape_rotation(shape_rotation + rotation.as_radians());
386 }
387 #[cfg(feature = "3d")]
388 {
389 shape_caster.set_global_shape_rotation(shape_rotation * rotation.0);
390 }
391 }
392 }
393 }
394}
395
396#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
397fn raycast(
398 mut rays: Query<(Entity, &mut RayCaster, &mut RayHits)>,
399 spatial_query: SpatialQuery,
400 mut diagnostics: ResMut<SpatialQueryDiagnostics>,
401) {
402 let start = crate::utils::Instant::now();
403
404 for (entity, mut ray_caster, mut hits) in &mut rays {
405 if ray_caster.enabled {
406 ray_caster.cast(entity, &mut hits, &spatial_query);
407 } else if !hits.is_empty() {
408 hits.clear();
409 }
410 }
411
412 diagnostics.update_ray_casters = start.elapsed();
413}
414
415#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
416fn shapecast(
417 mut shape_casters: Query<(Entity, &mut ShapeCaster, &mut ShapeHits)>,
418 spatial_query: SpatialQuery,
419 mut diagnostics: ResMut<SpatialQueryDiagnostics>,
420) {
421 let start = crate::utils::Instant::now();
422
423 for (entity, mut shape_caster, mut hits) in &mut shape_casters {
424 if shape_caster.enabled {
425 shape_caster.cast(entity, &mut hits, &spatial_query);
426 } else if !hits.is_empty() {
427 hits.clear();
428 }
429 }
430
431 diagnostics.update_shape_casters = start.elapsed();
432}