Fix movement getting slower when looking up/down

This commit is contained in:
Felix Ableitner 2021-01-02 20:04:05 +01:00
parent e31a473b3e
commit b38d86f54f

View file

@ -1,7 +1,6 @@
use bevy::{ use bevy::{
input::mouse::MouseMotion, input::mouse::MouseMotion,
prelude::*, prelude::*,
render::camera::{PerspectiveProjection, VisibleEntities},
}; };
use bevy_rapier3d::{ use bevy_rapier3d::{
na::{clamp, wrap, Matrix3x1}, na::{clamp, wrap, Matrix3x1},
@ -41,8 +40,6 @@ struct CameraData {
#[derive(Bundle)] #[derive(Bundle)]
pub struct PlayerBundle { pub struct PlayerBundle {
pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform, pub transform: Transform,
pub global_transform: GlobalTransform, pub global_transform: GlobalTransform,
player_marker: PlayerMarker, player_marker: PlayerMarker,
@ -57,8 +54,6 @@ impl Default for PlayerBundle {
.translation(0.0, 3.0, 0.0) .translation(0.0, 3.0, 0.0)
.lock_rotations(), .lock_rotations(),
collider: ColliderBuilder::cylinder(PLAYER_HEIGHT / 2., PLAYER_WIDTH).friction(0.0), collider: ColliderBuilder::cylinder(PLAYER_HEIGHT / 2., PLAYER_WIDTH).friction(0.0),
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(), transform: Default::default(),
global_transform: Default::default(), global_transform: Default::default(),
player_marker: Default::default(), player_marker: Default::default(),
@ -94,12 +89,13 @@ fn player_movement_system(
for camera_transform in camera_transform_query.iter() { for camera_transform in camera_transform_query.iter() {
for body_handle in player_query.iter_mut() { for body_handle in player_query.iter_mut() {
let body: &mut RigidBody = bodies.get_mut(body_handle.handle()).unwrap(); let body: &mut RigidBody = bodies.get_mut(body_handle.handle()).unwrap();
let axis_strafe = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A); let axis_strafe = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A);
let axis_straight = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W); let axis_straight = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W);
let rotation = camera_transform.rotation; let rotation = camera_transform.rotation;
let mut speed_2d: Vec3 = let mut speed_2d: Vec3 =
(strafe_vector(&rotation) * axis_strafe) + (forward_vector(&rotation) * axis_straight); (strafe_vector(&rotation) * axis_strafe) + (straight_vector(&rotation) * axis_straight);
if speed_2d.length() != 0.0 { if speed_2d.length() != 0.0 {
speed_2d = speed_2d.normalize() * PLAYER_SPEED; speed_2d = speed_2d.normalize() * PLAYER_SPEED;
} }
@ -112,16 +108,15 @@ fn player_movement_system(
} }
} }
fn forward_vector(rotation: &Quat) -> Vec3 { fn straight_vector(rotation: &Quat) -> Vec3 {
rotation.mul_vec3(Vec3::unit_z()).normalize() let v = rotation.mul_vec3(Vec3::unit_z());
Vec3::new(v.x, 0.0, v.z).normalize()
} }
fn strafe_vector(rotation: &Quat) -> Vec3 { fn strafe_vector(rotation: &Quat) -> Vec3 {
let f = forward_vector(rotation); let forward = straight_vector(rotation);
let f_flattened = Vec3::new(f.x, 0.0, f.z).normalize(); Quat::from_rotation_y(FRAC_PI_2)
// Rotate it 90 degrees to get the strafe direction .mul_vec3(forward)
Quat::from_rotation_y(90.0f32.to_radians())
.mul_vec3(f_flattened)
.normalize() .normalize()
} }