mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-13 19:08:41 -06:00
yay it works!
This commit is contained in:
parent
56c2f0de88
commit
f3f0227511
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>,
|
||||||
) {
|
) {
|
||||||
|
let look = inputs.look * settings.mouse_sensitivity;
|
||||||
|
if look == Vec2::ZERO { return }
|
||||||
for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
for (_, mut transform) in (&controllers, &mut transforms).iter() {
|
||||||
let (scale, mut rotation, mut translation) = transform.0.to_scale_rotation_translation();
|
let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation();
|
||||||
let look = inputs.look * settings.mouse_sensitivity;
|
let (mut yaw, mut pitch, _roll) = rotation.to_euler(EulerRot::YXZ);
|
||||||
|
yaw -= look.x;
|
||||||
//rotation *= Quat::from_axis_angle(Vec3::Y, look.x);
|
pitch -= look.y;
|
||||||
|
pitch = pitch.clamp(-MAX_PITCH, MAX_PITCH);
|
||||||
//old way
|
rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, 0.);
|
||||||
// rotation = rotation.normalize();
|
transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
|
||||||
// rotation *= Quat::from_euler(EulerRot::ZYX, 0., look.x, look.y).normalize();
|
}
|
||||||
// rotation = rotation.normalize();
|
}
|
||||||
|
|
||||||
// let direction = (rotation * Vec3::Z).normalize();
|
fn update_movement(
|
||||||
// let camera_right = Vec3::Y.cross(direction).normalize();
|
controllers: View<FlyController>,
|
||||||
// let camera_up = direction.cross(camera_right);
|
mut transforms: ViewMut<Transform>,
|
||||||
// rotation *= Quat::from_axis_angle(Vec3::Y, look.x);
|
inputs: UniqueView<Inputs>,
|
||||||
// rotation *= Quat::from_axis_angle(camera_right, look.y);
|
settings: UniqueView<GameSettings>,
|
||||||
|
) {
|
||||||
//translation += (rotation * Vec3::X) / 4.;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue