Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(traversal): drive the Aravath motor from the tick sim
Ground, air, crouch, sprint, and roll now step once per 60 Hz tick.
Roll i-frames are ticks 5 through 19. The demo replay golden matches
the new displacement.

Co-authored-by: Andrew Brown <lazyxeon@users.noreply.github.com>
  • Loading branch information
cursoragent and lazyxeon committed Oct 16, 2026
commit d559784cd4d647229d20190ebb43742d45146ad1
236 changes: 135 additions & 101 deletions aravath_runtime/src/motor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Player motor. Stamina lives here, not on `astraweave-gameplay::Stats`.

use astraweave_physics::traversal::{
self, ProbeFrame, TraversalBody, TraversalInput, TraversalMode, TICK_DT,
};
use astraweave_physics::PhysicsWorld;
use glam::Vec3;

Expand Down Expand Up @@ -39,6 +42,8 @@ pub struct Motor {
pub walking: bool,
pub grounded: bool,
pub invulnerable: bool,
body: TraversalBody,
synced: bool,
}

impl Default for Motor {
Expand All @@ -55,6 +60,8 @@ impl Default for Motor {
walking: false,
grounded: true,
invulnerable: false,
body: TraversalBody::default(),
synced: false,
}
}
}
Expand Down Expand Up @@ -83,7 +90,7 @@ impl Motor {
}
}

/// Advance one fixed step. Returns the horizontal wish passed to the character controller.
/// Advance one fixed 60 Hz tick. `dt` is ignored; callers pass [`TICK_DT`].
pub fn tick(
&mut self,
physics: &mut PhysicsWorld,
Expand All @@ -92,133 +99,146 @@ impl Motor {
input: MotorInput,
dt: f32,
) -> Vec3 {
let _ = dt;
if input.fly_toggle {
self.mode = if self.mode == MotorMode::Fly {
MotorMode::Locomotion
} else {
MotorMode::Fly
};
self.roll_time = 0.0;
self.synced = false;
}

if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.gravity_scale = if self.mode == MotorMode::Fly {
0.0
} else {
1.0
};
if self.mode == MotorMode::Fly {
return self.tick_fly(physics, player, camera_yaw, input);
}

let grounded = on_ground(physics, player.body);
self.grounded = grounded;
if grounded && self.mode != MotorMode::Roll {
self.jumps_used = 0;
}
self.sync_feet(physics, player);
let probe = ground_probe(physics, player.body);
let (move_x, move_y) = if input.feet_locked {
(0.0, 0.0)
} else {
(input.move_x, input.move_y)
};
traversal::step(
&mut self.body,
TraversalInput {
move_x,
move_y,
yaw: camera_yaw,
jump: input.jump,
roll: input.roll,
sprint: input.sprint,
crouch: input.crouch,
walk: input.walk,
},
&probe,
0,
);
self.publish(physics, player);
Vec3::new(self.body.vel.x, 0.0, self.body.vel.z)
}

if self.mode == MotorMode::Roll {
self.roll_time += dt;
self.invulnerable =
self.roll_time >= ROLL_IFRAME_START && self.roll_time <= ROLL_IFRAME_END;
let wish = self.roll_dir * ROLL_SPEED;
if self.roll_time >= ROLL_DURATION {
self.mode = MotorMode::Locomotion;
self.invulnerable = false;
self.roll_time = 0.0;
}
player.facing_yaw = crate::detmath::atan2(wish.z, wish.x);
physics.control_character(player.body, wish, dt, false);
return wish;
fn tick_fly(
&mut self,
physics: &mut PhysicsWorld,
player: &mut Player,
camera_yaw: f32,
input: MotorInput,
) -> Vec3 {
if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.gravity_scale = 0.0;
ctrl.vertical_velocity = if input.jump {
FLY_SPEED
} else if input.crouch {
-FLY_SPEED
} else {
0.0
};
}

self.invulnerable = false;
self.crouching = input.crouch && self.mode != MotorMode::Fly;
let moving = input.move_x.abs() + input.move_y.abs() > 0.05;
self.sprinting = input.sprint && moving && self.stamina > 0.0 && !self.crouching;
self.walking = input.walk && moving && !self.sprinting && !self.crouching;

if input.roll && self.mode != MotorMode::Fly && self.stamina >= ROLL_COST {
self.stamina -= ROLL_COST;
self.mode = MotorMode::Roll;
self.roll_time = 0.0;
let (forward, right) = player::flat_basis(camera_yaw);
let mut dir = forward * input.move_y + right * input.move_x;
if dir.length_squared() < 1e-4 {
dir = player::flat_basis(player.facing_yaw).0;
}
self.roll_dir = Vec3::new(dir.x, 0.0, dir.z).normalize_or_zero();
if self.roll_dir.length_squared() < 1e-4 {
self.roll_dir = Vec3::X;
}
}

if input.jump && self.mode != MotorMode::Roll {
if self.mode == MotorMode::Fly {
if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.vertical_velocity = FLY_SPEED;
}
} else if grounded && self.jumps_used == 0 {
physics.jump(player.body, JUMP_HEIGHT);
self.jumps_used = 1;
} else if self.jumps_used == 1 {
if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.vertical_velocity = crate::detmath::sqrt(2.0 * 9.81 * JUMP_HEIGHT);
}
self.jumps_used = 2;
}
} else if self.mode == MotorMode::Fly && input.crouch {
if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.vertical_velocity = -FLY_SPEED;
}
} else if self.mode == MotorMode::Fly {
if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.vertical_velocity = 0.0;
}
}

let speed = if self.mode == MotorMode::Fly {
FLY_SPEED
} else if self.crouching {
CROUCH_SPEED
} else if self.sprinting {
SPRINT_SPEED
} else if self.walking {
WALK_SPEED
} else if moving {
RUN_SPEED
let (forward, right) = player::flat_basis(camera_yaw);
let mut wish = if moving {
player::wish_from_axes(forward, right, input.move_x, input.move_y, FLY_SPEED)
} else {
0.0
Vec3::ZERO
};
let (forward, right) = player::flat_basis(camera_yaw);
let mut wish = player::wish_from_axes(forward, right, input.move_x, input.move_y, speed);
if !moving {
if input.feet_locked {
wish = Vec3::ZERO;
}
if input.feet_locked && self.mode != MotorMode::Fly {
wish = Vec3::ZERO;
}
if !grounded && self.mode == MotorMode::Locomotion {
wish *= 0.4;
}
if wish.length_squared() > 1e-6 {
player.facing_yaw = crate::detmath::atan2(wish.z, wish.x);
}
self.grounded = false;
self.crouching = false;
self.sprinting = false;
self.invulnerable = false;
physics.control_character(player.body, wish, TICK_DT, false);
wish
}

if self.sprinting {
self.stamina = (self.stamina - SPRINT_DRAIN * dt).max(0.0);
} else if self.mode != MotorMode::Roll && grounded {
self.stamina = (self.stamina + STAMINA_REGEN * dt).min(self.stamina_max);
fn sync_feet(&mut self, physics: &PhysicsWorld, player: &Player) {
if self.synced {
return;
}
let center = player.translation(physics).unwrap_or(Vec3::ZERO);
let half = capsule_half(physics, player.body);
self.body.pos = Vec3::new(center.x, center.y - half, center.z);
self.body.stamina = self.stamina;
self.body.stamina_max = self.stamina_max;
self.body.facing_yaw = player.facing_yaw;
self.synced = true;
}

physics.control_character(player.body, wish, dt, false);
wish
fn publish(&mut self, physics: &mut PhysicsWorld, player: &mut Player) {
self.stamina = self.body.stamina;
self.jumps_used = self.body.jumps_used;
self.invulnerable = self.body.invulnerable;
self.crouching = self.body.crouching;
self.sprinting = self.body.sprinting;
self.walking = self.body.walking;
self.grounded = self.body.mode != TraversalMode::Air;
self.roll_dir = self.body.roll_dir;
self.roll_time = f32::from(self.body.roll_tick);
self.mode = if self.body.mode == TraversalMode::Roll {
MotorMode::Roll
} else {
MotorMode::Locomotion
};
player.facing_yaw = self.body.facing_yaw;
let half = capsule_half(physics, player.body);
let center = self.body.pos + Vec3::Y * half;
physics.place_character(player.body, center);
Comment on lines +265 to +267

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Route locomotion through collision resolution

During ordinary non-fly movement, TraversalBody advances its position and this call teleports the kinematic capsule directly there. Unlike the previous control_character path, place_character performs no forward cast or slide, while ground_probe only checks downward support. Consequently, walking or rolling toward walls and other colliders can move the player into or through them; the traversal displacement must be resolved against the world before publishing the capsule position.

Useful? React with 👍 / 👎.

if let Some(ctrl) = physics.char_map.get_mut(&player.body) {
ctrl.vertical_velocity = self.body.vel.y;
ctrl.gravity_scale = 1.0;
ctrl.time_since_grounded = if self.grounded { 0.0 } else { TICK_DT };
}
}
}

fn on_ground(physics: &PhysicsWorld, body: u64) -> bool {
fn capsule_half(physics: &PhysicsWorld, body: u64) -> f32 {
physics
.char_map
.get(&body)
.is_some_and(|c| c.time_since_grounded <= 0.02)
.map(|ctrl| ctrl.height * 0.5 + ctrl.radius)
.unwrap_or(1.0)
}

fn ground_probe(physics: &PhysicsWorld, body: u64) -> ProbeFrame {
match physics.character_ground(body) {
Some((ground_y, normal_y)) => ProbeFrame {
support: normal_y > 0.35,
ground_y,
normal_y,
},
None => ProbeFrame {
support: false,
ground_y: 0.0,
normal_y: 1.0,
},
}
}

#[cfg(test)]
Expand All @@ -231,7 +251,9 @@ mod tests {
let mut physics = PhysicsWorld::new(Vec3::new(0.0, -9.81, 0.0));
let _ = physics.create_ground_plane(Vec3::new(20.0, 0.5, 20.0), 1.0);
physics.step();
let player = Player::spawn(&mut physics, Vec3::new(0.0, 2.0, 0.0));
let player = Player::spawn(&mut physics, Vec3::new(0.0, 1.25, 0.0));
physics.place_character(player.body, Vec3::new(0.0, 1.25, 0.0));
physics.step();
(physics, player)
}

Expand All @@ -252,7 +274,15 @@ mod tests {
);
assert_eq!(motor.mode, MotorMode::Roll);
assert!(!motor.invulnerable);
motor.tick(&mut physics, &mut player, 0.0, MotorInput::default(), 0.1);
for _ in 0..5 {
motor.tick(
&mut physics,
&mut player,
0.0,
MotorInput::default(),
TICK_DT,
);
}
assert!(motor.invulnerable);
assert_eq!(motor.apply_damage_multiplier(false), 0.0);

Expand All @@ -271,7 +301,11 @@ mod tests {
0.016,
);
assert_ne!(dry.mode, MotorMode::Roll);
assert!((dry.stamina - 10.0).abs() < 0.01);
assert!(
dry.stamina + 1e-3 >= 10.0,
"a refused roll must not spend stamina, got {}",
dry.stamina
);
}

#[test]
Expand Down
41 changes: 41 additions & 0 deletions astraweave-physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,47 @@ impl PhysicsWorld {
}
}

/// Move a kinematic capsule to `center` and keep the next physics step there.
pub fn place_character(&mut self, id: BodyId, center: Vec3) {
let Some(h) = self.handle_of(id) else {
return;
};
let Some(rb) = self.bodies.get(h) else {
return;
};
let mut iso = *rb.position();
iso.translation.x = center.x;
iso.translation.y = center.y;
iso.translation.z = center.z;
if let Some(rbmut) = self.bodies.get_mut(h) {
rbmut.set_translation(vector![center.x, center.y, center.z], true);
rbmut.set_next_kinematic_position(iso);
}
}

/// Downward probe that ignores the character collider.
/// Returns `(ground_y, normal_y)` when something is under the capsule center.
pub fn character_ground(&self, id: BodyId) -> Option<(f32, f32)> {
let h = self.handle_of(id)?;
let rb = self.bodies.get(h)?;
let origin = *rb.translation();
let ray = rapier3d::prelude::Ray::new(
point![origin.x, origin.y, origin.z],
vector![0.0, -1.0, 0.0],
);
let filter = QueryFilter::default().exclude_rigid_body(h);
let (_, hit) = self.query_pipeline.cast_ray_and_get_normal(
&self.bodies,
&self.colliders,
&ray,
6.0,
true,
filter,
)?;
let ground_y = origin.y - hit.time_of_impact;
Some((ground_y, hit.normal.y))
}

pub fn enable_ccd(&mut self, id: BodyId) {
if let Some(h) = self.handle_of(id) {
if let Some(rb) = self.bodies.get_mut(h) {
Expand Down
Loading