yay it works!

This commit is contained in:
griffi-gh 2023-01-26 03:28:05 +01:00
parent 56c2f0de88
commit f3f0227511
2 changed files with 38 additions and 28 deletions

View file

@ -40,13 +40,7 @@ fn update_view_matrix(
) { ) {
for (camera, transform) in (&mut vm_camera, v_transform.inserted_or_modified()).iter() { for (camera, transform) in (&mut vm_camera, v_transform.inserted_or_modified()).iter() {
let (_, rotation, translation) = transform.0.to_scale_rotation_translation(); let (_, rotation, translation) = transform.0.to_scale_rotation_translation();
//let direction = rotation * Vec3::Z; //this may be incorrect! let direction = rotation * Vec3::NEG_Z;
let (yaw, pitch, _) = rotation.to_euler(EulerRot::YXZ);
let direction= Vec3::new(
yaw.cos() * pitch.cos(),
pitch.sin(),
yaw.sin() * pitch.cos()
);
camera.view_matrix = Mat4::look_to_rh(translation, direction, camera.up); camera.view_matrix = Mat4::look_to_rh(translation, direction, camera.up);
} }
} }

View file

@ -1,35 +1,51 @@
use glam::{Vec3, Mat4, Quat, EulerRot}; use glam::{Vec3, Mat4, Quat, EulerRot, Vec2};
use shipyard::{Component, View, ViewMut, IntoIter, UniqueView}; use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload};
use std::f32::consts::PI;
use crate::{transform::Transform, input::Inputs, settings::GameSettings}; use crate::{transform::Transform, input::Inputs, settings::GameSettings};
#[derive(Component)] #[derive(Component)]
pub struct FlyController; 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<FlyController>, controllers: View<FlyController>,
mut transforms: ViewMut<Transform>, mut transforms: ViewMut<Transform>,
inputs: UniqueView<Inputs>, inputs: UniqueView<Inputs>,
settings: UniqueView<GameSettings>, settings: UniqueView<GameSettings>,
) { ) {
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; let look = inputs.look * settings.mouse_sensitivity;
if look == Vec2::ZERO { return }
//rotation *= Quat::from_axis_angle(Vec3::Y, look.x); for (_, mut transform) in (&controllers, &mut transforms).iter() {
let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation();
//old way let (mut yaw, mut pitch, _roll) = rotation.to_euler(EulerRot::YXZ);
// rotation = rotation.normalize(); yaw -= look.x;
// rotation *= Quat::from_euler(EulerRot::ZYX, 0., look.x, look.y).normalize(); pitch -= look.y;
// rotation = rotation.normalize(); pitch = pitch.clamp(-MAX_PITCH, MAX_PITCH);
rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, 0.);
// let direction = (rotation * Vec3::Z).normalize(); transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
// 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); fn update_movement(
controllers: View<FlyController>,
//translation += (rotation * Vec3::X) / 4.; mut transforms: ViewMut<Transform>,
inputs: UniqueView<Inputs>,
settings: UniqueView<GameSettings>,
) {
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); transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
} }
} }