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,
);
}
fn v_crease(world: &mut PhysicsWorld, turn: glam::DQuat) -> ColliderId {
use crate::{Transform, TriangleMesh};
let rise = 5.0 * 3_f64.sqrt();
let mesh = TriangleMesh::new(
&[
DVec3::new(-5.0, rise, -5.0),
DVec3::new(-5.0, rise, 5.0),
DVec3::new(0.0, 0.0, -5.0),
DVec3::new(0.0, 0.0, 5.0),
DVec3::new(5.0, rise, -5.0),
DVec3::new(5.0, rise, 5.0),
],
&[[0, 1, 3], [0, 3, 2], [2, 3, 5], [2, 5, 4]],
)
.expect("two faces");
world.add_mesh(
mesh,
Transform {
rotation: turn,
..Transform::IDENTITY
},
)
}
fn tick(
character: &mut CharacterController,
world: &mut PhysicsWorld,
fall: &mut f64,
walk: DVec3,
) -> bool {
const DT: f64 = 1.0 / 60.0;
*fall -= 9.81 * DT;
let outcome = character.move_and_slide(world, walk + DVec3::Y * (*fall * DT));
if outcome.grounded {
*fall = 0.0;
}
outcome.grounded
}
#[test]
fn a_character_wedged_in_a_v_crease_is_grounded() {
let mut world = PhysicsWorld::new();
v_crease(&mut world, glam::DQuat::IDENTITY);
let mut character =
CharacterController::new(CharacterConfig::default(), DVec3::new(0.0, 1.0, 0.0));
let mut fall = 0.0;
let grounded = (0..300)
.map(|_| tick(&mut character, &mut world, &mut fall, DVec3::ZERO))
.last()
.expect("ticks ran");
assert!(
grounded,
"settled in the crease at {:?}",
character.position()
);
let ground = character.ground().expect("ground");
assert!(
(ground.normal - DVec3::Y).length() < 1e-6,
"the crease's footing points up: {:?}",
ground.normal
);
}
#[test]
fn a_character_walks_along_a_v_crease() {
let mut world = PhysicsWorld::new();
v_crease(&mut world, glam::DQuat::IDENTITY);
let mut character =
CharacterController::new(CharacterConfig::default(), DVec3::new(0.0, 1.0, 0.0));
let mut fall = 0.0;
for _ in 0..120 {
tick(&mut character, &mut world, &mut fall, DVec3::ZERO);
}
let start = character.position().z;
for step in 0..60 {
assert!(
tick(&mut character, &mut world, &mut fall, DVec3::Z * 0.05),
"left the ground on step {step} at {:?}",
character.position()
);
}
let walked = character.position().z - start;
assert!((walked - 3.0).abs() < 0.05, "walked {walked} m");
}
#[test]
fn a_v_crease_too_steep_along_its_length_is_no_footing() {
let mut world = PhysicsWorld::new();
v_crease(
&mut world,
rotation_from_scaled_axis(DVec3::X * 50_f64.to_radians()),
);
let config = CharacterConfig::default();
let character = CharacterController::new(config, DVec3::new(0.0, 1.0, 0.0));
let probe = character
.probe_ground_at(&mut world, DVec3::new(0.0, 1.0, 0.0), 2.0)
.expect("the crease is below");
assert!(!probe.walkable, "{probe:?}");
}