avian2d/spatial_query/
ray_caster.rs

1use crate::prelude::*;
2use bevy::{
3    ecs::{
4        entity::{EntityMapper, MapEntities},
5        lifecycle::HookContext,
6        world::DeferredWorld,
7    },
8    prelude::*,
9};
10#[cfg(all(
11    feature = "default-collider",
12    any(feature = "parry-f32", feature = "parry-f64")
13))]
14use parry::{partitioning::BvhNode, query::RayCast};
15
16/// A component used for [raycasting](spatial_query#raycasting).
17///
18/// **Raycasting** is a type of [spatial query](spatial_query) that finds one or more hits
19/// between a ray and a set of colliders.
20///
21/// Each ray is defined by a local `origin` and a `direction`. The [`RayCaster`] will find each hit
22/// and add them to the [`RayHits`] component. Each hit has a `distance` property which refers to
23/// how far the ray travelled, along with a `normal` for the point of intersection.
24///
25/// The [`RayCaster`] is the easiest way to handle simple raycasts. If you want more control and don't want to
26/// perform raycasts every frame, consider using the [`SpatialQuery`] system parameter.
27///
28/// # Hit Count and Order
29///
30/// The results of a raycast are in an arbitrary order by default. You can iterate over them in the order of
31/// distance with the [`RayHits::iter_sorted`] method.
32///
33/// You can configure the maximum amount of hits for a ray using `max_hits`. By default this is unbounded,
34/// so you will get all hits. When the number or complexity of colliders is large, this can be very
35/// expensive computationally. Set the value to whatever works best for your case.
36///
37/// Note that when there are more hits than `max_hits`, **some hits will be missed**.
38/// To guarantee that the closest hit is included, you should set `max_hits` to one or a value that
39/// is enough to contain all hits.
40///
41/// # Example
42///
43/// ```
44/// # #[cfg(feature = "2d")]
45/// # use avian2d::prelude::*;
46/// # #[cfg(feature = "3d")]
47/// use avian3d::prelude::*;
48/// use bevy::prelude::*;
49///
50/// # #[cfg(all(feature = "3d", feature = "f32"))]
51/// fn setup(mut commands: Commands) {
52///     // Spawn a ray at the center going right
53///     commands.spawn(RayCaster::new(Vec3::ZERO, Dir3::X));
54///     // ...spawn colliders and other things
55/// }
56///
57/// # #[cfg(all(feature = "3d", feature = "f32"))]
58/// fn print_hits(query: Query<(&RayCaster, &RayHits)>) {
59///     for (ray, hits) in &query {
60///         // For the faster iterator that isn't sorted, use `.iter()`
61///         for hit in hits.iter_sorted() {
62///             println!(
63///                 "Hit entity {} at {} with normal {}",
64///                 hit.entity,
65///                 ray.origin + *ray.direction * hit.distance,
66///                 hit.normal,
67///             );
68///         }
69///     }
70/// }
71/// ```
72#[derive(Component, Clone, Debug, PartialEq, Reflect)]
73#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
74#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
75#[reflect(Debug, Component, PartialEq)]
76#[component(on_add = on_add_ray_caster)]
77#[require(RayHits)]
78pub struct RayCaster {
79    /// Controls if the ray caster is enabled.
80    pub enabled: bool,
81
82    /// The local origin of the ray relative to the [`Position`] and [`Rotation`] of the ray entity or its parent.
83    ///
84    /// To get the global origin, use the `global_origin` method.
85    pub origin: Vector,
86
87    /// The global origin of the ray.
88    global_origin: Vector,
89
90    /// The local direction of the ray relative to the [`Rotation`] of the ray entity or its parent.
91    ///
92    /// To get the global direction, use the `global_direction` method.
93    pub direction: Dir,
94
95    /// The global direction of the ray.
96    global_direction: Dir,
97
98    /// The maximum number of hits allowed.
99    ///
100    /// When there are more hits than `max_hits`, **some hits will be missed**.
101    /// To guarantee that the closest hit is included, you should set `max_hits` to one or a value that
102    /// is enough to contain all hits.
103    pub max_hits: u32,
104
105    /// The maximum distance the ray can travel.
106    ///
107    /// By default this is infinite, so the ray will travel until all hits up to `max_hits` have been checked.
108    #[doc(alias = "max_time_of_impact")]
109    pub max_distance: Scalar,
110
111    /// Controls how the ray behaves when the ray origin is inside of a [collider](Collider).
112    ///
113    /// If `true`, shapes will be treated as solid, and the ray cast will return with a distance of `0.0`
114    /// if the ray origin is inside of the shape. Otherwise, shapes will be treated as hollow, and the ray
115    /// will always return a hit at the shape's boundary.
116    pub solid: bool,
117
118    /// If true, the ray caster ignores hits against its own [`Collider`]. This is the default.
119    pub ignore_self: bool,
120
121    /// Rules that determine which colliders are taken into account in the ray cast.
122    pub query_filter: SpatialQueryFilter,
123}
124
125impl Default for RayCaster {
126    fn default() -> Self {
127        Self {
128            enabled: true,
129            origin: Vector::ZERO,
130            global_origin: Vector::ZERO,
131            direction: Dir::X,
132            global_direction: Dir::X,
133            max_distance: Scalar::MAX,
134            max_hits: u32::MAX,
135            solid: true,
136            ignore_self: true,
137            query_filter: SpatialQueryFilter::default(),
138        }
139    }
140}
141
142impl From<Ray> for RayCaster {
143    fn from(ray: Ray) -> Self {
144        RayCaster::from_ray(ray)
145    }
146}
147
148impl RayCaster {
149    /// Creates a new [`RayCaster`] with a given origin and direction.
150    pub fn new(origin: Vector, direction: Dir) -> Self {
151        Self {
152            origin,
153            direction,
154            ..default()
155        }
156    }
157
158    /// Creates a new [`RayCaster`] from a ray.
159    pub fn from_ray(ray: Ray) -> Self {
160        Self {
161            origin: ray.origin.adjust_precision(),
162            direction: ray.direction,
163            ..default()
164        }
165    }
166
167    /// Sets the ray origin.
168    pub fn with_origin(mut self, origin: Vector) -> Self {
169        self.origin = origin;
170        self
171    }
172
173    /// Sets the ray direction.
174    pub fn with_direction(mut self, direction: Dir) -> Self {
175        self.direction = direction;
176        self
177    }
178
179    /// Controls how the ray behaves when the ray origin is inside of a [collider](Collider).
180    ///
181    /// If `true`, shapes will be treated as solid, and the ray cast will return with a distance of `0.0`
182    /// if the ray origin is inside of the shape. Otherwise, shapes will be treated as hollow, and the ray
183    /// will always return a hit at the shape's boundary.
184    pub fn with_solidness(mut self, solid: bool) -> Self {
185        self.solid = solid;
186        self
187    }
188
189    /// Sets if the ray caster should ignore hits against its own [`Collider`].
190    ///
191    /// The default is `true`.
192    pub fn with_ignore_self(mut self, ignore: bool) -> Self {
193        self.ignore_self = ignore;
194        self
195    }
196
197    /// Sets the maximum distance the ray can travel.
198    pub fn with_max_distance(mut self, max_distance: Scalar) -> Self {
199        self.max_distance = max_distance;
200        self
201    }
202
203    /// Sets the maximum number of allowed hits.
204    pub fn with_max_hits(mut self, max_hits: u32) -> Self {
205        self.max_hits = max_hits;
206        self
207    }
208
209    /// Sets the ray caster's [query filter](SpatialQueryFilter) that controls which colliders
210    /// should be included or excluded by raycasts.
211    pub fn with_query_filter(mut self, query_filter: SpatialQueryFilter) -> Self {
212        self.query_filter = query_filter;
213        self
214    }
215
216    /// Enables the [`RayCaster`].
217    pub fn enable(&mut self) {
218        self.enabled = true;
219    }
220
221    /// Disables the [`RayCaster`].
222    pub fn disable(&mut self) {
223        self.enabled = false;
224    }
225
226    /// Returns the global origin of the ray.
227    pub fn global_origin(&self) -> Vector {
228        self.global_origin
229    }
230
231    /// Returns the global direction of the ray.
232    pub fn global_direction(&self) -> Dir {
233        self.global_direction
234    }
235
236    /// Sets the global origin of the ray.
237    pub(crate) fn set_global_origin(&mut self, global_origin: Vector) {
238        self.global_origin = global_origin;
239    }
240
241    /// Sets the global direction of the ray.
242    pub(crate) fn set_global_direction(&mut self, global_direction: Dir) {
243        self.global_direction = global_direction;
244    }
245
246    #[cfg(all(
247        feature = "default-collider",
248        any(feature = "parry-f32", feature = "parry-f64")
249    ))]
250    pub(crate) fn cast(
251        &mut self,
252        caster_entity: Entity,
253        hits: &mut RayHits,
254        query_pipeline: &SpatialQueryPipeline,
255    ) {
256        if self.ignore_self {
257            self.query_filter.excluded_entities.insert(caster_entity);
258        } else {
259            self.query_filter.excluded_entities.remove(&caster_entity);
260        }
261
262        hits.clear();
263
264        if self.max_hits == 1 {
265            query_pipeline.cast_ray(
266                self.global_origin(),
267                self.global_direction(),
268                self.max_distance,
269                self.solid,
270                &self.query_filter,
271            );
272        } else {
273            let ray = parry::query::Ray::new(
274                self.global_origin().into(),
275                self.global_direction().adjust_precision().into(),
276            );
277
278            let found_hits = query_pipeline
279                .bvh
280                .leaves(|node: &BvhNode| node.aabb().intersects_local_ray(&ray, self.max_distance))
281                .filter_map(|leaf| {
282                    let proxy = query_pipeline.proxies.get(leaf as usize)?;
283
284                    if !self.query_filter.test(proxy.entity, proxy.layers) {
285                        return None;
286                    }
287
288                    let hit = proxy.collider.shape_scaled().cast_ray_and_get_normal(
289                        &proxy.isometry,
290                        &ray,
291                        self.max_distance,
292                        self.solid,
293                    )?;
294
295                    Some(RayHitData {
296                        entity: proxy.entity,
297                        distance: hit.time_of_impact,
298                        normal: hit.normal.into(),
299                    })
300                })
301                .take(self.max_hits as usize);
302
303            hits.extend(found_hits);
304        }
305    }
306}
307
308fn on_add_ray_caster(mut world: DeferredWorld, ctx: HookContext) {
309    let ray_caster = world.get::<RayCaster>(ctx.entity).unwrap();
310    let max_hits = if ray_caster.max_hits == u32::MAX {
311        10
312    } else {
313        ray_caster.max_hits as usize
314    };
315
316    // Initialize capacity for hits
317    world.get_mut::<RayHits>(ctx.entity).unwrap().0 = Vec::with_capacity(max_hits);
318}
319
320/// Contains the hits of a ray cast by a [`RayCaster`].
321///
322/// The maximum number of hits depends on the value of `max_hits` in [`RayCaster`].
323///
324/// # Order
325///
326/// By default, the order of the hits is not guaranteed.
327///
328/// You can iterate the hits in the order of distance with `iter_sorted`.
329/// Note that this will create and sort a new vector instead of iterating over the existing one.
330///
331/// **Note**: When there are more hits than `max_hits`, **some hits will be missed**.
332/// If you want to guarantee that the closest hit is included, set `max_hits` to one.
333///
334/// # Example
335///
336/// ```
337#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
338#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
339/// use bevy::prelude::*;
340///
341/// fn print_hits(query: Query<&RayHits, With<RayCaster>>) {
342///     for hits in &query {
343///         // For the faster iterator that isn't sorted, use `.iter()`.
344///         for hit in hits.iter_sorted() {
345///             println!("Hit entity {} with distance {}", hit.entity, hit.distance);
346///         }
347///     }
348/// }
349/// ```
350#[derive(Component, Clone, Debug, Default, Deref, DerefMut, PartialEq, Reflect)]
351#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
352#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
353#[reflect(Component, Debug, Default, PartialEq)]
354pub struct RayHits(pub Vec<RayHitData>);
355
356impl RayHits {
357    /// Returns an iterator over the hits, sorted in ascending order according to the distance.
358    ///
359    /// Note that this allocates a new vector. If you don't need the hits in order, use `iter`.
360    pub fn iter_sorted(&self) -> alloc::vec::IntoIter<RayHitData> {
361        let mut vector = self.as_slice().to_vec();
362        vector.sort_by(|a, b| a.distance.partial_cmp(&b.distance).unwrap());
363        vector.into_iter()
364    }
365}
366
367impl IntoIterator for RayHits {
368    type Item = RayHitData;
369    type IntoIter = alloc::vec::IntoIter<RayHitData>;
370
371    fn into_iter(self) -> Self::IntoIter {
372        self.0.into_iter()
373    }
374}
375
376impl<'a> IntoIterator for &'a RayHits {
377    type Item = &'a RayHitData;
378    type IntoIter = core::slice::Iter<'a, RayHitData>;
379
380    fn into_iter(self) -> Self::IntoIter {
381        self.0.iter()
382    }
383}
384
385impl<'a> IntoIterator for &'a mut RayHits {
386    type Item = &'a mut RayHitData;
387    type IntoIter = core::slice::IterMut<'a, RayHitData>;
388
389    fn into_iter(self) -> Self::IntoIter {
390        self.0.iter_mut()
391    }
392}
393
394impl MapEntities for RayHits {
395    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
396        for hit in self {
397            hit.map_entities(entity_mapper);
398        }
399    }
400}
401
402/// Data related to a hit during a [raycast](spatial_query#raycasting).
403#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
404#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
405#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
406#[reflect(Debug, PartialEq)]
407pub struct RayHitData {
408    /// The entity of the collider that was hit by the ray.
409    pub entity: Entity,
410
411    /// How far the ray travelled. This is the distance between the ray origin and the point of intersection.
412    pub distance: Scalar,
413
414    /// The normal at the point of intersection, expressed in world space.
415    pub normal: Vector,
416}
417
418impl MapEntities for RayHitData {
419    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
420        self.entity = entity_mapper.get_mapped(self.entity);
421    }
422}