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 super::common::{
    convex1contact_manifold_wide_to_convex1contact_manifold, get_input_wide,
    Convex1ContactManifold, Mvec3, TestInput,
};
use crate::shapes::{Triangle, TriangleWide};
use crate::{PairWideTest, ShapeWideTester, Sphere, SphereWide};

wasm_bindgen_test_configure!(run_in_browser);
type InputType = TestInput<Sphere, Triangle>;

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_no_collide() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_triangle/nocollide.json");

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

    let output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: -0.0,
            y: -0.0,
            z: -0.6,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 0.0,
            z: 1.0,
        },
        depth: -0.1,
        feature_id: 1,
        contact_exists: false,
    };

    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        SphereWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_on_plane() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_triangle/collide_on_plane.json");

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

    let output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.10937982,
            y: 0.29367822,
            z: 0.16116269,
        },
        normal: Mvec3 {
            x: -0.31038722,
            y: -0.8333713,
            z: -0.45733166,
        },
        depth: 0.14760205,
        feature_id: 1,
        contact_exists: true,
    };

    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        SphereWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}

#[test]
#[wasm_bindgen_test]
#[cfg(feature = "serde")]
fn test_collide_on_backface() {
    let _ = env_logger::builder().is_test(true).try_init();

    let file_input = include_bytes!("resource/sphere_triangle/collide_on_backface.json");

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

    let output0 = Convex1ContactManifold {
        offset_a: Mvec3 {
            x: 0.0,
            y: 0.23489934,
            z: 0.33557045,
        },
        normal: Mvec3 {
            x: 0.0,
            y: 0.57346237,
            z: 0.81923187,
        },
        depth: 0.70961595,
        feature_id: 1,
        contact_exists: true,
    };

    let array = [input0];
    let outputs = [output0];
    let pair_count = array.len();

    TestWide!(
        SphereWide,
        TriangleWide,
        array,
        pair_count,
        outputs,
        convex1contact_manifold_wide_to_convex1contact_manifold
    );
}