diff --git a/src/spatial_query/query_filter.rs b/src/spatial_query/query_filter.rs index 3f9f0290..e6fad055 100644 --- a/src/spatial_query/query_filter.rs +++ b/src/spatial_query/query_filter.rs @@ -1,4 +1,7 @@ -use bevy::{prelude::*, utils::EntityHashSet}; +use bevy::{ + prelude::*, + utils::{EntityHash, EntityHashSet}, +}; use crate::prelude::*; @@ -41,14 +44,17 @@ pub struct SpatialQueryFilter { impl Default for SpatialQueryFilter { fn default() -> Self { - Self { - mask: LayerMask::ALL, - excluded_entities: default(), - } + Self::DEFAULT } } impl SpatialQueryFilter { + /// The default [`SpatialQueryFilter`] configuration that includes all collision layers and has no excluded entities. + pub const DEFAULT: Self = Self { + mask: LayerMask::ALL, + excluded_entities: EntityHashSet::with_hasher(EntityHash), + }; + /// Creates a new [`SpatialQueryFilter`] with the given [`LayerMask`] determining /// which [collision layers](CollisionLayers) will be included in the [spatial query](crate::spatial_query). pub fn from_mask(mask: impl Into) -> Self { diff --git a/src/spatial_query/ray_caster.rs b/src/spatial_query/ray_caster.rs index c1d7baa4..1c748b6b 100644 --- a/src/spatial_query/ray_caster.rs +++ b/src/spatial_query/ray_caster.rs @@ -395,36 +395,44 @@ impl Default for RayCastConfig { } impl RayCastConfig { + /// The default [`RayCastConfig`] configuration. + pub const DEFAULT: Self = Self { + max_distance: Scalar::MAX, + solid: true, + filter: SpatialQueryFilter::DEFAULT, + }; + /// Creates a new [`RayCastConfig`] with a given maximum distance the ray can travel. #[inline] - pub fn from_max_distance(max_distance: Scalar) -> Self { + pub const fn from_max_distance(max_distance: Scalar) -> Self { Self { max_distance, - ..default() + solid: true, + filter: SpatialQueryFilter::DEFAULT, } } /// Creates a new [`RayCastConfig`] with a given [`SpatialQueryFilter`]. #[inline] - pub fn from_filter(filter: SpatialQueryFilter) -> Self { + pub const fn from_filter(filter: SpatialQueryFilter) -> Self { Self { + max_distance: Scalar::MAX, + solid: true, filter, - ..default() } } /// Sets the maximum distance the ray can travel. #[inline] - pub fn with_max_distance(mut self, max_distance: Scalar) -> Self { + pub const fn with_max_distance(mut self, max_distance: Scalar) -> Self { self.max_distance = max_distance; self } /// Sets the [`SpatialQueryFilter`] for the ray cast. #[inline] - pub fn with_filter(mut self, filter: SpatialQueryFilter) -> Self { - self.filter = filter; - self + pub const fn with_filter(&self, filter: SpatialQueryFilter) -> Self { + Self { filter, ..*self } } } diff --git a/src/spatial_query/shape_caster.rs b/src/spatial_query/shape_caster.rs index 8ac83201..1d202f95 100644 --- a/src/spatial_query/shape_caster.rs +++ b/src/spatial_query/shape_caster.rs @@ -463,64 +463,75 @@ pub struct ShapeCastConfig { impl Default for ShapeCastConfig { fn default() -> Self { - Self { - max_distance: Scalar::MAX, - target_distance: 0.0, - compute_impact_on_penetration: true, - ignore_origin_penetration: false, - filter: SpatialQueryFilter::default(), - } + Self::DEFAULT } } impl ShapeCastConfig { + /// The default [`ShapeCastConfig`] configuration. + pub const DEFAULT: Self = Self { + max_distance: Scalar::MAX, + target_distance: 0.0, + compute_impact_on_penetration: true, + ignore_origin_penetration: false, + filter: SpatialQueryFilter::DEFAULT, + }; + /// Creates a new [`ShapeCastConfig`] with a given maximum distance the shape can travel. #[inline] - pub fn from_max_distance(max_distance: Scalar) -> Self { + pub const fn from_max_distance(max_distance: Scalar) -> Self { Self { max_distance, - ..default() + target_distance: 0.0, + compute_impact_on_penetration: true, + ignore_origin_penetration: false, + filter: SpatialQueryFilter::DEFAULT, } } /// Creates a new [`ShapeCastConfig`] with a given separation distance at which /// the shapes will be considered as impacting. #[inline] - pub fn from_target_distance(target_distance: Scalar) -> Self { + pub const fn from_target_distance(target_distance: Scalar) -> Self { Self { + max_distance: Scalar::MAX, target_distance, - ..default() + compute_impact_on_penetration: true, + ignore_origin_penetration: false, + filter: SpatialQueryFilter::DEFAULT, } } /// Creates a new [`ShapeCastConfig`] with a given [`SpatialQueryFilter`]. #[inline] - pub fn from_filter(filter: SpatialQueryFilter) -> Self { + pub const fn from_filter(filter: SpatialQueryFilter) -> Self { Self { + max_distance: Scalar::MAX, + target_distance: 0.0, + compute_impact_on_penetration: true, + ignore_origin_penetration: false, filter, - ..default() } } /// Sets the maximum distance the shape can travel. #[inline] - pub fn with_max_distance(mut self, max_distance: Scalar) -> Self { + pub const fn with_max_distance(mut self, max_distance: Scalar) -> Self { self.max_distance = max_distance; self } /// Sets the separation distance at which the shapes will be considered as impacting. #[inline] - pub fn with_target_distance(mut self, target_distance: Scalar) -> Self { + pub const fn with_target_distance(mut self, target_distance: Scalar) -> Self { self.target_distance = target_distance; self } /// Sets the [`SpatialQueryFilter`] for the shape cast. #[inline] - pub fn with_filter(mut self, filter: SpatialQueryFilter) -> Self { - self.filter = filter; - self + pub const fn with_filter(&self, filter: SpatialQueryFilter) -> Self { + Self { filter, ..*self } } }