From 05da53f0344416a5f14211a73f1f48d72c88b954 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 14:38:56 +0400 Subject: feat(ui): drive a strategic warbot with an automatic gunner --- apps/fparkan-game/src/cockpit_geometry.rs | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'apps/fparkan-game/src/cockpit_geometry.rs') diff --git a/apps/fparkan-game/src/cockpit_geometry.rs b/apps/fparkan-game/src/cockpit_geometry.rs index 7435708f..0faab1d2 100644 --- a/apps/fparkan-game/src/cockpit_geometry.rs +++ b/apps/fparkan-game/src/cockpit_geometry.rs @@ -14,6 +14,60 @@ struct Binding { projection: [f32; 3], } +pub(super) fn cockpit_sights( + hull: LegacyIron3dEulerTransform, + scale: [f32; 3], + point: [f32; 3], + node_rotation: [f32; 4], + manual_pitch: Option, +) -> Option { + if let Some(pitch) = manual_pitch { + return fparkan_render::forward_cockpit_camera(hull, scale, point)? + .with_cockpit_pitch(pitch); + } + let [x, y, z] = hull.try_transform_scaled_point(point, scale)?; + let hull_orientation = LegacyIron3dEulerTransform { + translation: [0.0; 3], + ..hull + }; + let transform_axis = |axis| { + hull_orientation + .try_transform_scaled_point(rotate_unit_vector(axis, node_rotation), [1.0; 3]) + }; + let [fx, fy, fz] = transform_axis([0.0, 1.0, 0.0])?; + let [lx, ly, lz] = transform_axis([-1.0, 0.0, 0.0])?; + let [ux, uy, uz] = transform_axis([0.0, 0.0, 1.0])?; + Some(RawCameraTransform { + words: [ + fx, lx, ux, x, fy, ly, uy, y, fz, lz, uz, z, 0.0, 0.0, 0.0, 1.0, + ] + .map(f32::to_bits), + }) +} + +#[cfg(test)] +#[test] +fn gunner_view_turns_with_the_camera_node() { + let hull = LegacyIron3dEulerTransform { + translation: [10.0, 0.0, 0.0], + orientation_radians: [0.0; 3], + }; + let half = std::f32::consts::FRAC_PI_4; + let sights = cockpit_sights( + hull, + [1.0; 3], + [0.0, 2.0, 3.0], + [0.0, 0.0, half.sin(), half.cos()], + None, + ) + .unwrap(); + assert_eq!(sights.translation(), [10.0, 2.0, 3.0]); + let forward = [0, 4, 8].map(|index| f32::from_bits(sights.words[index])); + for (actual, expected) in forward.into_iter().zip([-1.0, 0.0, 0.0]) { + assert!((actual - expected).abs() < 1e-6); + } +} + impl CockpitGeometry { pub(super) fn prepare( engine: &Engine, -- cgit v1.2.3