Cribbed from bevy flycam plugin, works much better

main
Elijah C. Voigt 1 year ago
parent 90f1df9da4
commit 1e42519a31

@ -1,3 +1,5 @@
use bevy::input::mouse::MouseButtonInput;
use crate::prelude::*; use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl /// Menu Plugin; empty struct for Plugin impl
@ -13,11 +15,13 @@ impl Plugin for CameraPlugin {
pub(crate) struct FlyCamera; pub(crate) struct FlyCamera;
/// Fly camera system for moving around like a drone /// Fly camera system for moving around like a drone
/// TODO: Only if key is pressed!
fn editor_fly_camera( fn editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>, mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>, windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>, primary_window: Query<Entity, With<PrimaryWindow>>,
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>, mut cursor_events: EventReader<CursorMoved>,
time: Res<Time>, time: Res<Time>,
) { ) {
@ -35,8 +39,10 @@ fn editor_fly_camera(
}, },
_ => None, _ => None,
}; };
let window = windows.get(target_window.unwrap()).unwrap();
// If the target window is focused // If the target window is focused
windows.get(target_window.unwrap()).unwrap().focused.then(|| { window.focused.then(|| {
let move_speed = 4.0; let move_speed = 4.0;
let mut delta = Vec3::ZERO; let mut delta = Vec3::ZERO;
if keys.pressed(KeyCode::KeyW) { if keys.pressed(KeyCode::KeyW) {
@ -60,26 +66,24 @@ fn editor_fly_camera(
t.translation += delta; t.translation += delta;
}); });
if mouse.pressed(MouseButton::Middle) {
cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| { cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| {
(*window == target_window.unwrap()).then_some(delta) (*window == target_window.unwrap()).then_some(delta)
}).for_each(|delta| { }).for_each(|delta| {
// If there was actual movement, unpack those values
if let Some(Vec2 { x, y }) = delta { if let Some(Vec2 { x, y }) = delta {
if x > y { // Cribbing from bevy_flycam
// Find the horizontal angle of movement // Link: https://github.com/sburris0/bevy_flycam/blob/baffe50e0961ad1491d467fa6ab5551f9f21db8f/src/lib.rs#L145-L151
let width = 1280.0; let (mut yaw, mut pitch, _) = t.rotation.to_euler(EulerRot::YXZ);
let ratio_width = x / width; let window_scale = window.height().min(window.width());
let angle_width = ratio_width * std::f32::consts::PI; let sensitivity = 0.00012;
t.rotate_local_y(-angle_width); pitch -= (sensitivity * y * window_scale).to_radians();
} else { yaw -= (sensitivity * x * window_scale).to_radians();
// Find the vertical angle of movement t.rotation = Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
let height = 720.0;
let ratio_height = y / height;
let angle_height = ratio_height * std::f32::consts::PI;
t.rotate_local_x(-angle_height);
}
} }
}); });
} else {
cursor_events.clear();
}
}); });
}); });
} }

@ -104,8 +104,8 @@ fn origin_directions(
fn world_plane( fn world_plane(
mut gizmos: Gizmos mut gizmos: Gizmos
) { ) {
(-10..10).into_iter().for_each(|x| { (-10..=10).into_iter().for_each(|x| {
(-10..10).into_iter().for_each(|z| { (-10..=10).into_iter().for_each(|z| {
{ {
let start = Vec3::new(x as f32, 0.0, -10.0); let start = Vec3::new(x as f32, 0.0, -10.0);
let end = Vec3::new(x as f32, 0.0, 10.0); let end = Vec3::new(x as f32, 0.0, 10.0);

Loading…
Cancel
Save