diff --git a/src/lib.rs b/src/lib.rs index da5f60e..7eef92c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,7 +154,10 @@ fn player_look( for mut transform in query.iter_mut() { for ev in state.reader_motion.read(&motion) { let mut yaw = 0.0; - let mut pitch = 0.0; + let up = transform.up(); + let right = transform.right(); + let original_pitch = settings.up.cross(up).dot(right).atan2(settings.up.dot(up)); + let mut pitch = original_pitch; match window.cursor.grab_mode { CursorGrabMode::None => (), _ => { @@ -165,21 +168,10 @@ fn player_look( } } - // Order is important to prevent unintended roll - transform.rotation = Quat::from_axis_angle(settings.up, yaw) - * transform.rotation - * Quat::from_axis_angle(Vec3::X, pitch); - - let up = transform.up(); - let right = transform.right(); - let pitch = settings.up.cross(up).dot(right).atan2(settings.up.dot(up)); let restricted_pitch = pitch.clamp(-1.54, 1.54); - let diff = restricted_pitch - pitch; - transform.rotate_axis(right, diff); - // Eliminate accumulated roll and ensure we don't flip onto our heads. - let forward = transform.forward(); - transform.look_to(forward, settings.up); + transform.rotate_axis(settings.up, yaw); + transform.rotate_axis(right, restricted_pitch - original_pitch); } } } else {