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 all commits
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
27 changes: 10 additions & 17 deletions src/lib.rs
Expand Up @@ -7,8 +7,6 @@
#[derive(Resource, Default)]
struct InputState {
reader_motion: ManualEventReader<MouseMotion>,
pitch: f32,
yaw: f32,
}

/// Mouse sensitivity and movement speed
Expand Down Expand Up @@ -114,26 +112,21 @@
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) {
match window.cursor_grab_mode() {
CursorGrabMode::None => (),
_ => {
// 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();
}
for ev in state.reader_motion.iter(&motion) {
let (mut yaw, mut pitch, _) = transform.rotation.to_euler(EulerRot::YXZ);
if window.cursor_locked() {

Check failure on line 118 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

no method named `cursor_locked` found for reference `&Window` in the current scope

Check failure on line 118 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no method named `cursor_locked` found for reference `&Window` in the current scope

Check failure on line 118 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

no method named `cursor_locked` found for reference `&Window` in the current scope
// Using smallest of height or width ensures equal vertical and horizontal sensitivity
let window_scale = window.height().min(window.width());
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