Expand description
§tpt-sci-physics-rigid
A small, dependency-light rigid-body (sphere) physics world with analytic
collision resolution, implemented entirely from scratch (no wrapped physics
engine such as rapier). Bodies are spheres of arbitrary dimension (2-D or
3-D) described by Body, simulated inside a World that supports
constant gravity, axis-aligned bounding walls, and pairwise elastic
collisions with a configurable restitution coefficient and an optional
global Coulomb friction coefficient (World::set_friction).
The integrator is a semi-implicit (symplectic) Euler scheme; collisions are
resolved with the standard two-sphere normal impulse, a tangential impulse
bounded by Coulomb’s law (|friction_impulse| <= mu * |normal_impulse|),
plus a positional (Baumgarte-style) correction to keep overlapping bodies
from sinking into one another. Candidate colliding pairs are found with a
sweep-and-prune broad-phase rather than an all-pairs O(n^2) scan.
Each Body is a genuine rigid body, not just a point mass: it carries an
orientation quaternion and an angular velocity, so torques change its spin
and its orientation integrates forward under Body::spin. For a sphere the
moment of inertia is isotropic (default (2/5)·m·r²), captured by a single
scalar Body::inertia.
§Examples
use tpt_sci_physics_rigid::{Body, World, PhysicsError};
use tpt_math_linalg::tpt_math_linalg_dense::DVector;
let mut world = World::new();
let a = Body::new(
0,
DVector::from_row_slice(&[0.0_f64, 0.0]),
DVector::from_row_slice(&[1.0_f64, 0.0]),
1.0,
0.5,
)?;
world.add_body(a).unwrap();
let b = Body::new(
1,
DVector::from_row_slice(&[1.0_f64, 0.0]),
DVector::from_row_slice(&[0.0_f64, 0.0]),
1.0,
0.5,
)?;
world.add_body(b).unwrap();
let p0 = world.body(0).unwrap().position.clone();
world.step(0.5);
let p1 = world.body(0).unwrap().position.clone();
assert!((p1[0] - p0[0]).abs() > 1e-9);Structs§
- Body
- A spherical rigid body living in an
n-dimensional space (the dimension is implied by the length of itsBody::position/[Body::velocity]vectors). - World
- A small rigid-body (sphere) simulation world.
Enums§
- Physics
Error - Errors produced by the physics world and body construction.
Functions§
- quat_
mul - Multiply two quaternions
a ⊗ b(Hamilton product), both[w, x, y, z]. - quat_
normalize - Normalise a quaternion to unit length, returning
[w, x, y, z]. - quat_
to_ matrix - Convert a unit quaternion
[w, x, y, z]to a 3×3 rotation matrix.