Finish M4: cart, log facade, idle diagnostics and terrain LOD

Add the core::log facade with five levels and a sink; migrate the app dev log and enable trace in --dev. Replace the dropped cubes with a four-wheel cart on revolute joints driven by velocity motors, add WASD controls, a golden trajectory and a slope test. Instrument idle frame spikes and throttle the window title. Add chunked terrain LOD with skirts (2368 vs 8192 triangles) while physics keeps the full mesh. Pin Rust 1.100.0; edition stays 2024.
This commit is contained in:
Loki 2026-11-17 14:32:52 +01:00
parent 190ef9cc4b
commit 1042269d7e
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
38 changed files with 2348 additions and 374 deletions

View file

@ -1,5 +1,6 @@
//! Fixed-step physics for the boxes shown in the window.
//! Fixed-step physics for the cart shown in the window.
mod cart;
mod pacing;
#[cfg(test)]
@ -8,45 +9,25 @@ mod tests;
use std::time::Duration;
use fabrilis_core::time::SimDelta;
use fabrilis_core::units::Kilograms;
use fabrilis_core::vec::{Quat, Transform, Vec3};
use fabrilis_physics::{BodyDesc, BodyId, PhysicsBackend, RapierBackend, ShapeDesc, TrimeshDesc};
use fabrilis_render::{Instance, Scene};
use fabrilis_core::vec::{Quat, Vec3};
use fabrilis_physics::{PhysicsBackend, RapierBackend, TrimeshDesc};
use fabrilis_render::Scene;
use fabrilis_worldgen::terrain::{Heightmap, TerrainMesh};
use crate::runtime::AppError;
use crate::runtime::metrics::Stopwatch;
use cart::{Cart, CartInfo, CartPose};
use pacing::{MAX_STEPS, Pacer};
pub(crate) use cart::Drive;
/// Physics rate; the step never changes with the frame time.
const HZ: u64 = 1_000;
/// Edge length of each dropped box, metres.
pub(crate) const BOX_SIZE: f64 = 0.6;
/// Mass of each dropped box, kilograms.
pub(crate) const BOX_MASS: f64 = 4.0;
/// Horizontal offset and drop height above the terrain for each box.
pub(crate) const DROPS: [(f64, f64, f64); 3] =
[(-1.5, 0.4, 2.0), (1.2, -0.8, 3.6), (0.2, 1.8, 5.2)];
/// One colour per box, linear RGBA.
const COLORS: [[f32; 4]; 3] = [
[0.85, 0.35, 0.15, 1.0],
[0.20, 0.55, 0.85, 1.0],
[0.85, 0.75, 0.20, 1.0],
];
struct BoxBody {
id: BodyId,
color: [f32; 4],
}
/// The Rapier world and the boxes it drives.
/// The Rapier world and the cart it drives.
pub(crate) struct Simulation {
backend: RapierBackend,
boxes: Vec<BoxBody>,
cart: Cart,
step: SimDelta,
pacer: Pacer,
total_steps: u64,
@ -54,7 +35,7 @@ pub(crate) struct Simulation {
}
impl Simulation {
/// Builds the terrain collider and drops the boxes into the scene.
/// Builds the terrain collider and spawns the cart into the scene.
pub(crate) fn new(
terrain: &TerrainMesh,
heightmap: &Heightmap,
@ -73,29 +54,10 @@ impl Simulation {
backend
.add_trimesh(collider)
.ok_or_else(|| AppError::Physics("terrain collider was rejected".to_owned()))?;
scene.instances.clear();
let mut boxes = Vec::with_capacity(DROPS.len());
for (offset, color) in DROPS.iter().zip(COLORS) {
let (x, y, drop) = *offset;
let center = Vec3::new(x, y, heightmap.height_at(x, y) + drop + BOX_SIZE * 0.5);
let id = backend.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(BOX_SIZE * 0.5, BOX_SIZE * 0.5, BOX_SIZE * 0.5),
},
Kilograms::new(BOX_MASS),
center,
));
let size = BOX_SIZE as f32;
scene.instances.push(Instance::box_at(
[center.x as f32, center.y as f32, center.z as f32],
[size, size, size],
color,
));
boxes.push(BoxBody { id, color });
}
let cart = Cart::new(&mut backend, heightmap, scene);
Ok(Self {
backend,
boxes,
cart,
step,
pacer: Pacer::new(),
total_steps: 0,
@ -108,19 +70,31 @@ impl Simulation {
/// Diagnostics time each step individually, but only observe it: the number
/// of steps, their order and the Rapier calls stay exactly the same, so the
/// resulting state hash does not depend on logging being enabled.
pub(crate) fn advance(&mut self, scene: &mut Scene) -> pacing::Frame {
pub(crate) fn advance(&mut self, scene: &mut Scene, drive: Drive) -> pacing::Frame {
let frame = self.pacer.advance(self.step.nanos(), MAX_STEPS);
self.slowest_step = Duration::ZERO;
self.total_steps = self.total_steps.saturating_add(u64::from(frame.steps));
for _ in 0..frame.steps {
let step_start = Stopwatch::start();
self.backend.step(self.step);
self.slowest_step = self.slowest_step.max(step_start.elapsed());
}
self.sync(scene);
self.cart.drive(&mut self.backend, drive);
self.run_steps(frame.steps);
self.cart.sync(&self.backend, scene);
frame
}
/// Runs exactly `steps` fixed steps with no wall clock, for golden tests and
/// a future headless CLI. The motors are set once, before the batch.
#[allow(
dead_code,
reason = "the headless CLI will step the same world without wall clock"
)]
pub(crate) fn step_fixed(&mut self, steps: u32, drive: Drive) {
self.slowest_step = Duration::ZERO;
self.total_steps = self.total_steps.saturating_add(u64::from(steps));
self.cart.drive(&mut self.backend, drive);
for _ in 0..steps {
self.backend.step(self.step);
}
}
/// Simulated seconds represented by the steps that have actually run.
#[must_use]
pub(crate) fn sim_seconds(&self) -> f64 {
@ -133,30 +107,28 @@ impl Simulation {
self.slowest_step
}
/// Copies every body transform into its rendered instance.
fn sync(&self, scene: &mut Scene) {
for (instance, body) in scene.instances.iter_mut().zip(&self.boxes) {
let Some(transform) = self.backend.body_transform(body.id) else {
continue;
};
instance.model = model(transform, BOX_SIZE as f32);
instance.color = body.color;
/// The current chassis pose and wheel angles, used by the golden test.
#[allow(
dead_code,
reason = "read by the golden test and by the future headless CLI"
)]
#[must_use]
pub(crate) fn cart_pose(&self) -> Option<CartPose> {
self.cart.pose(&self.backend)
}
/// What the startup banner reports about the cart.
#[must_use]
pub(crate) fn cart_info(&self) -> CartInfo {
self.cart.info()
}
/// Steps the backend while observing how long each step took.
fn run_steps(&mut self, steps: u32) {
for _ in 0..steps {
let step_start = Stopwatch::start();
self.backend.step(self.step);
self.slowest_step = self.slowest_step.max(step_start.elapsed());
}
}
}
/// A column-major model matrix for a rotated, translated box.
fn model(transform: Transform, size: f32) -> [[f32; 4]; 4] {
let [x, y, z] = transform.rotation.to_mat3().cols();
let [px, py, pz] = [
transform.position.x as f32,
transform.position.y as f32,
transform.position.z as f32,
];
[
[x.x as f32 * size, x.y as f32 * size, x.z as f32 * size, 0.0],
[y.x as f32 * size, y.y as f32 * size, y.z as f32 * size, 0.0],
[z.x as f32 * size, z.y as f32 * size, z.z as f32 * size, 0.0],
[px, py, pz, 1.0],
]
}