aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/cockpit_geometry.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 13:38:56 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 13:38:56 +0300
commit05da53f0344416a5f14211a73f1f48d72c88b954 (patch)
treed27d22f56a1bd185447b27223c79c9f97f7cc165 /apps/fparkan-game/src/cockpit_geometry.rs
parent787279a1f514fbbaf09ca9dd6e5015bdce36635b (diff)
downloadfparkan-05da53f0344416a5f14211a73f1f48d72c88b954.tar.xz
fparkan-05da53f0344416a5f14211a73f1f48d72c88b954.zip
feat(ui): drive a strategic warbot with an automatic gunner
Diffstat (limited to 'apps/fparkan-game/src/cockpit_geometry.rs')
-rw-r--r--apps/fparkan-game/src/cockpit_geometry.rs54
1 files changed, 54 insertions, 0 deletions
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<f32>,
+) -> Option<RawCameraTransform> {
+ 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,