aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 19:58:16 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 19:58:16 +0300
commit07a90f035ebd935ad9705a5f8a8916638a4a8f4f (patch)
tree64d8bb4c8c6936ca3f06b53ad04900be02adfef6 /crates/fparkan-runtime/src
parent23ec106748d4641a9dfd809b56029391daddf6ba (diff)
downloadfparkan-07a90f035ebd935ad9705a5f8a8916638a4a8f4f.tar.xz
fparkan-07a90f035ebd935ad9705a5f8a8916638a4a8f4f.zip
fix(movement): carry unfinished hero turns across ticks
Use the angular limit in Control profile word 11 and preserve the unconsumed phase. The original Control read and write dispatch and orientation update delivered 0.5022847 then 0.4401929 radians at two 10 ms samples. The same steps changed the view in a Vulkan window from the stock Firing Range start.
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/boarding.rs4
-rw-r--r--crates/fparkan-runtime/src/hero_look.rs75
-rw-r--r--crates/fparkan-runtime/src/player_motion.rs38
3 files changed, 84 insertions, 33 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs
index e7ad46d2..53ce8c81 100644
--- a/crates/fparkan-runtime/src/boarding.rs
+++ b/crates/fparkan-runtime/src/boarding.rs
@@ -143,7 +143,7 @@ pub fn board_friendly_warbot(
});
mission.player_pitch_bits = 0;
mission.hero_walk_velocity = [0.0; 2];
- mission.hero_look.filtered_x = 0.0;
+ mission.hero_look.reset();
}
Ok(Some(cockpit))
}
@@ -225,7 +225,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>,
mission.boarding = None;
mission.player_pitch_bits = 0;
mission.hero_walk_velocity = [0.0; 2];
- mission.hero_look.filtered_x = 0.0;
+ mission.hero_look.reset();
Ok(Some(hero))
}
diff --git a/crates/fparkan-runtime/src/hero_look.rs b/crates/fparkan-runtime/src/hero_look.rs
index 0c96a65d..57bd62b1 100644
--- a/crates/fparkan-runtime/src/hero_look.rs
+++ b/crates/fparkan-runtime/src/hero_look.rs
@@ -1,11 +1,13 @@
use fparkan_path::{normalize_relative, PathPolicy};
use fparkan_vfs::Vfs;
+use std::f32::consts::TAU;
#[derive(Clone, Copy, Debug)]
pub(super) struct HeroLook {
turns: f32,
sensitivity: f32,
- pub(super) filtered_x: f32,
+ filtered_x: f32,
+ phase: f32,
}
impl HeroLook {
@@ -26,24 +28,52 @@ impl HeroLook {
.and_then(|bytes| mouse_sensitivity(&String::from_utf8_lossy(bytes)))
.unwrap_or(1.0),
filtered_x: 0.0,
+ phase: 0.5,
}
}
- /// Returns the filter state and the requested change in world yaw.
- pub(super) fn sample(self, dx: f32, dy: f32) -> (f32, f32) {
- let filtered = if dx == 0.0 && dy == 0.0 {
+ pub(super) fn reset(&mut self) {
+ self.filtered_x = 0.0;
+ self.phase = 0.5;
+ }
+
+ pub(super) fn has_state(self) -> bool {
+ self.filtered_x != 0.0 || self.phase != 0.5
+ }
+
+ /// Samples the input once and returns the yaw delivered by this tick.
+ pub(super) fn advance(&mut self, dx: f32, dy: f32, max_angle: f32) -> f32 {
+ self.filtered_x = if dx == 0.0 && dy == 0.0 {
0.0
} else {
dx.clamp(i32::MIN as f32, i32::MAX as f32) * self.sensitivity * 0.95
+ self.filtered_x * 0.05
};
- let scaled = filtered * 0.006;
+ let scaled = self.filtered_x * 0.006;
let axis = if scaled.abs() > 0.999 {
scaled.signum()
} else {
scaled
};
- (filtered, -(axis * self.turns) * std::f32::consts::TAU)
+ if axis != 0.0 {
+ let phase = self.phase + axis * self.turns;
+ self.phase = if phase >= 1.0 {
+ phase - 1.0
+ } else if phase <= 0.0 {
+ phase + 1.0
+ } else {
+ phase
+ };
+ }
+
+ let requested = -(self.phase - 0.5) * TAU;
+ let delivered = requested.clamp(-max_angle, max_angle);
+ self.phase = if requested == delivered {
+ 0.5
+ } else {
+ (self.phase + delivered * (1.0 / TAU)).rem_euclid(1.0)
+ };
+ delivered
}
}
@@ -64,7 +94,7 @@ fn horizontal_binding(table: &str) -> Option<f32> {
}
}
let turns = words.next()?.parse::<f32>().ok()?;
- (turns.is_finite() && words.nth(1)?.eq_ignore_ascii_case("MAN_WRAP")).then_some(turns)
+ (turns.abs() <= 1.0 && words.nth(1)?.eq_ignore_ascii_case("MAN_WRAP")).then_some(turns)
})
}
@@ -106,15 +136,32 @@ MOUSE SCAN_NULL SCAN_MOUSE_X 1 CICLS_UNKNOWN MCMD_ANGLE_Z 0.20 0 MAN_WRAP 0.0 0
turns: 0.15,
sensitivity: 1.0,
filtered_x: 0.0,
+ phase: 0.5,
};
for (dx, expected) in [(100.0, 0.0855), (-100.0, -0.0855), (250.0, 0.15)] {
- let (_, yaw) = look.sample(dx, 0.0);
- assert!((yaw / -std::f32::consts::TAU - expected).abs() < 1e-6);
+ look.reset();
+ assert!((look.advance(dx, 0.0, TAU) / -TAU - expected).abs() < 1e-6);
}
- look.filtered_x = look.sample(100.0, 0.0).0;
- let (tail, yaw) = look.sample(0.0, 1.0);
- assert_eq!(tail, 4.75);
- assert!((yaw / -std::f32::consts::TAU - 0.004275).abs() < 1e-6);
- assert_eq!(look.sample(0.0, 0.0), (0.0, 0.0));
+ look.reset();
+ look.advance(100.0, 0.0, TAU);
+ assert!((look.advance(0.0, 1.0, TAU) / -TAU - 0.004275).abs() < 1e-6);
+ assert_eq!(look.filtered_x, 4.75);
+ assert_eq!(look.advance(0.0, 0.0, TAU), 0.0);
+ assert!(!look.has_state());
+ }
+
+ #[test]
+ fn hero_finishes_a_capped_turn_on_the_next_tick_without_mouse_input() {
+ let mut look = HeroLook {
+ turns: 0.15,
+ sensitivity: 1.0,
+ filtered_x: 0.0,
+ phase: 0.5,
+ };
+ let max_angle = 50.22849 * 0.01;
+ assert!((look.advance(250.0, 0.0, max_angle) + 0.5022847).abs() < 1e-6);
+ assert!((look.phase - 0.5700589).abs() < 1e-6);
+ assert!((look.advance(0.0, 0.0, max_angle) + 0.44019294).abs() < 1e-6);
+ assert!(!look.has_state());
}
}
diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs
index 8571b899..b2f91270 100644
--- a/crates/fparkan-runtime/src/player_motion.rs
+++ b/crates/fparkan-runtime/src/player_motion.rs
@@ -1,4 +1,5 @@
use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState};
+use crate::hero_look::HeroLook;
use crate::manual_flight::{FlightDrive, FlightProfile};
use crate::{hero_walk::WalkingDrive, TerrainWorld};
use fparkan_resource::CachedResourceRepository;
@@ -66,7 +67,7 @@ pub struct PlayerMotionStep {
pub(super) pitch_bits: u32,
flight_velocity: Option<[f32; 3]>,
hero_walk_velocity: Option<[f32; 2]>,
- hero_filtered_x: Option<f32>,
+ hero_look: Option<HeroLook>,
max_floor_clearance: f32,
previous_terrain_height: Option<f32>,
terrain_height: Option<f32>,
@@ -222,7 +223,7 @@ mod tests {
pitch_bits: 0,
flight_velocity: None,
hero_walk_velocity: None,
- hero_filtered_x: None,
+ hero_look: None,
max_floor_clearance: FLOOR_CLEARANCE,
previous_terrain_height: None,
terrain_height: Some(11.5),
@@ -262,7 +263,7 @@ mod tests {
pitch_bits: 0,
flight_velocity: None,
hero_walk_velocity: None,
- hero_filtered_x: None,
+ hero_look: None,
max_floor_clearance: 1.25,
previous_terrain_height: Some(10.0),
terrain_height: Some(10.0),
@@ -287,7 +288,7 @@ mod tests {
pitch_bits: 0,
flight_velocity: None,
hero_walk_velocity: None,
- hero_filtered_x: None,
+ hero_look: None,
max_floor_clearance: FLOOR_CLEARANCE,
previous_terrain_height: None,
terrain_height: None,
@@ -458,7 +459,7 @@ fn plan_player_steps(
&& flight.is_none_or(|drive| input.climb == 0.0 && drive.velocity == [0.0; 3])
&& input.mouse_x == 0.0
&& input.mouse_y == 0.0
- && (!hero_control || mission.hero_look.filtered_x == 0.0)
+ && (!hero_control || !mission.hero_look.has_state())
{
return Ok(None);
}
@@ -470,12 +471,17 @@ fn plan_player_steps(
{
return Err(EngineError::Movement("invalid player step".into()));
}
- let hero_look = hero_control.then(|| mission.hero_look.sample(input.mouse_x, input.mouse_y));
- // shortcut: mounted chassis still use a constant mouse gain.
- let mut yaw_delta = hero_look.map_or_else(
- || -input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003,
- |(_, yaw)| yaw,
- );
+ let mut hero_look = hero_control.then_some(mission.hero_look);
+ let mut yaw_delta = if let Some(look) = hero_look.as_mut() {
+ let max_angle = hero_profile
+ .map(|words| f32::from_bits(words[11]))
+ .filter(|speed| speed.is_finite() && *speed >= 0.0)
+ .map_or(f32::INFINITY, |speed| speed * input.delta_seconds);
+ look.advance(input.mouse_x, input.mouse_y, max_angle)
+ } else {
+ // shortcut: mounted chassis still use a constant mouse gain.
+ -input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003
+ };
let speeds = if boarded.is_some() && !flying {
crate::manual_chassis::speed_limits(mission, cockpit.handle)
.unwrap_or([WALK_SPEED_FALLBACK; 2])
@@ -526,7 +532,7 @@ fn plan_player_steps(
)?;
step.max_floor_clearance = max_floor_clearance;
step.hero_walk_velocity = walk.map(|(velocity, _)| velocity);
- step.hero_filtered_x = hero_look.map(|(filtered_x, _)| filtered_x);
+ step.hero_look = hero_look;
match resolve.as_mut() {
Some(resolve) => resolve(step),
None => Ok((step, false)),
@@ -555,7 +561,6 @@ fn plan_player_steps(
return Ok(Some(step));
}
yaw_delta = 0.0;
- input.mouse_x = 0.0;
input.mouse_y = 0.0;
pitch = f32::from_bits(step.pitch_bits);
previous = step.pose;
@@ -578,8 +583,8 @@ pub(super) fn commit_motion_state(
mission.hero_walk_velocity = if blocked { [0.0; 2] } else { velocity };
}
if mission.boarding.is_none() {
- if let Some(filtered_x) = step.hero_filtered_x {
- mission.hero_look.filtered_x = filtered_x;
+ if let Some(look) = step.hero_look {
+ mission.hero_look = look;
}
}
}
@@ -614,7 +619,6 @@ fn plan_motion(
.movement
.into_iter()
.chain([
- input.mouse_x,
input.mouse_y,
input.climb,
input.delta_seconds,
@@ -702,7 +706,7 @@ fn plan_motion(
pitch_bits: pitch.to_bits(),
flight_velocity,
hero_walk_velocity: None,
- hero_filtered_x: None,
+ hero_look: None,
max_floor_clearance: FLOOR_CLEARANCE,
previous_terrain_height,
terrain_height,