diff --git a/src/camera.rs b/src/camera.rs index 6405e04..61899e9 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -40,13 +40,7 @@ fn update_view_matrix( ) { for (camera, transform) in (&mut vm_camera, v_transform.inserted_or_modified()).iter() { let (_, rotation, translation) = transform.0.to_scale_rotation_translation(); - //let direction = rotation * Vec3::Z; //this may be incorrect! - let (yaw, pitch, _) = rotation.to_euler(EulerRot::YXZ); - let direction= Vec3::new( - yaw.cos() * pitch.cos(), - pitch.sin(), - yaw.sin() * pitch.cos() - ); + let direction = rotation * Vec3::NEG_Z; camera.view_matrix = Mat4::look_to_rh(translation, direction, camera.up); } } diff --git a/src/fly_controller.rs b/src/fly_controller.rs index b6f35f4..85f3821 100644 --- a/src/fly_controller.rs +++ b/src/fly_controller.rs @@ -1,35 +1,51 @@ -use glam::{Vec3, Mat4, Quat, EulerRot}; -use shipyard::{Component, View, ViewMut, IntoIter, UniqueView}; +use glam::{Vec3, Mat4, Quat, EulerRot, Vec2}; +use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload}; +use std::f32::consts::PI; use crate::{transform::Transform, input::Inputs, settings::GameSettings}; #[derive(Component)] pub struct FlyController; -pub fn update_controllers( +pub fn update_controllers() -> Workload { + ( + update_look, + update_movement + ).into_workload() +} + +const MAX_PITCH: f32 = PI/2. - 0.001; + +fn update_look( controllers: View, mut transforms: ViewMut, inputs: UniqueView, settings: UniqueView, ) { + let look = inputs.look * settings.mouse_sensitivity; + if look == Vec2::ZERO { return } for (_, mut transform) in (&controllers, &mut transforms).iter() { - let (scale, mut rotation, mut translation) = transform.0.to_scale_rotation_translation(); - let look = inputs.look * settings.mouse_sensitivity; - - //rotation *= Quat::from_axis_angle(Vec3::Y, look.x); - - //old way - // rotation = rotation.normalize(); - // rotation *= Quat::from_euler(EulerRot::ZYX, 0., look.x, look.y).normalize(); - // rotation = rotation.normalize(); - - // let direction = (rotation * Vec3::Z).normalize(); - // let camera_right = Vec3::Y.cross(direction).normalize(); - // let camera_up = direction.cross(camera_right); - // rotation *= Quat::from_axis_angle(Vec3::Y, look.x); - // rotation *= Quat::from_axis_angle(camera_right, look.y); - - //translation += (rotation * Vec3::X) / 4.; - + let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation(); + let (mut yaw, mut pitch, _roll) = rotation.to_euler(EulerRot::YXZ); + yaw -= look.x; + pitch -= look.y; + pitch = pitch.clamp(-MAX_PITCH, MAX_PITCH); + rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, 0.); + transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation); + } +} + +fn update_movement( + controllers: View, + mut transforms: ViewMut, + inputs: UniqueView, + settings: UniqueView, +) { + let movement = inputs.movement; + if movement == Vec2::ZERO { return } + for (_, mut transform) in (&controllers, &mut transforms).iter() { + let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation(); + translation += (rotation * Vec3::NEG_Z) * movement.y; + translation += (rotation * Vec3::X) * movement.x; transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation); } }