physics/src/character.rs#
Source: lib/physics/src/character.rs
character -- the kinematic character controller: an upright capsule that walks, runs, jumps and crouches
on the world's queries. Steps up to a step height, ramps up to a slope limit (steeper surfaces are walls
it slides down), sliding along walls and into corners, ground snapping on the way down ramps and steps,
steering in the air. The feel hangs on CharacterParams; the defaults suit a fast cartoon arena shooter.
One step is a function of (state, input, world, dt) alone: f64 with + - * /, the correctly rounded
square root and rt's portable cosine (once, in new), no fused multiply-add, no unordered iteration. So
it gives the same bits on every target, and a client predicts exactly what the server computes (G7e).
The step derives the ground itself; position, velocity and crouched are all the state it reads.
A step (docs/PHYSICS.md, "Der Character-Controller"):
1. push out of anything the capsule overlaps (only after a teleport or a body moved into it: the
controller's own moves are casts and never end inside anything)
2. find the ground: a short cast down; it carries the figure when the normal is walkable (y >=
cos(max slope)), or when the contact is on the edge of a walkable surface (a step, a ledge: a ray
down just beyond the contact finds that surface), and the figure does not move away from it
3. crouch (on the ground the head comes down, in the air the legs come up) or stand up (only where a
cast of the crouched capsule finds room for the standing one)
4. jump (sets the vertical speed); on the ground with input the velocity moves towards the wish
velocity along the ground (accel), without input Quake's friction brakes it; in the air Quake's
acceleration (the speed along the wish direction grows up to the wish speed, scaled by air_control)
5. move: collide and slide with up to four casts, stopping `skin` before each contact; Quake 3's clip
planes (two planes: along their crease, three: stop), so corners neither jitter nor trap; on the
ground steep surfaces count as vertical walls (no climbing them by pushing), a walkable one turns
the velocity onto it at the same speed; blocked on the ground, a step: up, forward, down onto
walkable ground, taken when it gets farther
6. on the ground at the start and no jump: snap down onto ground up to step_height below (stairs and
ramps downwards); in the air: land on ground within the probe distance
Gravity in the air uses the mean velocity of the tick (v - g dt / 2) for the move, so the height after k
free ticks is y0 + v0 t - g t^2 / 2 at t = k dt (up to rounding), whatever the tick rate.
/// at most so many walls one step reports in [`StepEvents::walls`]
MAX_WALL_TOUCHES: usize = 4
struct CharacterParamsHow a figure moves: its capsule, speeds, acceleration, friction, gravity, jump, steps and slopes. Metres, seconds, degrees. [CharacterParams::default] is tuned for a fast cartoon arena shooter (docs/PHYSICS.md lists the values and why); [CharacterController::new] limits values that make no sense.radius: f64-- the capsule's radius (default 0.4 m)height: f64-- standing height, feet to the top of the head (1.8 m), at least twice the radiuscrouch_height: f64-- crouched height (1.2 m), between twice the radius andheightwalk_speed: f64-- wish speed withoutrun(5 m/s)run_speed: f64-- wish speed withrun(9 m/s)crouch_speed: f64-- wish speed while crouched (3 m/s)accel: f64-- ground acceleration (12 per second): with input the velocity moves towards the wish velocity (wish direction along the ground times wish speed) by up toacceltimes the wish speed per second; 0 to full speed in 1/12 s, sliding along a wall at the wish velocity's part along itfriction: f64-- ground friction without input (6 per second, Quake's): each second the speed drops byfrictiontimes max(speed,stop_speed); from run speed to a stop in about 0.4 sstop_speed: f64-- below this speed friction brakes as if the figure moved this fast, so it stops (2.5 m/s)air_control: f64-- acceleration in the air as a fraction ofaccel(0.3; 0: no steering in the air), Quake's model: the speed along the wish direction grows by up to air_control times accel times the wish speed per second, not beyond the wish speed; steering at an angle to the velocity can gain speed (strafe jumping) up tomax_speedgravity: f64-- downwards (20 m/s², about twice the earth's: short, snappy jumps)jump_height: f64-- height of a jump's apex above the take-off (1.2 m: onto a crate)step_height: f64-- the highest step the figure walks up and the deepest it snaps down to (0.4 m)max_slope_deg: f64-- the steepest walkable slope (45 degrees); steeper surfaces are walls it slides downskin: f64-- the gap kept between the capsule and everything (0.005 m)max_speed: f64-- the horizontal speed never gets above this (30 m/s)max_fall_speed: f64-- falling never gets faster than this (50 m/s)
struct CharacterStateWhat a figure is: all a step reads isposition,velocityandcrouched;grounded,ground_normalandground_bodyare what the last step found (a step finds them again).position: Vec3-- the feet: the lowest point of the capsule (m)velocity: Vec3-- m/s; on the ground along the groundcrouched: bool-- the capsule has the crouched heightgrounded: bool-- stands on walkable ground (output)ground_normal: Vec3-- the normal of that ground, [Vec3::Y] in the air (output)ground_body: Option<BodyId>-- the body the figure stands on (output)
impl CharacterState
const fn new(position: Vec3) -> CharacterStatestanding atposition(the feet), not moving, in the air until the first step finds groundstruct CharacterInputWhat the player wants in one tickwish: Vec3-- the direction to move in, in world space; y is ignored. Length 1 moves at the full wish speed, shorter moves slower (a stick), longer counts as 1. From a yaw:math::sincos(rt's portable sine and cosine), never std's, which differs between targets.run: bool-- the run speed instead of the walk speedjump: bool-- jump when on the ground (held: again on every landing)crouch: bool-- crouch while held; on release stand up as soon as there is room
struct Touchone contact of a step with a wallbody: BodyId-- the bodypoint: Vec3-- the contact point on its surfacenormal: Vec3-- the surface normal towards the figuretriangle: u32-- the triangle of a mesh, or [crate::NO_TRIANGLE]
struct StepEventsWhat happened in one steplanded: bool-- was in the air (after a jump of this step too) and stands at the endimpact_speed: f64-- the speed into the ground at the landing (m/s, for sounds and damage)jumped: bool-- jumped at the start of this stepleft_ground: bool-- stood at the start, did not jump and is in the air at the end (walked off a ledge)hit_ceiling: bool-- touched a surface facing down (the normal's y at most -cos(max slope))walls: [Option<Touch>; MAX_WALL_TOUCHES]-- the walls touched, in order, each body and triangle once, at most [MAX_WALL_TOUCHES]stepped_up: f64-- how far a step lifted the figure (m)snapped_down: f64-- how far ground snapping lowered it (m)stand_blocked: bool-- wanted to stand up and had no roompushed_out: f64-- how far it was pushed out of an overlap at the start (m)casts: u32-- the casts this step made (for statistics)
impl StepEvents
fn walls(&self) -> impl Iterator<Item = &Touch> + '_the walls touchedfn wall_count(&self) -> usizehow many walls were touchedstruct CharacterControllerThe controller: the parameters checked and the values derived from them. One controller serves any number of figures; [CharacterController::step] takes&self.
impl CharacterController
fn new(params: CharacterParams) -> CharacterControllera controller withparams, limited to what makes sense: radius at least 1 cm, heights at least twice the radius (crouched not above standing), skin between 0.1 mm and a quarter of the radius, slope 0 to 89 degrees, no negative speeds; a value that is not finite takes the defaultfn params(&self) -> &CharacterParamsthe parameters as the controller uses themfn jump_speed(&self) -> f64the vertical speed a jump starts with: sqrt(2 gravity jump_height)fn walkable_y(&self) -> f64cos(max_slope_deg): a normal with at least this y is walkablefn walkable(&self, normal: Vec3) -> boolcan a figure stand on a surface with this unit normalfn height(&self, crouched: bool) -> f64the capsule's total heightfn capsule(&self, crouched: bool) -> Shapethe capsule (upright, its centre half the height above the feet)fn capsule_iso(&self, state: &CharacterState) -> Isowhere the capsule ofstateis: for [World::set_iso] when the figure is a body of the worldfn step(&self, state: &CharacterState, world: &World, filter: &Filter, input: &CharacterInput, dt: f64) -> (CharacterState, StepEvents)One tick: the state afterdtseconds ofinputamong the bodies ofworldthatfilterlets through (leave the figure's own body out:Filter::ALL.excluding(id)), and what happened. The same bits on every target for the same arguments. Adtthat is not positive and finite, or a position that is not finite, changes nothing; a velocity that is not finite counts as 0.