From 1f534bb1aacbe0a30b7fadc145e3a09e1703a667 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 26 Oct 2023 12:17:10 -0400 Subject: [PATCH] Fix unintended roll of fly cam --- examples/testbed/src/free_fly_camera.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/testbed/src/free_fly_camera.rs b/examples/testbed/src/free_fly_camera.rs index 590c850..10cdb45 100644 --- a/examples/testbed/src/free_fly_camera.rs +++ b/examples/testbed/src/free_fly_camera.rs @@ -5,7 +5,7 @@ use lyra_engine::{ ecs::{components::camera::CameraComponent, EventQueue, SimpleSystem}, game::Game, input::{InputButtons, KeyCode, MouseMotion}, - math::{Angle, Quat, Vec3}, + math::{Angle, Quat, Vec3, EulerRot}, plugin::Plugin, }; use tracing::debug; @@ -23,7 +23,7 @@ impl Default for FreeFlyCamera { Self { speed: 0.07, look_speed: 0.01, - mouse_sensitivity: 0.03, + mouse_sensitivity: 0.04, look_with_keys: false, } } @@ -86,7 +86,7 @@ impl SimpleSystem for FreeFlyCameraController { { let forward = cam.transform.forward(); let left = cam.transform.left(); - let up = cam.transform.up(); + let up = Vec3::Y; // handle camera movement if let Some(keys) = keys.as_ref() { @@ -130,9 +130,15 @@ impl SimpleSystem for FreeFlyCameraController { if camera_rot != Vec3::ZERO { let look_velocity = camera_rot * fly.look_speed; - cam.transform.rotation *= Quat::from_rotation_x(look_velocity.x) - * Quat::from_rotation_y(look_velocity.y) - * Quat::from_rotation_z(look_velocity.z); + + let (mut y, mut x, _) = cam.transform.rotation.to_euler(EulerRot::YXZ); + x += look_velocity.x; + y += look_velocity.y; + x = x.clamp(-1.54, 1.54); + + // rotation is not commutative, keep this order to avoid unintended roll + cam.transform.rotation = Quat::from_axis_angle(Vec3::Y, y) + * Quat::from_axis_angle(Vec3::X, x); } }