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::nums::*;
use glam_det::{UnitQuat, UnitQuatx4, UnitVec3x4, Vec3, Vec3x4};

use crate::collision_tasks::{ShapeTester, ShapeWideTester};
use crate::convex_contact_manifold::{Convex4ContactManifoldWide, ConvexContactManifold};
use crate::shapes::{Capsule, CapsuleWide, InfinitePlane, InfinitePlaneWide};
use crate::traits::{ContactContext, ContactManifoldWide, CreateShapeWide, PairTest, PairWideTest};
use crate::ShapeContainer;
impl PairWideTest<CapsuleWide, InfinitePlaneWide> for ShapeWideTester {
    #[inline]
    fn should_reset_manifold_before_test() -> bool {
        false
    }

    // 2 manifold in Convex4ContactManifoldWide
    #[inline]
    fn test(
        a: &CapsuleWide,
        _b: &InfinitePlaneWide,
        contact_context: &ContactContext,
        manifold: &mut Convex4ContactManifoldWide,
    ) {
        // convert capsule's axis and hemisphere centers into infinite plane space
        let y_axis = contact_context.orientation_a * UnitVec3x4::Y;
        let capsule_in_b = contact_context.orientation_b.inverse() * (-*contact_context.offset_b);
        let capsule_axis_in_b = contact_context.orientation_b.inverse() * y_axis;
        let capsule_top_center_in_b = capsule_in_b + capsule_axis_in_b * a.half_height;
        let capsule_bottom_center_in_b = capsule_in_b + capsule_axis_in_b * (-a.half_height);

        manifold.depth[0] = a.radius - capsule_top_center_in_b.y;
        manifold.depth[1] = a.radius - capsule_bottom_center_in_b.y;

        manifold.contact_exists[0] = manifold.depth[0].gt(-contact_context.speculative_margin);
        manifold.contact_exists[1] = manifold.depth[1].gt(-contact_context.speculative_margin);

        // contact normal
        manifold.normal = contact_context.orientation_b * InfinitePlaneWide::NORMAL;

        // hemisphere center's offset + contact normal's push
        manifold.offset_a[0] = y_axis * a.half_height - manifold.normal * a.radius;
        manifold.offset_a[1] = y_axis * (-a.half_height) - manifold.normal * a.radius;

        manifold.feature_id[0] = u32x4::ZERO;
        manifold.feature_id[1] = u32x4::ONE;
    }
}

impl_pair_narrowphase!(Capsule, InfinitePlane, CapsuleWide, InfinitePlaneWide, 2);