phys-collision 2.0.1-beta.0

Provides collision detection ability
// Copyright (C) 2020-2025 phys-collision 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 glam_det::{UnitQuat, Vec3};

use super::traits::*;
use crate::shapes::ConvexHull;
use crate::{Shape, ShapeContainer, ShapeRef};

macro_rules! impl_overlap_tests {
    ($(($query_name:ident, $hit_name:ident)),*) => {
        #[allow(unreachable_patterns)]
        #[inline]
        fn overlap_impl(
            query_shape: &ShapeRef,
            hit_shape: &ShapeRef,
            offset_b: Vec3,
            orientation_a: UnitQuat,
            orientation_b: UnitQuat,
        ) -> bool {
            match (&query_shape.value, &hit_shape.value) {

                (Shape::ConvexHull(_), _) => {
                    todo!("issue #1201")
                }
                (query_shape_enum, Shape::ConvexHull(hit_convex_hull_id)) => {
                    match query_shape_enum {
                        Shape::Cuboid(query_shape) => {
                            let convex_hull = hit_shape.container.get::<ConvexHull>(*hit_convex_hull_id)
                                .expect("fail to map ConvexHullId to ConvexHull");
                            query_shape.overlap_test(convex_hull, offset_b, orientation_a, orientation_b,None)
                        }
                        Shape::Sphere(query_shape) => {
                            let convex_hull = hit_shape.container.get::<ConvexHull>(*hit_convex_hull_id)
                                .expect("fail to map ConvexHullId to ConvexHull");
                            query_shape.overlap_test(convex_hull, offset_b, orientation_a, orientation_b,None)
                        }
                        Shape::Capsule(query_shape) => {
                            let convex_hull = hit_shape.container.get::<ConvexHull>(*hit_convex_hull_id)
                                .expect("fail to map ConvexHullId to ConvexHull");
                            query_shape.overlap_test(convex_hull, offset_b, orientation_a, orientation_b,None)
                        }
                        _ => todo!("other shape overlap convex, issue #1201")
                    }
                }
                $(
                    (Shape::$query_name(query_shape), Shape::$hit_name(hit_shape)) => {
                        query_shape.overlap_test(hit_shape, offset_b, orientation_a,orientation_b,None)
                    }
                )*
                _=>false
            }
        }
    };
}

impl_overlap_tests!(
    (Sphere, Sphere),
    (Sphere, Capsule),
    (Sphere, Cuboid),
    (Sphere, Cylinder),
    (Sphere, InfinitePlane),
    (Cuboid, Cuboid),
    (Cuboid, Capsule),
    (Cuboid, Sphere),
    (Cuboid, Cylinder),
    (Cuboid, InfinitePlane),
    (Capsule, Cuboid),
    (Capsule, Capsule),
    (Capsule, Sphere),
    (Capsule, Cylinder),
    (Capsule, InfinitePlane)
);

#[must_use]
pub fn overlap(
    query_shape: &ShapeRef,
    hit_shape: &ShapeRef,
    offset_b: Vec3,
    orientation_a: UnitQuat,
    orientation_b: UnitQuat,
    _: Option<&ShapeContainer>,
) -> bool {
    overlap_impl(
        query_shape,
        hit_shape,
        offset_b,
        orientation_a,
        orientation_b,
    )
}