add camera::sync_camera_to_player function, fixing cam alignment

This commit is contained in:
yuni 2024-03-30 16:18:49 +01:00
parent bee0d98ae1
commit b945d7bda7

View file

@ -14,6 +14,7 @@ impl Plugin for CameraControllerPlugin {
app.add_systems(Startup, setup_camera); app.add_systems(Startup, setup_camera);
app.add_systems(Update, handle_input); app.add_systems(Update, handle_input);
app.add_systems(Update, manage_player_actor); app.add_systems(Update, manage_player_actor);
app.add_systems(Update, sync_camera_to_player);
app.add_systems(Update, run_camera_controller); app.add_systems(Update, run_camera_controller);
} }
} }
@ -66,6 +67,29 @@ fn setup_camera(
)); ));
} }
pub fn sync_camera_to_player(
settings: Res<settings::Settings>,
mut q_camera: Query<&mut Transform, With<CameraController>>,
q_playercam: Query<(&actor::Actor, &Transform), (With<actor::PlayerCamera>, Without<actor::PlayerDrivesThis>, Without<CameraController>)>,
) {
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( pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut settings: ResMut<settings::Settings>, mut settings: ResMut<settings::Settings>,
@ -104,10 +128,9 @@ fn run_camera_controller(
rocket_sound_controller: Query<&AudioSink, With<audio::ComponentRocketSound>>, rocket_sound_controller: Query<&AudioSink, With<audio::ComponentRocketSound>>,
ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>, ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>,
mut q_engine: Query<&mut actor::Engine, With<actor::PlayerDrivesThis>>, mut q_engine: Query<&mut actor::Engine, With<actor::PlayerDrivesThis>>,
mut q_camera: Query<(&mut Transform, &mut CameraController, &mut Projection)>, mut q_camera: Query<(&mut CameraController, &mut Projection)>,
q_player: Query<&actor::LifeForm, With<actor::Player>>, q_player: Query<&actor::LifeForm, With<actor::Player>>,
mut q_playercam: Query<( mut q_playercam: Query<(
&actor::Actor,
&mut Transform, &mut Transform,
&mut actor::Engine, &mut actor::Engine,
&mut AngularVelocity, &mut AngularVelocity,
@ -124,9 +147,9 @@ fn run_camera_controller(
} }
if let ( if let (
Ok((mut camera_transform, mut controller, mut projection)), Ok((mut controller, mut projection)),
Ok(lifeform), 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()) { ) = (q_camera.get_single_mut(), q_player.get_single(), q_playercam.get_single_mut()) {
if !controller.enabled { if !controller.enabled {
mouse_events.clear(); mouse_events.clear();
@ -226,14 +249,6 @@ fn run_camera_controller(
.clamp(-PI / 2., PI / 2.); .clamp(-PI / 2., PI / 2.);
controller.yaw += mouse_delta.x * RADIANS_PER_DOT * controller.sensitivity; 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(); 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(); let fov = (lifeform.adrenaline * lifeform.adrenaline * lifeform.adrenaline * 45.0 + 45.0).to_radians();