|
|
|
|
@ -40,7 +40,7 @@ pub fn camera_free_look( |
|
|
|
|
camera: Single<(&mut Transform, &mut CameraFreeLook)>, |
|
|
|
|
) { |
|
|
|
|
let (window, mut cursor) = window.into_inner(); |
|
|
|
|
let (mut transform, mut camera) = camera.into_inner(); |
|
|
|
|
let (mut transform, mut look) = camera.into_inner(); |
|
|
|
|
|
|
|
|
|
if !window.focused || key_input.just_pressed(KeyCode::Escape) { |
|
|
|
|
cursor.grab_mode = CursorGrabMode::None; |
|
|
|
|
@ -54,19 +54,19 @@ pub fn camera_free_look( |
|
|
|
|
if cursor.grab_mode == CursorGrabMode::Locked { |
|
|
|
|
// Ensure the yaw and pitch are initialized once
|
|
|
|
|
// from the camera transform's current rotation.
|
|
|
|
|
if !camera.initialized { |
|
|
|
|
(camera.yaw, camera.pitch, _) = transform.rotation.to_euler(EulerRot::YXZ); |
|
|
|
|
camera.initialized = true; |
|
|
|
|
if !look.initialized { |
|
|
|
|
(look.yaw, look.pitch, _) = transform.rotation.to_euler(EulerRot::YXZ); |
|
|
|
|
look.initialized = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Update the current camera state's internal yaw and pitch.
|
|
|
|
|
let motion = accumulated_mouse_motion.delta * camera.sensitivity; |
|
|
|
|
let (min, max) = camera.pitch_limit.clone().into_inner(); |
|
|
|
|
camera.yaw = (camera.yaw - motion.x).rem_euclid(TAU); // keep within 0°..360°
|
|
|
|
|
camera.pitch = (camera.pitch - motion.y).clamp(min, max); |
|
|
|
|
let motion = accumulated_mouse_motion.delta * look.sensitivity; |
|
|
|
|
let (min, max) = look.pitch_limit.clone().into_inner(); |
|
|
|
|
look.yaw = (look.yaw - motion.x).rem_euclid(TAU); // keep within 0°..360°
|
|
|
|
|
look.pitch = (look.pitch - motion.y).clamp(min, max); |
|
|
|
|
|
|
|
|
|
// Override the camera transform's rotation.
|
|
|
|
|
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, camera.yaw, camera.pitch); |
|
|
|
|
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, look.yaw, look.pitch); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|