From 42e6d03d7a7acf7558faa359a2e9304933387471 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 17:35:20 +0400 Subject: fix(player): resolve stairs during long frames --- crates/fparkan-runtime/src/lib.rs | 4 +- crates/fparkan-runtime/src/player_motion.rs | 75 ++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 9 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 716673e4..f7a81c7b 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -70,7 +70,9 @@ pub use mission_events::{ capture_building, capture_neutral_building, capture_neutral_warbot, MissionEventStop, MissionEventTick, MissionNotice, }; -pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep}; +pub use player_motion::{ + plan_player_motion, plan_player_motion_with_collision, PlayerMotion, PlayerMotionStep, +}; pub use player_status::{player_status, PlayerStatus}; pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons}; pub use production::{ diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index c521b8b6..877063f3 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -4,6 +4,8 @@ use crate::TerrainWorld; // shortcut: bound stair steps and drops until the native hero collider is wired. const STEP_UP: f32 = 0.85; const STEP_DOWN: f32 = 2.0; +// shortcut: sample the temporary floor probe at most 0.16 units apart until native collision is wired. +const WALK_COLLISION_INTERVAL: f32 = 0.02; /// Player controls sampled once for a simulation step. #[derive(Clone, Copy, Debug, Default)] @@ -127,6 +129,31 @@ pub fn plan_player_motion( engine: &Engine, cockpit: StartingCockpit, input: PlayerMotion, +) -> Result, EngineError> { + plan_player_steps(engine, cockpit, input, None) +} + +/// Resolves walking in short sweeps and returns the last reachable pose for this frame. +/// +/// # Errors +/// Returns an error if motion or the scene collision query fails. +pub fn plan_player_motion_with_collision( + engine: &Engine, + cockpit: StartingCockpit, + input: PlayerMotion, + mut resolve: impl FnMut(PlayerMotionStep) -> Result<(PlayerMotionStep, bool), EngineError>, +) -> Result, EngineError> { + plan_player_steps(engine, cockpit, input, Some(&mut resolve)) +} + +type ResolveMotion<'a> = + &'a mut dyn FnMut(PlayerMotionStep) -> Result<(PlayerMotionStep, bool), EngineError>; + +fn plan_player_steps( + engine: &Engine, + cockpit: StartingCockpit, + mut input: PlayerMotion, + resolve: Option>, ) -> Result, EngineError> { let (Some(mission), Ok(pose)) = ( &engine.loaded, @@ -165,14 +192,46 @@ pub fn plan_player_motion( { return Ok(None); } - plan_motion( - &mission.terrain, - pose, - f32::from_bits(mission.player_pitch_bits), - flying, - input, - ) - .map(Some) + let pitch = f32::from_bits(mission.player_pitch_bits); + let Some(resolve) = resolve else { + return plan_motion(&mission.terrain, pose, pitch, flying, input).map(Some); + }; + if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 { + return Err(EngineError::Movement("invalid player step".into())); + } + let count = if !flying && input.movement != [0.0; 2] { + // shortcut: bound movement after a long stall while the scene uses the render clock. + input.delta_seconds = input.delta_seconds.min(0.5); + (input.delta_seconds / WALK_COLLISION_INTERVAL).ceil() as u32 + } else { + 1 + } + .max(1); + input.delta_seconds /= count as f32; + let mut step = plan_motion(&mission.terrain, pose, pitch, flying, input)?; + let mut previous = pose; + input.mouse_x = 0.0; + input.mouse_y = 0.0; + for index in 0..count { + if index != 0 { + step = plan_motion( + &mission.terrain, + previous, + f32::from_bits(step.pitch_bits), + flying, + input, + )?; + } + let (resolved, blocked) = resolve(step)?; + step = resolved; + if blocked { + step.pose.position = previous.position; + step.sweep = None; + break; + } + previous = step.pose; + } + Ok(Some(step)) } fn plan_motion( -- cgit v1.2.3