aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-render
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 05:40:01 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 05:40:01 +0300
commit8065381152e2d4f89279fd662e7376013ad13e82 (patch)
tree27bcf4b04ddb71fc8df23a53514f87a3dc509ef7 /crates/fparkan-render
parent520273dbe04fc1c9ea40127fb198d2bb5f0813f9 (diff)
downloadfparkan-8065381152e2d4f89279fd662e7376013ad13e82.tar.xz
fparkan-8065381152e2d4f89279fd662e7376013ad13e82.zip
feat(game): tilt the cockpit view from vertical mouse input
Diffstat (limited to 'crates/fparkan-render')
-rw-r--r--crates/fparkan-render/src/lib.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/crates/fparkan-render/src/lib.rs b/crates/fparkan-render/src/lib.rs
index f34138e1..c8c94e09 100644
--- a/crates/fparkan-render/src/lib.rs
+++ b/crates/fparkan-render/src/lib.rs
@@ -25,6 +25,25 @@ impl RawCameraTransform {
Self::TRANSLATION_WORD_INDICES.map(|index| f32::from_bits(self.words[index]))
}
+ /// Rotates a cockpit's forward and up axes about its lateral axis,
+ /// preserving the camera point.
+ #[must_use]
+ pub fn with_cockpit_pitch(mut self, radians: f32) -> Option<Self> {
+ if !radians.is_finite() {
+ return None;
+ }
+ if radians != 0.0 {
+ let (sin, cos) = radians.sin_cos();
+ for row in self.words[..12].chunks_exact_mut(4) {
+ let forward = f32::from_bits(row[0]);
+ let up = f32::from_bits(row[2]);
+ row[0] = (forward * cos + up * sin).to_bits();
+ row[2] = (up * cos - forward * sin).to_bits();
+ }
+ }
+ Some(self)
+ }
+
/// Inverts a finite row-major affine transform without assigning it a
/// camera-space meaning.
///
@@ -609,6 +628,26 @@ mod tests {
}
#[test]
+ fn pitching_the_cockpit_preserves_its_origin_and_lateral_axis() {
+ let unit = LegacyIron3dEulerTransform {
+ translation: [12.0, 3.0, 4.0],
+ orientation_radians: [0.0, 0.0, 0.0],
+ };
+ let camera = forward_cockpit_camera(unit, [1.0; 3], [0.0; 3]).unwrap();
+ assert_eq!(camera.with_cockpit_pitch(0.0), Some(camera));
+ let tilted = camera
+ .with_cockpit_pitch(std::f32::consts::FRAC_PI_2)
+ .unwrap();
+ assert_eq!(camera.translation(), tilted.translation());
+ for axis in [1, 5, 9] {
+ assert_eq!(camera.words[axis], tilted.words[axis]);
+ }
+ let tilted = tilted.words.map(f32::from_bits);
+ assert_eq!(tilted[8], 1.0);
+ assert!((tilted[6] + 1.0).abs() < 1e-6);
+ }
+
+ #[test]
fn water_reflection_preserves_the_plane_and_reverses_height_before_projection() {
let matrix = [
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,