Skip to content

Commit

Permalink
Rename RayTest to RayCast (#11635)
Browse files Browse the repository at this point in the history
# Objective

- `RayTest` vs `AabbCast` and `CircleCast` is inconsistent

## Solution

- Renaming the other two would only make the name more confusing, so we
rename `RayTest2d/3d` to `RayCast2d/3d`
  • Loading branch information
NiseVoid authored Feb 2, 2024
1 parent 8866c61 commit 0ffc8d8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 72 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_math/src/bounding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub use bounded2d::*;
mod bounded3d;
pub use bounded3d::*;

mod raytest2d;
pub use raytest2d::*;
mod raytest3d;
pub use raytest3d::*;
mod raycast2d;
pub use raycast2d::*;
mod raycast3d;
pub use raycast3d::*;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{primitives::Direction2d, Ray2d, Vec2};

/// A raycast intersection test for 2D bounding volumes
#[derive(Clone, Debug)]
pub struct RayTest2d {
pub struct RayCast2d {
/// The ray for the test
pub ray: Ray2d,
/// The maximum distance for the ray
Expand All @@ -12,13 +12,13 @@ pub struct RayTest2d {
direction_recip: Vec2,
}

impl RayTest2d {
/// Construct a [`RayTest2d`] from an origin, [`Direction2d`], and max distance.
impl RayCast2d {
/// Construct a [`RayCast2d`] from an origin, [`Direction2d`], and max distance.
pub fn new(origin: Vec2, direction: Direction2d, max: f32) -> Self {
Self::from_ray(Ray2d { origin, direction }, max)
}

/// Construct a [`RayTest2d`] from a [`Ray2d`] and max distance.
/// Construct a [`RayCast2d`] from a [`Ray2d`] and max distance.
pub fn from_ray(ray: Ray2d, max: f32) -> Self {
Self {
ray,
Expand Down Expand Up @@ -86,13 +86,13 @@ impl RayTest2d {
}
}

impl IntersectsVolume<Aabb2d> for RayTest2d {
impl IntersectsVolume<Aabb2d> for RayCast2d {
fn intersects(&self, volume: &Aabb2d) -> bool {
self.aabb_intersection_at(volume).is_some()
}
}

impl IntersectsVolume<BoundingCircle> for RayTest2d {
impl IntersectsVolume<BoundingCircle> for RayCast2d {
fn intersects(&self, volume: &BoundingCircle) -> bool {
self.circle_intersection_at(volume).is_some()
}
Expand All @@ -102,7 +102,7 @@ impl IntersectsVolume<BoundingCircle> for RayTest2d {
#[derive(Clone, Debug)]
pub struct AabbCast2d {
/// The ray along which to cast the bounding volume
pub ray: RayTest2d,
pub ray: RayCast2d,
/// The aabb that is being cast
pub aabb: Aabb2d,
}
Expand All @@ -116,7 +116,7 @@ impl AabbCast2d {
/// Construct an [`AabbCast2d`] from an [`Aabb2d`], [`Ray2d`], and max distance.
pub fn from_ray(aabb: Aabb2d, ray: Ray2d, max: f32) -> Self {
Self {
ray: RayTest2d::from_ray(ray, max),
ray: RayCast2d::from_ray(ray, max),
aabb,
}
}
Expand All @@ -139,7 +139,7 @@ impl IntersectsVolume<Aabb2d> for AabbCast2d {
#[derive(Clone, Debug)]
pub struct BoundingCircleCast {
/// The ray along which to cast the bounding volume
pub ray: RayTest2d,
pub ray: RayCast2d,
/// The circle that is being cast
pub circle: BoundingCircle,
}
Expand All @@ -153,7 +153,7 @@ impl BoundingCircleCast {
/// Construct a [`BoundingCircleCast`] from a [`BoundingCircle`], [`Ray2d`], and max distance.
pub fn from_ray(circle: BoundingCircle, ray: Ray2d, max: f32) -> Self {
Self {
ray: RayTest2d::from_ray(ray, max),
ray: RayCast2d::from_ray(ray, max),
circle,
}
}
Expand Down Expand Up @@ -183,37 +183,37 @@ mod tests {
for (test, volume, expected_distance) in &[
(
// Hit the center of a centered bounding circle
RayTest2d::new(Vec2::Y * -5., Direction2d::Y, 90.),
RayCast2d::new(Vec2::Y * -5., Direction2d::Y, 90.),
BoundingCircle::new(Vec2::ZERO, 1.),
4.,
),
(
// Hit the center of a centered bounding circle, but from the other side
RayTest2d::new(Vec2::Y * 5., -Direction2d::Y, 90.),
RayCast2d::new(Vec2::Y * 5., -Direction2d::Y, 90.),
BoundingCircle::new(Vec2::ZERO, 1.),
4.,
),
(
// Hit the center of an offset circle
RayTest2d::new(Vec2::ZERO, Direction2d::Y, 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::Y, 90.),
BoundingCircle::new(Vec2::Y * 3., 2.),
1.,
),
(
// Just barely hit the circle before the max distance
RayTest2d::new(Vec2::X, Direction2d::Y, 1.),
RayCast2d::new(Vec2::X, Direction2d::Y, 1.),
BoundingCircle::new(Vec2::ONE, 0.01),
0.99,
),
(
// Hit a circle off-center
RayTest2d::new(Vec2::X, Direction2d::Y, 90.),
RayCast2d::new(Vec2::X, Direction2d::Y, 90.),
BoundingCircle::new(Vec2::Y * 5., 2.),
3.268,
),
(
// Barely hit a circle on the side
RayTest2d::new(Vec2::X * 0.99999, Direction2d::Y, 90.),
RayCast2d::new(Vec2::X * 0.99999, Direction2d::Y, 90.),
BoundingCircle::new(Vec2::Y * 5., 1.),
4.996,
),
Expand All @@ -231,7 +231,7 @@ mod tests {
actual_distance
);

let inverted_ray = RayTest2d::new(test.ray.origin, -test.ray.direction, test.max);
let inverted_ray = RayCast2d::new(test.ray.origin, -test.ray.direction, test.max);
assert!(!inverted_ray.intersects(volume), "{}", case);
}
}
Expand All @@ -241,17 +241,17 @@ mod tests {
for (test, volume) in &[
(
// The ray doesn't go in the right direction
RayTest2d::new(Vec2::ZERO, Direction2d::X, 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::X, 90.),
BoundingCircle::new(Vec2::Y * 2., 1.),
),
(
// Ray's alignment isn't enough to hit the circle
RayTest2d::new(Vec2::ZERO, Direction2d::from_xy(1., 1.).unwrap(), 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::from_xy(1., 1.).unwrap(), 90.),
BoundingCircle::new(Vec2::Y * 2., 1.),
),
(
// The ray's maximum distance isn't high enough
RayTest2d::new(Vec2::ZERO, Direction2d::Y, 0.5),
RayCast2d::new(Vec2::ZERO, Direction2d::Y, 0.5),
BoundingCircle::new(Vec2::Y * 2., 1.),
),
] {
Expand All @@ -275,7 +275,7 @@ mod tests {
-Direction2d::Y,
] {
for max in &[0., 1., 900.] {
let test = RayTest2d::new(*origin, *direction, *max);
let test = RayCast2d::new(*origin, *direction, *max);

let case = format!(
"Case:\n origin: {:?}\n Direction: {:?}\n Max: {}",
Expand All @@ -295,37 +295,37 @@ mod tests {
for (test, volume, expected_distance) in &[
(
// Hit the center of a centered aabb
RayTest2d::new(Vec2::Y * -5., Direction2d::Y, 90.),
RayCast2d::new(Vec2::Y * -5., Direction2d::Y, 90.),
Aabb2d::new(Vec2::ZERO, Vec2::ONE),
4.,
),
(
// Hit the center of a centered aabb, but from the other side
RayTest2d::new(Vec2::Y * 5., -Direction2d::Y, 90.),
RayCast2d::new(Vec2::Y * 5., -Direction2d::Y, 90.),
Aabb2d::new(Vec2::ZERO, Vec2::ONE),
4.,
),
(
// Hit the center of an offset aabb
RayTest2d::new(Vec2::ZERO, Direction2d::Y, 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::Y, 90.),
Aabb2d::new(Vec2::Y * 3., Vec2::splat(2.)),
1.,
),
(
// Just barely hit the aabb before the max distance
RayTest2d::new(Vec2::X, Direction2d::Y, 1.),
RayCast2d::new(Vec2::X, Direction2d::Y, 1.),
Aabb2d::new(Vec2::ONE, Vec2::splat(0.01)),
0.99,
),
(
// Hit an aabb off-center
RayTest2d::new(Vec2::X, Direction2d::Y, 90.),
RayCast2d::new(Vec2::X, Direction2d::Y, 90.),
Aabb2d::new(Vec2::Y * 5., Vec2::splat(2.)),
3.,
),
(
// Barely hit an aabb on corner
RayTest2d::new(Vec2::X * -0.001, Direction2d::from_xy(1., 1.).unwrap(), 90.),
RayCast2d::new(Vec2::X * -0.001, Direction2d::from_xy(1., 1.).unwrap(), 90.),
Aabb2d::new(Vec2::Y * 2., Vec2::ONE),
1.414,
),
Expand All @@ -343,7 +343,7 @@ mod tests {
actual_distance
);

let inverted_ray = RayTest2d::new(test.ray.origin, -test.ray.direction, test.max);
let inverted_ray = RayCast2d::new(test.ray.origin, -test.ray.direction, test.max);
assert!(!inverted_ray.intersects(volume), "{}", case);
}
}
Expand All @@ -353,17 +353,17 @@ mod tests {
for (test, volume) in &[
(
// The ray doesn't go in the right direction
RayTest2d::new(Vec2::ZERO, Direction2d::X, 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::X, 90.),
Aabb2d::new(Vec2::Y * 2., Vec2::ONE),
),
(
// Ray's alignment isn't enough to hit the aabb
RayTest2d::new(Vec2::ZERO, Direction2d::from_xy(1., 0.99).unwrap(), 90.),
RayCast2d::new(Vec2::ZERO, Direction2d::from_xy(1., 0.99).unwrap(), 90.),
Aabb2d::new(Vec2::Y * 2., Vec2::ONE),
),
(
// The ray's maximum distance isn't high enough
RayTest2d::new(Vec2::ZERO, Direction2d::Y, 0.5),
RayCast2d::new(Vec2::ZERO, Direction2d::Y, 0.5),
Aabb2d::new(Vec2::Y * 2., Vec2::ONE),
),
] {
Expand All @@ -387,7 +387,7 @@ mod tests {
-Direction2d::Y,
] {
for max in &[0., 1., 900.] {
let test = RayTest2d::new(*origin, *direction, *max);
let test = RayCast2d::new(*origin, *direction, *max);

let case = format!(
"Case:\n origin: {:?}\n Direction: {:?}\n Max: {}",
Expand Down Expand Up @@ -464,7 +464,7 @@ mod tests {
);

let inverted_ray =
RayTest2d::new(test.ray.ray.origin, -test.ray.ray.direction, test.ray.max);
RayCast2d::new(test.ray.ray.origin, -test.ray.ray.direction, test.ray.max);
assert!(!inverted_ray.intersects(volume), "{}", case);
}
}
Expand Down Expand Up @@ -531,7 +531,7 @@ mod tests {
);

let inverted_ray =
RayTest2d::new(test.ray.ray.origin, -test.ray.ray.direction, test.ray.max);
RayCast2d::new(test.ray.ray.origin, -test.ray.ray.direction, test.ray.max);
assert!(!inverted_ray.intersects(volume), "{}", case);
}
}
Expand Down
Loading

0 comments on commit 0ffc8d8

Please sign in to comment.