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 approx_det::assert_relative_eq;
use wasm_bindgen_test::*;

use crate::collision_tasks::tests::common::{
    convex1contact_manifold_wide_to_convex1contact_manifold, get_input_wide,
    Convex1ContactManifold, Mvec3, TestInput,
};
use crate::collision_tasks::ShapeWideTester;
use crate::shapes::{CapsuleWide, SphereWide};
use crate::traits::PairWideTest;
use crate::{Capsule, Sphere};
wasm_bindgen_test_configure!(run_in_browser);
pub(crate) type InputType = TestInput<Sphere, Capsule>;
const ADJUST_DEPTH_FACTOR: f32 = 0.5;

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_random() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/random.json");

    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: -0.030215789,
            y: -0.20614994,
            z: 0.12844165,
        },
        normal: Mvec3 {
            x: 0.12345008,
            y: 0.8422493,
            z: -0.5247631,
        },
        depth: -0.08952239,
        feature_id: 0,
        contact_exists: false,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_close_at_hemispherical_end() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/close_at_hemispherical_end.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.5005,
            z: 0.0,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        },
        depth: -0.0010000467,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_tangent_at_hemispherical_end() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/tangent_at_hemispherical_end.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.5,
            z: 0.0,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        },
        depth: 0.0,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_penetrate_at_hemispherical_end() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/penetrate_at_hemispherical_end.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.49949995,
            z: 0.0,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 0.99999994,
            z: 0.0,
        },
        depth: 0.0010000467,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_close_at_cylindrical_part() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/close_at_cylindrical_part.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.5005,
            z: -2.9832142E-7,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 1.,
            z: 5.960468E-7,
        },
        depth: -0.0010000467,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_tangent_at_cylindrical_part() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/tangent_at_cylindrical_part.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.5,
            z: -2.980234E-7,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 1.,
            z: 5.960468E-7,
        },
        depth: 0.0,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
fn test_sphere_capsule_tester_penetrate_at_cylindrical_part() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_capsule/penetrate_at_cylindrical_part.json");
    let input0: InputType = serde_json::from_slice(file_input).expect("file should be proper JSON");

    let mut output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: -0.4995,
            z: -2.9772536E-7,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 1.,
            z: 5.960468E-7,
        },
        depth: 0.0009999871,
        feature_id: 0,
        contact_exists: true,
    };
    output0.adjust_offset_a_to_shape_surface(ADJUST_DEPTH_FACTOR);
    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();
    TestWide!(
        SphereWide,
        CapsuleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}