aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/player_gait.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 13:00:09 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 13:00:09 +0300
commit514136ca97191672ecd24173d1836cdc4d26493b (patch)
tree2f7f21d8d7e6182ec283636e6c1d9a340fba62eb /crates/fparkan-runtime/src/player_gait.rs
parent5ad056292d91bd6a38299c5d890a975a435c8422 (diff)
downloadfparkan-514136ca97191672ecd24173d1836cdc4d26493b.tar.xz
fparkan-514136ca97191672ecd24173d1836cdc4d26493b.zip
feat(player): bob the cockpit view when walking
Use the 0.45 second vertical cycle observed in the GOG hero camera. The stock Firing Range window shows lift on an accepted walk and settles on release. The body and muzzle still use their own poses.
Diffstat (limited to 'crates/fparkan-runtime/src/player_gait.rs')
-rw-r--r--crates/fparkan-runtime/src/player_gait.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/player_gait.rs b/crates/fparkan-runtime/src/player_gait.rs
new file mode 100644
index 00000000..3b293d9a
--- /dev/null
+++ b/crates/fparkan-runtime/src/player_gait.rs
@@ -0,0 +1,78 @@
+//! First-person motion of the walking hero's camera.
+
+use crate::{Engine, LoadedMissionState, StartingCockpit};
+use fparkan_world::{transform_state, World};
+
+const WALK_CYCLE_MS: f32 = 448.0;
+const CAMERA_LIFT: f32 = 0.15;
+
+#[derive(Default)]
+pub(super) struct PlayerGait {
+ previous: Option<(u32, [u32; 2])>,
+ phase: f32,
+ lift: f32,
+}
+
+impl PlayerGait {
+ fn advance(&mut self, clock_ms: u32, xy: [u32; 2]) {
+ let Some((previous_ms, previous_xy)) = self.previous.replace((clock_ms, xy)) else {
+ return;
+ };
+ let elapsed_ms = clock_ms.wrapping_sub(previous_ms).min(500) as f32;
+ if previous_xy == xy {
+ let decay = (1.0 - elapsed_ms / (WALK_CYCLE_MS / 4.0)).clamp(0.0, 1.0);
+ self.lift *= decay;
+ if self.lift == 0.0 {
+ self.phase = 0.0;
+ }
+ return;
+ }
+ // shortcut: a sine matches the observed main component of the walk;
+ // the native camera track and its response to movement speed are open.
+ self.phase = (self.phase + elapsed_ms / WALK_CYCLE_MS).fract();
+ self.lift = CAMERA_LIFT * (self.phase * std::f32::consts::TAU).sin();
+ }
+}
+
+pub(super) fn advance(mission: &mut LoadedMissionState, world: &World, now: u32) {
+ let pose = mission
+ .local_hero
+ .filter(|_| mission.boarding.is_none())
+ .and_then(|hero| transform_state(world, hero).ok());
+ if let Some(pose) = pose {
+ let [x, y, _] = pose.position;
+ mission.player_gait.advance(now, [x, y]);
+ } else {
+ mission.player_gait = PlayerGait::default();
+ }
+}
+
+/// Vertical movement of the local hero's camera at the latest simulation tick.
+pub fn player_walk_camera_lift(engine: &Engine, cockpit: StartingCockpit) -> f32 {
+ engine
+ .loaded
+ .as_ref()
+ .filter(|mission| mission.local_hero == Some(cockpit.handle) && mission.boarding.is_none())
+ .map_or(0.0, |mission| mission.player_gait.lift)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn camera_oscillates_only_after_walk_displaces_the_hero() {
+ let mut gait = PlayerGait::default();
+ gait.advance(0, [0, 0]);
+ gait.advance(112, [0, 0]);
+ assert_eq!(gait.lift, 0.0);
+ gait.advance(224, [1, 0]);
+ assert_eq!(gait.lift, CAMERA_LIFT);
+ gait.advance(448, [3, 0]);
+ assert_eq!(gait.lift, -CAMERA_LIFT);
+ gait.advance(504, [3, 0]);
+ assert_eq!(gait.lift, -CAMERA_LIFT * 0.5);
+ gait.advance(616, [3, 0]);
+ assert_eq!(gait.lift, 0.0);
+ }
+}