//! M4c regression: the three dropped boxes settle upright and stay put. #![allow( clippy::unwrap_used, clippy::expect_used, reason = "test helper: a failure should abort the test" )] 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 BOX: f64 = 0.6; const DROPS: [(f64, f64, f64); 3] = [(-1.5, 0.4, 2.0), (1.2, -0.8, 3.6), (0.2, 1.8, 5.2)]; #[test] fn dropped_boxes_settle_and_stop() { let mut config = TerrainConfig { seed: 42, size: 65, extent: 100.0, height_scale: 4.0, ..TerrainConfig::default() }; config.frequency = 2.0; let map = Heightmap::generate(config).expect("terrain"); let mesh = map.mesh(); let mut world = RapierBackend::new(); world .add_trimesh(TrimeshDesc { vertices: mesh.vertices, indices: mesh.indices, position: Vec3::ZERO, rotation: Quat::IDENTITY, }) .expect("terrain collider"); let mut bodies = Vec::new(); for (x, y, drop) in DROPS { let center = Vec3::new(x, y, map.height_at(x, y) + drop + BOX * 0.5); bodies.push(world.add_body(BodyDesc::dynamic( ShapeDesc::Cuboid { half_extents: Vec3::new(BOX * 0.5, BOX * 0.5, BOX * 0.5), }, Kilograms::new(4.0), center, ))); } let dt = SimDelta::from_hz(1_000).expect("1 kHz"); let mut before = Vec::new(); for step in 0..5_000 { world.step(dt); if step == 3_999 { before = bodies .iter() .map(|&body| world.body_transform(body).expect("body").position) .collect(); } } for (index, &body) in bodies.iter().enumerate() { let transform = world.body_transform(body).expect("body"); let ground = map.height_at(transform.position.x, transform.position.y); println!( "box {index}: pos {:?} ground {ground:.3}", transform.position ); assert!( transform.position.z >= ground - 0.05, "box {index} sank through the terrain" ); let moved = (transform.position - before[index]).length(); assert!(moved < 1e-3, "box {index} still moves: {moved}"); } } /// Diagnostic: `cargo test -p fabrilis-modes --test box_drops -- --ignored --nocapture`. #[test] #[ignore = "diagnostic timing, not a pass/fail test"] #[allow( clippy::disallowed_methods, reason = "diagnostic timing uses the wall clock on purpose" )] fn physics_step_time_spikes() { let config = TerrainConfig { seed: 42, size: 65, extent: 100.0, height_scale: 4.0, frequency: 2.0, ..TerrainConfig::default() }; let map = Heightmap::generate(config).expect("terrain"); let mesh = map.mesh(); let mut world = RapierBackend::new(); world .add_trimesh(TrimeshDesc { vertices: mesh.vertices, indices: mesh.indices, position: Vec3::ZERO, rotation: Quat::IDENTITY, }) .expect("terrain collider"); for (x, y, drop) in DROPS { let center = Vec3::new(x, y, map.height_at(x, y) + drop + BOX * 0.5); world.add_body(BodyDesc::dynamic( ShapeDesc::Cuboid { half_extents: Vec3::new(BOX * 0.5, BOX * 0.5, BOX * 0.5), }, Kilograms::new(4.0), center, )); } let dt = SimDelta::from_hz(1_000).expect("1 kHz"); let mut times = Vec::with_capacity(5_000); for _ in 0..5_000 { let start = std::time::Instant::now(); world.step(dt); times.push((start.elapsed().as_secs_f64() * 1_000.0, times.len())); } let total: f64 = times.iter().map(|&(ms, _)| ms).sum(); let mut sorted = times.clone(); sorted.sort_by(|a, b| b.0.total_cmp(&a.0)); println!("mean step {:.3} ms, total {:.1} ms", total / 5_000.0, total); for &(ms, index) in sorted.iter().take(10) { println!("slow step {index}: {ms:.3} ms"); } }