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 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},
};
@ -72,6 +72,7 @@ pub fn follow_cursor_with_camera(
mut motions: EventReader<MouseMotion>,
mut player_query: Query<&mut Transform, (With<Player>, Without<MainCamera>)>,
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>>,
btn: Res<Input<MouseButton>>,
time: Res<Time>,
@ -123,67 +124,62 @@ pub fn follow_cursor_with_camera(
if window.cursor.grab_mode != CursorGrabMode::None {
for mut player_transform in player_query.iter_mut() {
for mut camera_transform in camera_query.iter_mut() {
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() {
let window_scale = window.height().min(window.width());
if btn.pressed(MouseButton::Right) {
pitch -= ((settings.aimed_sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -= ((settings.aimed_sensitivity
* motion.delta.x as f64
* window_scale as f64) as f32)
.to_radians();
} else {
pitch -= ((settings.sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -=
((settings.sensitivity * motion.delta.x as f64 * window_scale as f64)
as f32)
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) ;
for motion in motions.read() {
let window_scale = window.height().min(window.width());
if btn.pressed(MouseButton::Right) {
pitch -= ((settings.aimed_sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -= ((settings.aimed_sensitivity
* motion.delta.x as f64
* window_scale as f64) as f32)
.to_radians();
} else {
pitch -= ((settings.sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -=
((settings.sensitivity * motion.delta.x as f64 * window_scale as f64)
as f32)
.to_radians();
}
}
pitch = pitch.clamp(-1.54, 1.54);
let desired_rotation_quat_camera =
Quat::from_axis_angle(Vec3::X, pitch);
let desired_rotation_quat_player = Quat::from_axis_angle(Vec3::Y, yaw);
//let local_z = camera_transform.local_z();
//let right = Vec3::new(local_z.z, camera_transform.translation.y, -local_z.x);
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) {
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());
player_model_transform.rotation = player_model_transform.rotation.lerp(
player_model_final_rot,
time.delta_seconds() / player_values_state.player_lean_time,
)
} else if keyboard_input.pressed(KeyCode::E) {
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());
player_model_transform.rotation = player_model_transform.rotation.lerp(
player_model_final_rot,
time.delta_seconds() / player_values_state.player_lean_time,
)
} else {
player_model_transform.rotation = player_model_transform.rotation.lerp(
player_model_default_rot,
time.delta_seconds() / player_values_state.player_lean_time,
)
}
}
pitch = pitch.clamp(-1.54, 1.54);
let desired_rotation_quat_camera =
Quat::from_axis_angle(Vec3::X, pitch);
let desired_rotation_quat_player = Quat::from_axis_angle(Vec3::Y, yaw) * -1. ;
//let local_z = camera_transform.local_z();
//let right = Vec3::new(local_z.z, camera_transform.translation.y, -local_z.x);
camera_transform.rotation = desired_rotation_quat_camera;
if keyboard_input.pressed(KeyCode::Q) {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ);
eulers.2 = eulers.2 + player_values_state.player_lean_angle.to_radians();
let mut new_rot = player_transform.rotation.lerp(
Quat::from_euler(EulerRot::XYZ, eulers.0, eulers.1, eulers.2),
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) {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ);
eulers.2 = eulers.2 + (player_values_state.player_lean_angle.to_radians() * -1.0);
let mut new_rot = player_transform.rotation.lerp(
Quat::from_euler(EulerRot::XYZ, eulers.0, eulers.1, eulers.2),
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 {
let mut eulers = desired_rotation_quat_player.to_euler(EulerRot::XYZ);
let mut new_rot = player_transform.rotation.lerp(
Quat::from_rotation_z(0.0),
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);
}
}
}
}