use std::f64::consts::{FRAC_PI_2, PI};
use glam::DVec3;
use crate::collider::{BoxCollider, Capsule, LyingCapsule, Sphere};
use crate::components::Transform;
use crate::compound_shape::{CompoundPart, CompoundShape};
use crate::integrator::rotation_from_scaled_axis;
use crate::mesh::TriangleMesh;
use super::{ALL_LAYERS, ColliderId, PhysicsWorld, QueryFilter};
const RADIUS: f64 = 0.5;
const LENGTH: f64 = 1.5;
fn lying(yaw: f64) -> LyingCapsule {
LyingCapsule::new(DVec3::new(0.0, RADIUS, 0.0), yaw, RADIUS, LENGTH)
}
fn blocker(world: &mut PhysicsWorld, capsule: &LyingCapsule) -> Option<ColliderId> {
world.lying_capsule_blocker(capsule, QueryFilter::ALL)
}
fn square(half: f64) -> TriangleMesh {
TriangleMesh::new(
&[
DVec3::new(-half, 0.0, -half),
DVec3::new(-half, 0.0, half),
DVec3::new(half, 0.0, half),
DVec3::new(half, 0.0, -half),
],
&[[0, 1, 2], [0, 2, 3]],
)
.unwrap()
}
fn mesh_wall(world: &mut PhysicsWorld, near: f64) -> ColliderId {
world.add_mesh(
square(1.0),
Transform::new(
DVec3::new(0.0, 1.0, near),
rotation_from_scaled_axis(DVec3::X * FRAC_PI_2),
),
)
}
#[test]
fn a_lying_capsule_in_open_space_is_blocked_by_nothing() {
let mut world = PhysicsWorld::new();
world.add_box(BoxCollider::new(
DVec3::new(10.0, 1.0, 10.0),
DVec3::splat(1.0),
));
for yaw in [0.0, FRAC_PI_2, PI, 3.0 * FRAC_PI_2, 0.3] {
assert_eq!(blocker(&mut world, &lying(yaw)), None, "yaw {yaw}");
}
}
#[test]
fn a_wall_behind_the_head_blocks_the_legs_until_the_body_turns_away() {
let mut world = PhysicsWorld::new();
let wall = world.add_box(BoxCollider::new(
DVec3::new(0.0, 1.0, 1.5),
DVec3::new(2.0, 1.0, 0.5),
));
assert_eq!(
world.lying_capsule_blocker(
&LyingCapsule::new(DVec3::new(0.0, RADIUS, 0.0), 0.0, RADIUS, 0.0),
QueryFilter::ALL,
),
None,
"the head alone is clear of the wall, so a sphere there fits"
);
assert_eq!(blocker(&mut world, &lying(0.0)), Some(wall));
assert_eq!(blocker(&mut world, &lying(PI)), None, "feet toward -Z");
assert_eq!(
blocker(&mut world, &lying(FRAC_PI_2)),
None,
"feet toward +X"
);
}
#[test]
fn every_collider_kind_blocks_a_capsule_lying_into_it() {
type Place = fn(&mut PhysicsWorld, f64) -> ColliderId;
let kinds: [(&str, Place); 5] = [
("sphere", |world, near| {
world.add_sphere(Sphere::new(DVec3::new(0.0, RADIUS, near + 0.4), 0.4))
}),
("box", |world, near| {
world.add_box(BoxCollider::new(
DVec3::new(0.0, 1.0, near + 0.5),
DVec3::new(1.0, 1.0, 0.5),
))
}),
("capsule", |world, near| {
world.add_capsule(Capsule::new(DVec3::new(0.0, 1.0, near + 0.4), 0.4, 1.0))
}),
("mesh", mesh_wall),
("compound", |world, near| {
let part =
|centre: DVec3, half: DVec3| CompoundPart::new(centre, glam::DQuat::IDENTITY, half);
let shape = CompoundShape::new(vec![
part(DVec3::new(0.0, 2.5, -0.5), DVec3::new(1.0, 0.5, 1.0)),
part(DVec3::new(0.0, 1.0, 0.5), DVec3::new(1.0, 1.0, 0.5)),
])
.expect("a valid shape");
world.add_compound(
&shape,
DVec3::ZERO,
&Transform::from_position(DVec3::new(0.0, 0.0, near)),
)
}),
];
for (kind, place) in kinds {
let mut world = PhysicsWorld::new();
let obstacle = place(&mut world, 1.9);
assert_eq!(
blocker(&mut world, &lying(0.0)),
Some(obstacle),
"a {kind} a tenth of a metre inside the feet end"
);
let mut world = PhysicsWorld::new();
place(&mut world, 2.1);
assert_eq!(
blocker(&mut world, &lying(0.0)),
None,
"a {kind} a tenth of a metre past the feet end"
);
}
}
#[test]
fn a_floor_touched_at_resting_height_does_not_block() {
let mut boxed = PhysicsWorld::new();
let box_floor = boxed.add_box(BoxCollider::new(
DVec3::new(0.0, -1.0, 0.0),
DVec3::new(50.0, 1.0, 50.0),
));
let mut meshed = PhysicsWorld::new();
let mesh_floor = meshed.add_mesh(square(50.0), Transform::from_position(DVec3::ZERO));
for (world, floor) in [(&mut boxed, box_floor), (&mut meshed, mesh_floor)] {
for yaw in [0.0, FRAC_PI_2, PI, 3.0 * FRAC_PI_2, 0.3] {
assert_eq!(blocker(world, &lying(yaw)), None, "resting, yaw {yaw}");
let sunk = LyingCapsule {
head: DVec3::new(0.0, RADIUS - 1e-6, 0.0),
..lying(yaw)
};
assert_eq!(blocker(world, &sunk), Some(floor), "sunk, yaw {yaw}");
}
}
}
#[test]
fn an_excluded_masked_out_or_trigger_collider_does_not_block() {
const LEVEL: u32 = 1 << 0;
const ITEMS: u32 = 1 << 1;
let mut world = PhysicsWorld::new();
let own = world.add_sphere(Sphere::new(DVec3::new(0.0, RADIUS, 0.75), RADIUS));
let item = world.add_sphere(Sphere::new(DVec3::new(0.0, RADIUS, 1.0), 0.25));
assert!(world.set_layers(item, ITEMS));
let trigger = world.add_sphere(Sphere::new(DVec3::new(0.0, RADIUS, 1.25), 0.25));
assert!(world.set_trigger(trigger, true));
for id in [own, trigger] {
assert!(world.set_layers(id, LEVEL));
}
let filter = QueryFilter::excluding(Some(own)).with_mask(LEVEL);
assert_eq!(world.lying_capsule_blocker(&lying(0.0), filter), None);
assert_eq!(
world.lying_capsule_blocker(&lying(0.0), filter.with_mask(ALL_LAYERS)),
Some(item),
"the item blocks once the mask includes it"
);
let wall = mesh_wall(&mut world, 1.9);
assert!(world.set_layers(wall, LEVEL));
assert_eq!(world.lying_capsule_blocker(&lying(0.0), filter), Some(wall));
}
#[test]
fn a_pitched_capsule_is_checked_where_its_pitch_puts_its_feet() {
let mut world = PhysicsWorld::new();
let floor = world.add_box(BoxCollider::new(
DVec3::new(0.0, -1.0, 0.0),
DVec3::new(50.0, 1.0, 50.0),
));
for yaw in [0.0, FRAC_PI_2, 0.3] {
let level = lying(yaw);
assert_eq!(blocker(&mut world, &level), None, "yaw {yaw}");
assert_eq!(
blocker(&mut world, &level.with_pitch_sine(0.01)),
Some(floor),
"yaw {yaw}, head up"
);
assert_eq!(
blocker(&mut world, &level.with_pitch_sine(-0.01)),
None,
"yaw {yaw}, head down"
);
}
}