//! M4a: terrain generated from a seed carries a body without falling through. #![allow( clippy::unwrap_used, clippy::expect_used, reason = "test helper: a failure should abort the test" )] use fabrilis_core::snapshot::hash::StateHasher; use fabrilis_core::time::SimDelta; use fabrilis_core::units::Kilograms; use fabrilis_core::vec::{Quat, Vec3}; use fabrilis_physics::RapierBackend; use fabrilis_physics::backend::{BodyDesc, PhysicsBackend, ShapeDesc, TrimeshDesc}; use fabrilis_worldgen::terrain::{Heightmap, TerrainConfig}; const STEPS: usize = 3_000; fn terrain(seed: u64, height_scale: f64, size: usize) -> (RapierBackend, Heightmap) { let config = TerrainConfig { seed, size, extent: 100.0, height_scale, frequency: 2.0, octaves: 4, ..TerrainConfig::default() }; let map = Heightmap::generate(config).expect("the terrain configuration is valid"); let mesh = map.mesh(); let mut world = RapierBackend::new(); let terrain = world .add_trimesh(TrimeshDesc { vertices: mesh.vertices, indices: mesh.indices, position: Vec3::ZERO, rotation: Quat::IDENTITY, }) .expect("the terrain mesh is valid"); assert!(world.body_transform(terrain).is_some()); (world, map) } #[test] fn a_ball_rests_on_flat_terrain() { let (mut world, _map) = terrain(1, 0.0, 33); let radius = 0.25; let ball = world.add_body(BodyDesc::dynamic( ShapeDesc::Ball { radius }, Kilograms::new(1.0), Vec3::new(0.0, 0.0, 3.0), )); let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second"); for _ in 0..STEPS { world.step(dt); } let z = world .body_transform(ball) .expect("the ball exists") .position .z; assert!((z - radius).abs() < 0.02, "ball z = {z}"); } #[test] fn a_box_stays_above_a_seeded_relief() { let (world, map) = terrain(42, 4.0, 65); let mut world = world; let start = map.height_at(0.0, 0.0) + 3.0; let body = world.add_body(BodyDesc::dynamic( ShapeDesc::Cuboid { half_extents: Vec3::new(0.25, 0.25, 0.1), }, Kilograms::new(5.0), Vec3::new(0.0, 0.0, start), )); let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second"); for _ in 0..STEPS { world.step(dt); } let position = world.body_transform(body).expect("the box exists").position; assert!(position.is_finite()); let ground = map.height_at(position.x, position.y); assert!( position.z >= ground - 0.05, "box at {position:?} below terrain {ground}" ); } #[test] fn the_terrain_scene_is_deterministic() { let (first, second) = (run_scene(), run_scene()); assert_eq!(first.0.x.to_bits(), second.0.x.to_bits()); assert_eq!(first.0.y.to_bits(), second.0.y.to_bits()); assert_eq!(first.0.z.to_bits(), second.0.z.to_bits()); assert_eq!(first.1, second.1); } #[test] fn the_terrain_trajectory_matches_golden() { let (_, hash) = run_scene(); assert_eq!(hash, GOLDEN, "new hash: {hash:#018x}"); } const GOLDEN: u64 = 0xbb09_e470_43d3_1f2e; fn run_scene() -> (Vec3, u64) { let (mut world, map) = terrain(42, 4.0, 65); let start = map.height_at(0.0, 0.0) + 3.0; let body = world.add_body(BodyDesc::dynamic( ShapeDesc::Cuboid { half_extents: Vec3::new(0.25, 0.25, 0.1), }, Kilograms::new(5.0), Vec3::new(0.0, 0.0, start), )); let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second"); let mut hasher = StateHasher::new(); let mut last = Vec3::ZERO; for step in 0..1_000 { world.step(dt); last = world.body_transform(body).expect("the box exists").position; hasher.write_u64(step as u64); hasher.write_f64(last.x); hasher.write_f64(last.y); hasher.write_f64(last.z); } (last, hasher.finish()) }