From e31a473b3ee6da77d500f6f380eb1c4369eb0782 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 2 Jan 2021 19:26:37 +0100 Subject: [PATCH] Fix camera getting uneven, clamp top/bottom rotation --- src/player.rs | 57 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/player.rs b/src/player.rs index e5dc46d..1b77736 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,16 +1,17 @@ use bevy::{ input::mouse::MouseMotion, prelude::*, - render::camera::{Camera, PerspectiveProjection, VisibleEntities}, + render::camera::{PerspectiveProjection, VisibleEntities}, }; use bevy_rapier3d::{ - na::Matrix3x1, + na::{clamp, wrap, Matrix3x1}, physics::RigidBodyHandleComponent, rapier::{ dynamics::{RigidBody, RigidBodyBuilder, RigidBodySet}, geometry::ColliderBuilder, }, }; +use std::f32::consts::{FRAC_PI_2, PI}; pub struct PlayerPlugin; @@ -32,6 +33,12 @@ struct State { #[derive(Default)] struct PlayerMarker; +#[derive(Default, Debug)] +struct CameraData { + yaw: f32, + pitch: f32, +} + #[derive(Bundle)] pub struct PlayerBundle { pub perspective_projection: PerspectiveProjection, @@ -60,7 +67,7 @@ impl Default for PlayerBundle { } const PLAYER_SPEED: f32 = 320.0; -const MOUSE_SENSITIVITY: f32 = 1.0; +const MOUSE_SENSITIVITY: f32 = 0.5; const PLAYER_HEIGHT: f32 = 1.8; const PLAYER_WIDTH: f32 = 0.25; @@ -68,10 +75,12 @@ fn init_player(commands: &mut Commands) { commands .spawn(PlayerBundle::default()) .with_children(|parent| { - parent.spawn(Camera3dBundle { - transform: Transform::from_translation(Vec3::new(0.0, PLAYER_HEIGHT / 2., 0.0)), - ..Default::default() - }); + parent + .spawn(Camera3dBundle { + transform: Transform::from_translation(Vec3::new(0.0, PLAYER_HEIGHT / 2., 0.0)), + ..Default::default() + }) + .with(CameraData::default()); }); } @@ -79,19 +88,18 @@ fn player_movement_system( time: Res