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:
parent
190ef9cc4b
commit
1042269d7e
38 changed files with 2348 additions and 374 deletions
|
|
@ -5,7 +5,7 @@
|
|||
//! (docs/COORDINATES.md).
|
||||
|
||||
use fabrilis_core::time::SimDelta;
|
||||
use fabrilis_core::units::{Kilograms, Radians};
|
||||
use fabrilis_core::units::{Kilograms, Radians, RadiansPerSecond};
|
||||
use fabrilis_core::vec::{Quat, Transform, Vec3};
|
||||
|
||||
use crate::joints::{JointId, RevoluteDesc};
|
||||
|
|
@ -132,6 +132,14 @@ pub trait PhysicsBackend {
|
|||
/// ignored. See [`RapierBackend`](crate::rapier::RapierBackend) for the model.
|
||||
fn set_motor_position(&mut self, joint: JointId, target: Radians, stiffness: f64, damping: f64);
|
||||
|
||||
/// Drives a joint at `target` angular velocity with a velocity motor.
|
||||
///
|
||||
/// Unlike [`set_motor_position`](Self::set_motor_position) the motor tracks a
|
||||
/// speed instead of an angle, which is what wheels need. `factor` is the
|
||||
/// motor's damping coefficient: a larger value follows the target more
|
||||
/// tightly. Unknown joints are ignored.
|
||||
fn set_motor_velocity(&mut self, joint: JointId, target: RadiansPerSecond, factor: f64);
|
||||
|
||||
/// Sets the world-frame torque for the next step, N·m; replaces the previous one.
|
||||
///
|
||||
/// A controller calls this every step; unknown bodies are ignored.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
//! determinism caveats.
|
||||
|
||||
use fabrilis_core::time::SimDelta;
|
||||
use fabrilis_core::units::{Kilograms, Radians};
|
||||
use fabrilis_core::units::{Kilograms, Radians, RadiansPerSecond};
|
||||
use fabrilis_core::vec::{Quat, Transform, Vec3};
|
||||
use rapier3d_f64::dynamics::{
|
||||
CCDSolver, ImpulseJointHandle, ImpulseJointSet, IntegrationParameters, IslandManager,
|
||||
|
|
@ -159,6 +159,18 @@ impl PhysicsBackend for RapierBackend {
|
|||
.set_motor_position(JointAxis::AngX, target.get(), stiffness, damping);
|
||||
}
|
||||
|
||||
fn set_motor_velocity(&mut self, joint: JointId, target: RadiansPerSecond, factor: f64) {
|
||||
let Some(handle) = self.joint_handle(joint) else {
|
||||
return;
|
||||
};
|
||||
let Some(entry) = self.joints.get_mut(handle, true) else {
|
||||
return;
|
||||
};
|
||||
entry
|
||||
.data
|
||||
.set_motor_velocity(JointAxis::AngX, target.get(), factor);
|
||||
}
|
||||
|
||||
fn apply_torque(&mut self, body: BodyId, torque: Vec3) {
|
||||
let Some(handle) = self.body_handle(body) else {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use super::*;
|
|||
use crate::backend::{BodyDesc, BodyId, PhysicsBackend, ShapeDesc, TrimeshDesc};
|
||||
use crate::joints::{JointId, RevoluteDesc};
|
||||
use fabrilis_core::time::SimDelta;
|
||||
use fabrilis_core::units::{Kilograms, Radians};
|
||||
use fabrilis_core::units::{Kilograms, Radians, RadiansPerSecond};
|
||||
use fabrilis_core::vec::{Quat, Vec3};
|
||||
|
||||
fn step_at(hz: u64) -> SimDelta {
|
||||
|
|
@ -82,6 +82,92 @@ fn motor_reaches_target_angle() {
|
|||
}
|
||||
}
|
||||
|
||||
/// A horizontal link hinged on Z at the origin, in a world without gravity.
|
||||
fn build_velocity_world() -> (RapierBackend, BodyId, JointId) {
|
||||
let mut world = RapierBackend::with_gravity(Vec3::ZERO);
|
||||
let base = world.add_body(BodyDesc::fixed(
|
||||
ShapeDesc::Cuboid {
|
||||
half_extents: Vec3::new(0.05, 0.05, 0.05),
|
||||
},
|
||||
Vec3::ZERO,
|
||||
));
|
||||
let link = world.add_body(BodyDesc::dynamic(
|
||||
ShapeDesc::Cuboid {
|
||||
half_extents: Vec3::new(0.5, 0.05, 0.05),
|
||||
},
|
||||
Kilograms::new(1.0),
|
||||
Vec3::new(0.5, 0.0, 0.0),
|
||||
));
|
||||
let joint = world.add_revolute(RevoluteDesc::new(
|
||||
base,
|
||||
link,
|
||||
Vec3::Z,
|
||||
Vec3::ZERO,
|
||||
Vec3::new(-0.5, 0.0, 0.0),
|
||||
));
|
||||
(world, link, joint)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn velocity_motor_spins_at_target_speed() {
|
||||
let (mut world, _link, joint) = build_velocity_world();
|
||||
world.set_motor_velocity(joint, RadiansPerSecond::new(2.0), 100.0);
|
||||
let dt = step_at(1_000);
|
||||
for _ in 0..500 {
|
||||
world.step(dt);
|
||||
}
|
||||
let spun = world.joint_angle(joint).expect("the joint exists").get();
|
||||
// Rapier's acceleration-based velocity motor needs a few dozen milliseconds
|
||||
// to reach the target, so the first half second covers a little less than
|
||||
// the ideal 2 rad/s * 0.5 s = 1 rad.
|
||||
assert!(spun > 0.9 && spun <= 1.0, "angle = {spun}");
|
||||
for _ in 0..500 {
|
||||
world.step(dt);
|
||||
}
|
||||
let later = world.joint_angle(joint).expect("the joint exists").get();
|
||||
let per_half_second = later - spun;
|
||||
assert!(
|
||||
(per_half_second - 1.0).abs() <= 0.01,
|
||||
"angle per half second = {per_half_second}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn velocity_motor_stops_at_zero_target() {
|
||||
let (mut world, _link, joint) = build_velocity_world();
|
||||
world.set_motor_velocity(joint, RadiansPerSecond::new(2.0), 100.0);
|
||||
let dt = step_at(1_000);
|
||||
for _ in 0..500 {
|
||||
world.step(dt);
|
||||
}
|
||||
let spun = world.joint_angle(joint).expect("the joint exists").get();
|
||||
world.set_motor_velocity(joint, RadiansPerSecond::ZERO, 100.0);
|
||||
for _ in 0..200 {
|
||||
world.step(dt);
|
||||
}
|
||||
let stopped = world.joint_angle(joint).expect("the joint exists").get();
|
||||
assert!(
|
||||
(stopped - spun).abs() <= 0.1,
|
||||
"angle ran on from {spun} to {stopped}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn velocity_motor_worlds_match_bitwise() {
|
||||
let (mut first, _first_link, first_joint) = build_velocity_world();
|
||||
let (mut second, _second_link, second_joint) = build_velocity_world();
|
||||
first.set_motor_velocity(first_joint, RadiansPerSecond::new(2.0), 100.0);
|
||||
second.set_motor_velocity(second_joint, RadiansPerSecond::new(2.0), 100.0);
|
||||
let dt = step_at(1_000);
|
||||
for _ in 0..500 {
|
||||
first.step(dt);
|
||||
second.step(dt);
|
||||
}
|
||||
let angle_a = first.joint_angle(first_joint).expect("the joint exists");
|
||||
let angle_b = second.joint_angle(second_joint).expect("the joint exists");
|
||||
assert_eq!(angle_a.get().to_bits(), angle_b.get().to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identical_worlds_match_bitwise() {
|
||||
let (mut first, first_body, first_joint) = build_motor_world();
|
||||
|
|
@ -111,6 +197,7 @@ fn unknown_ids_return_none() {
|
|||
assert!(world.body_transform(BodyId(u32::MAX)).is_none());
|
||||
assert!(world.joint_angle(JointId(u32::MAX)).is_none());
|
||||
world.set_motor_position(JointId(7), Radians::new(1.0), 1.0, 1.0);
|
||||
world.set_motor_velocity(JointId(7), RadiansPerSecond::new(1.0), 1.0);
|
||||
assert!(world.body_transform(BodyId(7)).is_none());
|
||||
assert!(world.joint_angle(JointId(7)).is_none());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue