diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 20:21:49 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 20:21:49 +0300 |
| commit | 78a63745f471c028c0ed67f133de9034e901c905 (patch) | |
| tree | 4f9f382e15dfe642505699e73d2526ce864680ae /crates/fparkan-runtime | |
| parent | 75dcc70912d4e6feb14d9370452914ff826849f7 (diff) | |
| download | fparkan-78a63745f471c028c0ed67f133de9034e901c905.tar.xz fparkan-78a63745f471c028c0ed67f133de9034e901c905.zip | |
feat(camera): free look with left Shift from the cockpit
Route shifted mouse samples to the independent Control camera phases, apply yaw then pitch at the existing cockpit point, and recenter on Shift release or right click. Original World3D dispatch and Control camera calls verified wrapping, clamping, recentering, and the transform. A stock Firing Range window showed the view move and return with hull yaw and turret pitch unchanged.
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/hero_look.rs | 139 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_motion.rs | 41 |
3 files changed, 148 insertions, 39 deletions
diff --git a/crates/fparkan-runtime/src/hero_look.rs b/crates/fparkan-runtime/src/hero_look.rs index 8e15effd..d671f2b7 100644 --- a/crates/fparkan-runtime/src/hero_look.rs +++ b/crates/fparkan-runtime/src/hero_look.rs @@ -1,15 +1,17 @@ use fparkan_path::{normalize_relative, PathPolicy}; use fparkan_vfs::Vfs; -use std::f32::consts::TAU; +use std::f32::consts::{PI, TAU}; #[derive(Clone, Copy, Debug)] pub(super) struct HeroLook { yaw_turns: f32, pitch_blend: f32, + camera_gain: [f32; 2], reverse_y: bool, sensitivity: f32, filtered: [f32; 2], phase: f32, + camera_phase: [f32; 2], } impl HeroLook { @@ -25,6 +27,7 @@ impl HeroLook { Self { yaw_turns: mouse_binding( &table, + "SCAN_NULL", "SCAN_MOUSE_X", "CICLS_UNKNOWN", "MCMD_ANGLE_Z", @@ -33,31 +36,66 @@ impl HeroLook { .unwrap_or(0.15), pitch_blend: mouse_binding( &table, + "SCAN_NULL", "SCAN_MOUSE_Y", "CICLS_TURRET", "MCMD_ANGLE_Y", "MAN_NOTWRAP", ) .unwrap_or(0.25), + camera_gain: [ + mouse_binding( + &table, + "SCAN_LSHIFT", + "SCAN_MOUSE_X", + "CICLS_CAMERA", + "MCMD_ANGLE_X", + "MAN_WRAP", + ) + .unwrap_or(0.1), + mouse_binding( + &table, + "SCAN_LSHIFT", + "SCAN_MOUSE_Y", + "CICLS_CAMERA", + "MCMD_ANGLE_Y", + "MAN_NOTWRAP", + ) + .unwrap_or(0.15), + ], reverse_y: control_setting(&settings, "MOUSE_REV_Y").unwrap_or(0) != 0, sensitivity: control_setting(&settings, "MOUSE_SENS") .map_or(1.0, |value| value as f32 * 0.01), filtered: [0.0; 2], phase: 0.5, + camera_phase: [0.5; 2], } } pub(super) fn reset(&mut self) { self.filtered = [0.0; 2]; self.phase = 0.5; + self.camera_phase = [0.5; 2]; } pub(super) fn has_state(self) -> bool { - self.filtered != [0.0; 2] || self.phase != 0.5 + self.filtered != [0.0; 2] || self.phase != 0.5 || self.camera_phase != [0.5; 2] + } + + /// Local yaw and pitch relative to the direction of the sights. + pub(super) fn camera_offset(self) -> [f32; 2] { + let [x, y] = self.camera_phase; + [(0.5 - x) * TAU, (0.5 - y) * PI] } /// Returns delivered yaw in radians and the requested change in turret blend. - pub(super) fn advance(&mut self, raw: [f32; 2], max_angle: f32) -> [f32; 2] { + pub(super) fn advance( + &mut self, + raw: [f32; 2], + max_angle: f32, + look_around: bool, + center_view: bool, + ) -> [f32; 2] { self.filtered = if raw == [0.0; 2] { [0.0; 2] } else { @@ -77,15 +115,16 @@ impl HeroLook { scaled } }); - if x != 0.0 { - let phase = self.phase + x * self.yaw_turns; - self.phase = if phase >= 1.0 { - phase - 1.0 - } else if phase <= 0.0 { - phase + 1.0 - } else { - phase - }; + let y = if self.reverse_y { -y } else { y }; + if !look_around || center_view { + self.camera_phase = [0.5; 2]; + } else { + let [horizontal, vertical] = self.camera_gain; + self.camera_phase[0] = wrap_angle(self.camera_phase[0] + x * horizontal); + self.camera_phase[1] = (self.camera_phase[1] + y * vertical).clamp(0.0, 1.0); + } + if x != 0.0 && !look_around { + self.phase = wrap_angle(self.phase + x * self.yaw_turns); } let requested = -(self.phase - 0.5) * TAU; @@ -95,8 +134,24 @@ impl HeroLook { } else { (self.phase + delivered * (1.0 / TAU)).rem_euclid(1.0) }; - let pitch = y * self.pitch_blend; - [delivered, if self.reverse_y { -pitch } else { pitch }] + [ + delivered, + if look_around { + 0.0 + } else { + y * self.pitch_blend + }, + ] + } +} + +fn wrap_angle(phase: f32) -> f32 { + if phase >= 1.0 { + phase - 1.0 + } else if phase <= 0.0 { + phase + 1.0 + } else { + phase } } @@ -114,11 +169,18 @@ pub(super) fn turret_pitch( Some((target - neutral) * span) } -fn mouse_binding(table: &str, axis: &str, class: &str, command: &str, wrap: &str) -> Option<f32> { +fn mouse_binding( + table: &str, + modifier: &str, + axis: &str, + class: &str, + command: &str, + wrap: &str, +) -> Option<f32> { table.lines().find_map(|line| { let line = line.split_once("//").map_or(line, |(binding, _)| binding); let mut words = line.split_ascii_whitespace(); - for name in ["MOUSE", "SCAN_NULL", axis, "1", class, command] { + for name in ["MOUSE", modifier, axis, "1", class, command] { if !words.next()?.eq_ignore_ascii_case(name) { return None; } @@ -153,10 +215,12 @@ mod tests { HeroLook { yaw_turns: 0.15, pitch_blend: 0.25, + camera_gain: [0.1, 0.15], reverse_y: true, sensitivity: 1.0, filtered: [0.0; 2], phase: 0.5, + camera_phase: [0.5; 2], } } @@ -171,6 +235,7 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 assert_eq!( mouse_binding( bindings, + "SCAN_NULL", "SCAN_MOUSE_X", "CICLS_UNKNOWN", "MCMD_ANGLE_Z", @@ -181,6 +246,7 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 assert_eq!( mouse_binding( bindings, + "SCAN_NULL", "SCAN_MOUSE_Y", "CICLS_TURRET", "MCMD_ANGLE_Y", @@ -198,13 +264,13 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 let mut look = stock_look(); for (dx, expected) in [(100.0, 0.0855), (-100.0, -0.0855), (250.0, 0.15)] { look.reset(); - assert!((look.advance([dx, 0.0], TAU)[0] / -TAU - expected).abs() < 1e-6); + assert!((look.advance([dx, 0.0], TAU, false, false)[0] / -TAU - expected).abs() < 1e-6); } look.reset(); - look.advance([100.0, 0.0], TAU); - assert!((look.advance([0.0, 1.0], TAU)[0] / -TAU - 0.004275).abs() < 1e-6); + look.advance([100.0, 0.0], TAU, false, false); + assert!((look.advance([0.0, 1.0], TAU, false, false)[0] / -TAU - 0.004275).abs() < 1e-6); assert_eq!(look.filtered[0], 4.75); - assert_eq!(look.advance([0.0; 2], TAU), [0.0; 2]); + assert_eq!(look.advance([0.0; 2], TAU, false, false), [0.0; 2]); assert!(!look.has_state()); } @@ -212,9 +278,9 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 fn hero_finishes_a_capped_turn_on_the_next_tick_without_mouse_input() { let mut look = stock_look(); let max_angle = 50.22849 * 0.01; - assert!((look.advance([250.0, 0.0], max_angle)[0] + 0.5022847).abs() < 1e-6); + assert!((look.advance([250.0, 0.0], max_angle, false, false)[0] + 0.5022847).abs() < 1e-6); assert!((look.phase - 0.5700589).abs() < 1e-6); - assert!((look.advance([0.0; 2], max_angle)[0] + 0.44019294).abs() < 1e-6); + assert!((look.advance([0.0; 2], max_angle, false, false)[0] + 0.44019294).abs() < 1e-6); assert!(!look.has_state()); } @@ -231,20 +297,41 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 flags: 0, }; let pitch = |previous, blend| turret_pitch(&row, previous, blend).unwrap().to_degrees(); - let upward = look.advance([0.0, -100.0], TAU)[1]; + let upward = look.advance([0.0, -100.0], TAU, false, false)[1]; assert!((upward - 0.171).abs() < 1e-6); assert!((pitch(0.0, upward) - 18.81).abs() < 1e-5); - let tail = look.advance([1.0, 0.0], TAU)[1]; + let tail = look.advance([1.0, 0.0], TAU, false, false)[1]; assert!((tail - 0.00855).abs() < 1e-6); look.reset(); - let downward = look.advance([0.0, 300.0], TAU)[1]; + let downward = look.advance([0.0, 300.0], TAU, false, false)[1]; assert_eq!(downward, -0.25); assert!((pitch(-25.0_f32.to_radians(), downward) + 30.0).abs() < 1e-5); assert!((pitch(70.0_f32.to_radians(), upward) - 80.0).abs() < 1e-5); look.reset(); look.reverse_y = false; - assert!((look.advance([0.0, 100.0], TAU)[1] - upward).abs() < 1e-6); + assert!((look.advance([0.0, 100.0], TAU, false, false)[1] - upward).abs() < 1e-6); + } + + #[test] + fn shift_routes_motion_to_camera_and_release_or_center_restores_sights() { + let mut look = stock_look(); + assert_eq!(look.advance([100.0, -100.0], TAU, true, false), [0.0; 2]); + let [yaw, pitch] = look.camera_offset(); + assert!((yaw + 0.057 * TAU).abs() < 1e-6); + assert!((pitch + 0.1026 * PI).abs() < 1e-6); + look.advance([0.0; 2], TAU, true, false); + assert_eq!(look.camera_offset(), [yaw, pitch]); + look.advance([0.0; 2], TAU, false, false); + assert_eq!(look.camera_offset(), [0.0; 2]); + assert!(!look.has_state()); + + look.camera_phase = [0.99; 2]; + look.advance([100.0, -100.0], TAU, true, false); + assert!((look.camera_phase[0] - 0.047).abs() < 1e-6); + assert_eq!(look.camera_phase[1], 1.0); + look.advance([100.0, -100.0], TAU, true, true); + assert_eq!(look.camera_offset(), [0.0; 2]); } } diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index f62c0784..3aa78bbe 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1408,6 +1408,8 @@ pub struct FrameResult { pub clan_notices: Vec<MissionNotice>, /// Vertical aim of the controlled hero as an IEEE-754 radian value. pub player_pitch_bits: u32, + /// View-only yaw and pitch relative to the sights, encoded as radians. + pub player_view_offset_bits: [u32; 2], /// Footstep from the accepted player motion, if this tick reached a footfall. pub player_footstep: Option<PlayerFootstep>, /// Autonomous combat events during the step. @@ -2990,6 +2992,9 @@ pub fn step_headless( .loaded .as_ref() .map_or(0, |mission| mission.player_pitch_bits), + player_view_offset_bits: engine.loaded.as_ref().map_or([0; 2], |mission| { + mission.hero_look.camera_offset().map(f32::to_bits) + }), }) } @@ -3045,6 +3050,7 @@ pub fn frame_with_player_motion( motion, blocked || drowned || landing.is_some(), ); + frame.player_view_offset_bits = mission.hero_look.camera_offset().map(f32::to_bits); if !blocked && !drowned && landing.is_none() @@ -17987,6 +17993,7 @@ mod tests { mouse_x: 100.0, mouse_y: -500.0, delta_seconds: 0.1, + ..Default::default() }; let proposed = plan_player_motion(&engine, cockpit, input) .unwrap() diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index de988a89..525bc7b3 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -56,6 +56,10 @@ pub struct PlayerMotion { pub mouse_x: f32, /// Relative vertical mouse motion for this step, positive down. pub mouse_y: f32, + /// While held, mouse input turns the cockpit view independently of the weapons. + pub look_around: bool, + /// Return the cockpit view to the sights, even if free look remains held. + pub center_view: bool, /// Time spent on this simulation step, excluding time spent paused. pub delta_seconds: f32, } @@ -459,7 +463,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.has_state()) + && !mission.hero_look.has_state() { return Ok(None); } @@ -471,15 +475,20 @@ fn plan_player_steps( { return Err(EngineError::Movement("invalid player step".into())); } - let mut hero_look = hero_control.then_some(mission.hero_look); + let mut look = mission.hero_look; + 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); + let [yaw, blend_delta] = look.advance( + [input.mouse_x, input.mouse_y], + max_angle, + input.look_around, + input.center_view, + ); // shortcut: mounted chassis use fixed gains until their bindings are wired. let fallback_pitch = || (pitch - input.mouse_y * 0.005).clamp(-1.0, 1.0); - let (mut yaw_delta, pitch) = 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); - let [yaw, blend_delta] = look.advance([input.mouse_x, input.mouse_y], max_angle); + let (mut yaw_delta, pitch) = if hero_control { let next_pitch = || { let control = mission .mission_assets @@ -496,6 +505,8 @@ fn plan_player_steps( pitch }; (yaw, pitch) + } else if input.look_around { + (0.0, pitch) } else { ( -input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003, @@ -551,7 +562,7 @@ fn plan_player_steps( )?; step.max_floor_clearance = max_floor_clearance; step.hero_walk_velocity = walk.map(|(velocity, _)| velocity); - step.hero_look = hero_look; + step.hero_look = Some(look); match resolve.as_mut() { Some(resolve) => resolve(step), None => Ok((step, false)), @@ -599,10 +610,14 @@ pub(super) fn commit_motion_state( if let Some(velocity) = step.hero_walk_velocity { mission.hero_walk_velocity = if blocked { [0.0; 2] } else { velocity }; } - if mission.boarding.is_none() { - if let Some(look) = step.hero_look { - mission.hero_look = look; - } + } + let active_cockpit = mission.boarding.map_or_else( + || mission.local_hero == Some(cockpit.handle), + |boarding| boarding.cockpit.handle == cockpit.handle, + ); + if active_cockpit { + if let Some(look) = step.hero_look { + mission.hero_look = look; } } if let (Some(boarding), Some(velocity)) = (&mut mission.boarding, step.flight_velocity) { |
