Fixed gimball lock and all rotations work great now

This commit is contained in:
Franklin 2023-11-23 10:43:57 -04:00
parent 521491d3a6
commit c5c8a4ba5b

View File

@ -2,7 +2,7 @@ use bevy::{input::mouse::MouseMotion, prelude::*, window::CursorGrabMode};
//use bevy_rapier3d::prelude::*; //use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
comps::core::markers::{camera::MainCamera, player::Player}, comps::core::markers::{camera::MainCamera, player::Player, proxy::{physics::utils::F32Ext, character::player_character::PlayerCharacter}},
ui::game::game_ui_state::{GameUiState, GameUiWindow}, ui::game::game_ui_state::{GameUiState, GameUiWindow},
}; };
@ -72,6 +72,7 @@ pub fn follow_cursor_with_camera(
mut motions: EventReader<MouseMotion>, mut motions: EventReader<MouseMotion>,
mut player_query: Query<&mut Transform, (With<Player>, Without<MainCamera>)>, mut player_query: Query<&mut Transform, (With<Player>, Without<MainCamera>)>,
mut camera_query: Query<&mut Transform, (With<MainCamera>, Without<Player>)>, mut camera_query: Query<&mut Transform, (With<MainCamera>, Without<Player>)>,
mut player_model_query: Query<&mut Transform, (With<PlayerCharacter>, Without<Player>, Without<MainCamera>)>,
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
btn: Res<Input<MouseButton>>, btn: Res<Input<MouseButton>>,
time: Res<Time>, time: Res<Time>,
@ -123,6 +124,7 @@ pub fn follow_cursor_with_camera(
if window.cursor.grab_mode != CursorGrabMode::None { if window.cursor.grab_mode != CursorGrabMode::None {
for mut player_transform in player_query.iter_mut() { for mut player_transform in player_query.iter_mut() {
for mut camera_transform in camera_query.iter_mut() { for mut camera_transform in camera_query.iter_mut() {
for mut player_model_transform in player_model_query.iter_mut() {
let (mut yaw, mut pitch) = (player_transform.rotation.to_euler(EulerRot::YXZ).0, camera_transform.rotation.to_euler(EulerRot::YXZ).1) ; let (mut yaw, mut pitch) = (player_transform.rotation.to_euler(EulerRot::YXZ).0, camera_transform.rotation.to_euler(EulerRot::YXZ).1) ;
for motion in motions.read() { for motion in motions.read() {
let window_scale = window.height().min(window.width()); let window_scale = window.height().min(window.width());
@ -151,40 +153,34 @@ pub fn follow_cursor_with_camera(
let desired_rotation_quat_camera = let desired_rotation_quat_camera =
Quat::from_axis_angle(Vec3::X, pitch); Quat::from_axis_angle(Vec3::X, pitch);
let desired_rotation_quat_player = Quat::from_axis_angle(Vec3::Y, yaw) * -1. ; let desired_rotation_quat_player = Quat::from_axis_angle(Vec3::Y, yaw);
//let local_z = camera_transform.local_z(); //let local_z = camera_transform.local_z();
//let right = Vec3::new(local_z.z, camera_transform.translation.y, -local_z.x); //let right = Vec3::new(local_z.z, camera_transform.translation.y, -local_z.x);
camera_transform.rotation = desired_rotation_quat_camera; camera_transform.rotation = desired_rotation_quat_camera;
player_transform.rotation = desired_rotation_quat_player;
let player_model_default_rot = Quat::from_euler(EulerRot::XYZ, -180.0f32.to_radians(), 0.0, -180.0f32.to_radians());
if keyboard_input.pressed(KeyCode::Q) { if keyboard_input.pressed(KeyCode::Q) {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ); let player_model_final_rot = Quat::from_euler(EulerRot::XYZ, -180.0f32.to_radians(), 0.0, (-180.0 - player_values_state.player_lean_angle).to_radians());
eulers.2 = eulers.2 + player_values_state.player_lean_angle.to_radians(); player_model_transform.rotation = player_model_transform.rotation.lerp(
let mut new_rot = player_transform.rotation.lerp( player_model_final_rot,
Quat::from_euler(EulerRot::XYZ, eulers.0, eulers.1, eulers.2),
time.delta_seconds() / player_values_state.player_lean_time, time.delta_seconds() / player_values_state.player_lean_time,
).to_euler(EulerRot::XYZ); )
new_rot.1 = eulers.1;
player_transform.rotation = Quat::from_euler(EulerRot::XYZ, new_rot.0, new_rot.1, new_rot.2);
} else if keyboard_input.pressed(KeyCode::E) { } else if keyboard_input.pressed(KeyCode::E) {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ); let player_model_final_rot = Quat::from_euler(EulerRot::XYZ, -180.0f32.to_radians(), 0.0, (-180.0 + player_values_state.player_lean_angle).to_radians());
eulers.2 = eulers.2 + (player_values_state.player_lean_angle.to_radians() * -1.0); player_model_transform.rotation = player_model_transform.rotation.lerp(
let mut new_rot = player_transform.rotation.lerp( player_model_final_rot,
Quat::from_euler(EulerRot::XYZ, eulers.0, eulers.1, eulers.2),
time.delta_seconds() / player_values_state.player_lean_time, time.delta_seconds() / player_values_state.player_lean_time,
).to_euler(EulerRot::XYZ); )
new_rot.1 = eulers.1;
player_transform.rotation = Quat::from_euler(EulerRot::XYZ, new_rot.0, new_rot.1, new_rot.2);
} else { } else {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ); player_model_transform.rotation = player_model_transform.rotation.lerp(
let mut new_rot = player_transform.rotation.lerp( player_model_default_rot,
Quat::from_rotation_z(0.0),
time.delta_seconds() / player_values_state.player_lean_time, time.delta_seconds() / player_values_state.player_lean_time,
).to_euler(EulerRot::XYZ); )
new_rot.1 = eulers.1;
player_transform.rotation = Quat::from_euler(EulerRot::XYZ, new_rot.0, new_rot.1, new_rot.2);
} }
} }
}
} }
} }
} else { } else {