From b945d7bda7c3ea606b5ed9a8cee87b237dfe5963 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 30 Mar 2024 16:18:49 +0100 Subject: [PATCH] add camera::sync_camera_to_player function, fixing cam alignment --- src/camera.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 1462b87..43e6640 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -14,6 +14,7 @@ impl Plugin for CameraControllerPlugin { app.add_systems(Startup, setup_camera); app.add_systems(Update, handle_input); app.add_systems(Update, manage_player_actor); + app.add_systems(Update, sync_camera_to_player); app.add_systems(Update, run_camera_controller); } } @@ -66,6 +67,29 @@ fn setup_camera( )); } +pub fn sync_camera_to_player( + settings: Res, + mut q_camera: Query<&mut Transform, With>, + q_playercam: Query<(&actor::Actor, &Transform), (With, Without, Without)>, +) { + if q_camera.is_empty() || q_playercam.is_empty() { + return; + } + let mut camera_transform = q_camera.get_single_mut().unwrap(); + let (actor, player_transform) = q_playercam.get_single().unwrap(); + + // Rotation + camera_transform.rotation = player_transform.rotation * Quat::from_array([0.0, -1.0, 0.0, 0.0]); + + // Translation + if settings.third_person { + camera_transform.translation = player_transform.translation + camera_transform.rotation * (actor.camdistance * Vec3::new(0.0, 0.2, 1.0)); + } + else { + camera_transform.translation = player_transform.translation; + } +} + pub fn handle_input( keyboard_input: Res>, mut settings: ResMut, @@ -104,10 +128,9 @@ fn run_camera_controller( rocket_sound_controller: Query<&AudioSink, With>, ion_sound_controller: Query<&AudioSink, With>, mut q_engine: Query<&mut actor::Engine, With>, - mut q_camera: Query<(&mut Transform, &mut CameraController, &mut Projection)>, + mut q_camera: Query<(&mut CameraController, &mut Projection)>, q_player: Query<&actor::LifeForm, With>, mut q_playercam: Query<( - &actor::Actor, &mut Transform, &mut actor::Engine, &mut AngularVelocity, @@ -124,9 +147,9 @@ fn run_camera_controller( } if let ( - Ok((mut camera_transform, mut controller, mut projection)), + Ok((mut controller, mut projection)), Ok(lifeform), - Ok((actor, mut player_transform, player_engine, mut angularvelocity, mut v)), + Ok((mut player_transform, player_engine, mut angularvelocity, mut v)), ) = (q_camera.get_single_mut(), q_player.get_single(), q_playercam.get_single_mut()) { if !controller.enabled { mouse_events.clear(); @@ -226,14 +249,6 @@ fn run_camera_controller( .clamp(-PI / 2., PI / 2.); controller.yaw += mouse_delta.x * RADIANS_PER_DOT * controller.sensitivity; player_transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, -controller.yaw, controller.pitch).normalize(); - camera_transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, PI-controller.yaw, -controller.pitch).normalize(); - } - - if settings.third_person { - camera_transform.translation = player_transform.translation + camera_transform.rotation * (actor.camdistance * Vec3::new(0.0, 0.2, 1.0)); - } - else { - camera_transform.translation = player_transform.translation; } let fov = (lifeform.adrenaline * lifeform.adrenaline * lifeform.adrenaline * 45.0 + 45.0).to_radians();