Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use yaw/pitch from Camera's rotation. #21

Merged
merged 2 commits into from Apr 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/lib.rs
Expand Up @@ -6,8 +6,6 @@ use bevy::prelude::*;
#[derive(Default)]
struct InputState {
reader_motion: ManualEventReader<MouseMotion>,
pitch: f32,
yaw: f32,
}

/// Mouse sensitivity and movement speed
Expand Down Expand Up @@ -101,23 +99,21 @@ fn player_look(
mut query: Query<&mut Transform, With<FlyCam>>,
) {
if let Some(window) = windows.get_primary() {
let mut delta_state = state.as_mut();
for mut transform in query.iter_mut() {
for ev in delta_state.reader_motion.iter(&motion) {
for ev in state.reader_motion.iter(&motion) {
let (mut yaw, mut pitch, _) = transform.rotation.to_euler(EulerRot::YXZ);
if window.cursor_locked() {
// Using smallest of height or width ensures equal vertical and horizontal sensitivity
let window_scale = window.height().min(window.width());
delta_state.pitch -=
(settings.sensitivity * ev.delta.y * window_scale).to_radians();
delta_state.yaw -=
(settings.sensitivity * ev.delta.x * window_scale).to_radians();
pitch -= (settings.sensitivity * ev.delta.y * window_scale).to_radians();
yaw -= (settings.sensitivity * ev.delta.x * window_scale).to_radians();
}

delta_state.pitch = delta_state.pitch.clamp(-1.54, 1.54);
pitch = pitch.clamp(-1.54, 1.54);

// Order is important to prevent unintended roll
transform.rotation = Quat::from_axis_angle(Vec3::Y, delta_state.yaw)
* Quat::from_axis_angle(Vec3::X, delta_state.pitch);
transform.rotation =
Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
}
}
} else {
Expand Down