Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 3, 2023
1 parent e858926 commit bc99770
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (),
_ => {
Expand All @@ -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 {
Expand Down

0 comments on commit bc99770

Please sign in to comment.