use std::f64::consts::FRAC_PI_6;
use glam::DVec3;
use crate::collider::BoxCollider;
use crate::integrator::rotation_from_scaled_axis;
use crate::world::{ColliderId, PhysicsWorld};
use super::{CharacterConfig, CharacterController};
fn corner(world: &mut PhysicsWorld) -> (ColliderId, ColliderId) {
let wall = world.add_box(BoxCollider::new(
DVec3::new(6.0, 0.0, 0.0),
DVec3::new(1.0, 5.0, 10.0),
));
let slab = world.add_box(
BoxCollider::new(DVec3::new(4.6, 0.0, -2.0), DVec3::new(0.25, 5.0, 1.0))
.with_rotation(rotation_from_scaled_axis(DVec3::Y * FRAC_PI_6)),
);
(wall, slab)
}
#[test]
fn a_character_falling_into_a_corner_keeps_falling_down_the_crease() {
let config = CharacterConfig::default();
let mut world = PhysicsWorld::new();
let (wall, slab) = corner(&mut world);
let mut character = CharacterController::new(config, DVec3::new(3.0, 2.0, -1.0));
let fall = -0.01;
let motion = DVec3::new(0.08, fall, -0.03);
let mut contacts = Vec::new();
let mut wedged = false;
let mut last = DVec3::ZERO;
for tick in 0..200 {
let outcome = character.move_and_slide_into(&mut world, motion, &mut contacts);
assert!(
outcome.motion.y < 0.0,
"tick {tick} stopped falling at {:?}, met {contacts:?}",
character.position(),
);
let met = |collider| contacts.iter().any(|contact| contact.collider == collider);
wedged |= met(wall) && met(slab);
last = outcome.motion;
}
assert!(
wedged,
"the move never met the wall and the box together, so the crease this \
is about never ran"
);
assert!(
(last - DVec3::Y * fall).length() < 1e-12,
"wedged in a vertical crease, the whole fall survives and nothing else \
does: {last:?}",
);
}
#[test]
fn no_start_falling_into_the_corner_hangs_on_the_turned_face() {
const COLUMNS: u32 = 60;
const ROWS: u32 = 80;
const TICKS: u32 = 100;
let (x_from, x_to) = (1.0, 4.0);
let (z_from, z_to) = (-2.0, 2.0);
let motion = DVec3::new(0.05, -0.01, -0.05);
let mut world = PhysicsWorld::new();
let (_, slab) = corner(&mut world);
let mut contacts = Vec::new();
let mut hung = Vec::new();
let mut met_slab = 0;
for column in 0..COLUMNS {
for row in 0..ROWS {
let start = DVec3::new(
x_from + (x_to - x_from) * f64::from(column) / f64::from(COLUMNS),
2.0,
z_from + (z_to - z_from) * f64::from(row) / f64::from(ROWS),
);
let mut character = CharacterController::new(CharacterConfig::default(), start);
let mut met = false;
for tick in 0..TICKS {
let outcome = character.move_and_slide_into(&mut world, motion, &mut contacts);
met |= contacts.iter().any(|contact| contact.collider == slab);
if outcome.motion.y >= 0.0 {
hung.push((start, tick, character.position()));
break;
}
}
met_slab += u32::from(met);
}
}
assert!(
met_slab > COLUMNS * ROWS / 2,
"only {met_slab} starts met the slab, so the scan mostly fell past the \
face this is about"
);
assert!(
hung.is_empty(),
"{} of {} starts stopped falling in mid-air (start, tick, position): \
{hung:?}",
hung.len(),
COLUMNS * ROWS,
);
}