pub struct RayCaster {
pub enabled: bool,
pub origin: Vector,
pub direction: Dir3,
pub max_time_of_impact: Scalar,
pub max_hits: u32,
pub solid: bool,
pub ignore_self: bool,
pub query_filter: SpatialQueryFilter,
/* private fields */
}Expand description
A component used for raycasting.
Raycasting is a type of spatial query that finds one or more hits between a ray and a set of colliders.
Each ray is defined by a local origin and a direction. The RayCaster will find each hit
and add them to the RayHits component. Each hit has a time_of_impact property
which refers to how long the ray travelled, i.e. the distance between the origin and the point of intersection.
The RayCaster is the easiest way to handle simple raycasts. If you want more control and don’t want to
perform raycasts every frame, consider using the SpatialQuery system parameter.
§Hit count and order
The results of a raycast are in an arbitrary order by default. You can iterate over them in the order of
time of impact with the RayHits::iter_sorted method.
You can configure the maximum amount of hits for a ray using max_hits. By default this is unbounded,
so you will get all hits. When the number or complexity of colliders is large, this can be very
expensive computationally. Set the value to whatever works best for your case.
Note that when there are more hits than max_hits, some hits will be missed.
To guarantee that the closest hit is included, you should set max_hits to one or a value that
is enough to contain all hits.
§Example
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
fn setup(mut commands: Commands) {
// Spawn a ray at the center going right
commands.spawn(RayCaster::new(Vec3::ZERO, Dir3::X));
// ...spawn colliders and other things
}
fn print_hits(query: Query<(&RayCaster, &RayHits)>) {
for (ray, hits) in &query {
// For the faster iterator that isn't sorted, use `.iter()`
for hit in hits.iter_sorted() {
println!(
"Hit entity {:?} at {} with normal {}",
hit.entity,
ray.origin + *ray.direction * hit.time_of_impact,
hit.normal,
);
}
}
}Fields§
§enabled: boolControls if the ray caster is enabled.
origin: VectorThe local origin of the ray relative to the Position and Rotation of the ray entity or its parent.
To get the global origin, use the global_origin method.
direction: Dir3The local direction of the ray relative to the Rotation of the ray entity or its parent.
To get the global direction, use the global_direction method.
max_time_of_impact: ScalarThe maximum distance the ray can travel. By default this is infinite, so the ray will travel
until all hits up to max_hits have been checked.
max_hits: u32The maximum number of hits allowed.
When there are more hits than max_hits, some hits will be missed.
To guarantee that the closest hit is included, you should set max_hits to one or a value that
is enough to contain all hits.
solid: boolControls how the ray behaves when the ray origin is inside of a collider.
If solid is true, the point of intersection will be the ray origin itself.
If solid is false, the collider will be considered to have no interior, and the point of intersection
will be at the collider shape’s boundary.
ignore_self: boolIf true, the ray caster ignores hits against its own Collider. This is the default.
query_filter: SpatialQueryFilterRules that determine which colliders are taken into account in the query.
Implementations§
Source§impl RayCaster
impl RayCaster
Sourcepub fn new(origin: Vector, direction: Dir3) -> Self
pub fn new(origin: Vector, direction: Dir3) -> Self
Creates a new RayCaster with a given origin and direction.
Sourcepub fn with_origin(self, origin: Vector) -> Self
pub fn with_origin(self, origin: Vector) -> Self
Sets the ray origin.
Sourcepub fn with_direction(self, direction: Dir3) -> Self
pub fn with_direction(self, direction: Dir3) -> Self
Sets the ray direction.
Sourcepub fn with_solidness(self, solid: bool) -> Self
pub fn with_solidness(self, solid: bool) -> Self
Sets if the ray treats colliders as solid.
If solid is true, the point of intersection will be the ray origin itself.
If solid is false, the collider will be considered to have no interior, and the point of intersection
will be at the collider shape’s boundary.
Sourcepub fn with_ignore_self(self, ignore: bool) -> Self
pub fn with_ignore_self(self, ignore: bool) -> Self
Sets if the ray caster should ignore hits against its own Collider.
The default is true.
Sourcepub fn with_max_time_of_impact(self, max_time_of_impact: Scalar) -> Self
pub fn with_max_time_of_impact(self, max_time_of_impact: Scalar) -> Self
Sets the maximum time of impact, i.e. the maximum distance that the ray is allowed to travel.
Sourcepub fn with_max_hits(self, max_hits: u32) -> Self
pub fn with_max_hits(self, max_hits: u32) -> Self
Sets the maximum number of allowed hits.
Sourcepub fn with_query_filter(self, query_filter: SpatialQueryFilter) -> Self
pub fn with_query_filter(self, query_filter: SpatialQueryFilter) -> Self
Sets the ray caster’s query filter that controls which colliders should be included or excluded by raycasts.
Sourcepub fn global_origin(&self) -> Vector
pub fn global_origin(&self) -> Vector
Returns the global origin of the ray.
Sourcepub fn global_direction(&self) -> Dir3
pub fn global_direction(&self) -> Dir3
Returns the global direction of the ray.
Trait Implementations§
Source§impl Component for RayCaster
impl Component for RayCaster
Source§const STORAGE_TYPE: StorageType = bevy::ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy::ecs::component::StorageType::Table
Source§fn register_component_hooks(_hooks: &mut ComponentHooks)
fn register_component_hooks(_hooks: &mut ComponentHooks)
ComponentHooks.Auto Trait Implementations§
impl Freeze for RayCaster
impl RefUnwindSafe for RayCaster
impl Send for RayCaster
impl Sync for RayCaster
impl Unpin for RayCaster
impl UnsafeUnpin for RayCaster
impl UnwindSafe for RayCaster
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
Source§fn get_component_ids(
components: &Components,
ids: &mut impl FnMut(Option<ComponentId>),
)
fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ConditionalSend for Twhere
T: Send,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self using data from the given World.Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Settings for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.