phys-raycast 2.0.0

Ray casting functionality for 3D physics shapes
Documentation
// Copyright (C) 2020-2025 phys-raycast authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use phys_geom::math::{Real, *};
use phys_geom::shape::Cuboid;
use phys_geom::ComputeAabb3;

use crate::{Raycast, RaycastHitResult};

impl Raycast for Cuboid {
    fn raycast(
        &self,
        local_ray: phys_geom::Ray,
        max_distance: Real,
        discard_inside_hit: bool,
    ) -> Option<RaycastHitResult> {
        let one_over_direction = local_ray.one_over_direction();
        let aabb = self.compute_aabb();

        if let Some((entry, max_entry)) = aabb.raycast_by_one_over_direction_impl(
            local_ray.origin,
            one_over_direction,
            max_distance,
        ) {
            if max_entry <= 0.0 {
                if discard_inside_hit {
                    return None;
                }
                return Some(RaycastHitResult {
                    distance: 0.0,
                    normal: -local_ray.direction,
                });
            }

            let mut normal: UnitVec3;
            let inverse;

            #[allow(clippy::float_cmp)]
            // Logic of raycast_by_one_over_direction_impl ensured we can use == here.
            if entry.x == max_entry {
                normal = Vec3::x_axis();
                inverse = local_ray.direction.x > 0.0;
            } else if entry.y == max_entry {
                normal = Vec3::y_axis();
                inverse = local_ray.direction.y > 0.0;
            } else {
                normal = Vec3::z_axis();
                inverse = local_ray.direction.z > 0.0;
            }

            if inverse {
                normal = -normal;
            }

            Some(RaycastHitResult {
                distance: max_entry,
                normal,
            })
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use phys_geom::math::*;
    use phys_geom::Ray;

    use super::*;
    #[test]
    fn test_raycast() {
        let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
        let ray_z = Ray::new_with_vec3(Point3::new(0.0, 0.0, 2.0), Vec3::new(0.0, 0.0, -1.0));
        assert_eq!(
            cuboid.raycast(ray_z, 5.0, false),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: UnitVec3::new_normalize(Vec3::new(0.0, 0.0, 1.0)),
            })
        );

        assert_eq!(cuboid.raycast(ray_z, 0.5, false), None);

        assert_eq!(
            cuboid.raycast(
                Ray::new_with_vec3(Point3::new(2.0, 0.0, 0.0), Vec3::new(-1.0, 0.0, 0.0)),
                5.0,
                false
            ),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: UnitVec3::new_normalize(Vec3::new(1.0, 0.0, 0.0)),
            })
        );

        assert_eq!(
            cuboid.raycast(
                Ray::new_with_vec3(Point3::new(0.0, 2.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
                5.0,
                false
            ),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: Vec3::y_axis(),
            })
        );

        assert_eq!(
            cuboid.raycast(
                Ray::new_with_vec3(Point3::new(-2.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0)),
                5.0,
                false
            ),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: -Vec3::x_axis(),
            })
        );

        assert_eq!(
            cuboid.raycast(
                Ray::new_with_vec3(Point3::new(0.0, -2.0, 0.0), Vec3::new(0.0, 1.0, 0.0)),
                5.0,
                false
            ),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: -Vec3::y_axis(),
            })
        );

        assert_eq!(
            cuboid.raycast(
                Ray::new_with_vec3(Point3::new(0.0, 0.0, -2.0), Vec3::new(0.0, 0.0, 1.0)),
                5.0,
                false
            ),
            Some(RaycastHitResult {
                distance: 1.0,
                normal: -Vec3::z_axis(),
            })
        );

        {
            let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 2.0), Vec3::new(3.0, 0.0, -2.0));
            assert_eq!(cuboid.raycast(ray, 10.0, false), None);
        }
    }

    #[test]
    fn test_raycast_surface() {
        let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
        let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0));
        assert_eq!(cuboid.raycast(ray, 10.0, true), None);
        assert_eq!(
            cuboid.raycast(ray, 10.0, false),
            Some(RaycastHitResult {
                distance: 0.0,
                normal: -ray.direction,
            })
        );
    }

    // The origin of the ray is inside the cube.
    #[test]
    fn test_raycast_inner() {
        let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
        let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, -1.0));
        assert_eq!(
            cuboid.raycast(ray, 5.0, false),
            Some(RaycastHitResult {
                distance: 0.0,
                normal: -ray.direction,
            })
        );

        assert_eq!(cuboid.raycast(ray, 5.0, true), None);
    }
}