use core::f64::consts::FRAC_PI_2;
use glam::{DQuat, DVec3};
use crate::broadphase::Ray;
use crate::components::{ColliderComponent, RigidBody, Transform};
use crate::system::PhysicsSystem;
fn quarter() -> DQuat {
crate::rotation_from_scaled_axis(DVec3::Z * FRAC_PI_2)
}
const CLOSE: f64 = 1e-12;
fn body(rotation: DQuat, component: &ColliderComponent) -> (PhysicsSystem, crcbl_ecs::Entity) {
let entity =
crcbl_ecs::Entity::from_bits((1u64 << 32) | 3).expect("generation 1 is never zero");
let transform = Transform::new(DVec3::ZERO, rotation);
let mut phys = PhysicsSystem::new();
phys.set_body(entity, RigidBody::new_kinematic());
phys.set_transform(entity, transform);
phys.set_collider(entity, component, &transform);
(phys, entity)
}
#[test]
fn a_turned_bodys_sphere_is_hit_at_its_turned_offset() {
let sphere = ColliderComponent::Sphere {
offset: DVec3::new(0.0, 2.0, 0.0),
radius: 0.5,
is_trigger: false,
};
let (mut phys, entity) = body(quarter(), &sphere);
let down = Ray::new(DVec3::new(-2.0, 5.0, 0.0), DVec3::NEG_Y);
let (hit, at) = phys
.cast_ray(&down)
.expect("the sphere at its turned offset");
assert_eq!(hit, entity);
assert!((at.t - 4.5).abs() < CLOSE, "{at:?}");
assert!((at.normal - DVec3::Y).length() < CLOSE, "{at:?}");
let unturned = Ray::new(DVec3::new(5.0, 2.0, 0.0), DVec3::NEG_X);
assert_eq!(
phys.cast_ray(&unturned),
None,
"nothing at the unturned offset"
);
phys.step(1.0 / 60.0);
assert!(phys.cast_ray(&down).is_some(), "placed again after a step");
}
#[test]
fn a_turned_bodys_capsule_is_centred_at_its_turned_offset() {
let capsule = ColliderComponent::Capsule {
offset: DVec3::new(0.0, 2.0, 0.0),
radius: 0.25,
half_height: 0.5,
is_trigger: false,
};
let (phys, entity) = body(quarter(), &capsule);
let bounds = phys
.world()
.aabb_of(phys.collider_of(entity).expect("a collider"))
.expect("in the world");
let centre = bounds.centre();
assert!(
(centre - DVec3::new(-2.0, 0.0, 0.0)).length() < CLOSE,
"{bounds:?}"
);
}