Improved camera control

This commit is contained in:
Felix Ableitner 2021-01-01 18:31:38 +01:00
parent 9875417232
commit f85b5cf7af

View file

@ -1,4 +1,4 @@
use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*}; use bevy::{input::mouse::MouseMotion, prelude::*};
use bevy_rapier3d::{ use bevy_rapier3d::{
na::Matrix3x1, na::Matrix3x1,
physics::RigidBodyHandleComponent, physics::RigidBodyHandleComponent,
@ -25,32 +25,33 @@ struct State {
mouse_motion_event_reader: EventReader<MouseMotion>, mouse_motion_event_reader: EventReader<MouseMotion>,
} }
struct Player { #[derive(Default)]
/// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) struct Player;
pitch: f32,
/// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
yaw: f32,
}
const PLAYER_SPEED: f32 = 320.0; const PLAYER_SPEED: f32 = 320.0;
const MOUSE_SENSITIVITY: f32 = 6.0; const MOUSE_SENSITIVITY: f32 = 1.0;
impl Player { fn init_player(
fn create() -> Self { commands: &mut Commands,
Self { mut meshes: ResMut<Assets<Mesh>>,
pitch: 0.0, mut materials: ResMut<Assets<StandardMaterial>>,
yaw: 0.0, ) {
}
}
}
fn init_player(commands: &mut Commands) {
commands commands
.spawn(Camera3dBundle { .spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)), transform: Transform::from_translation(Vec3::new(0.0, 1.0, 3.0)),
..Default::default() ..Default::default()
}) })
.with(Player::create()) .with(Player::default());
// TODO: temporarily create a separate bundle for movement, to test mouse look
commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.25 })),
material: materials.add(Color::rgb(0.0, 1.0, 0.3).into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
})
.with(Player::default())
.with( .with(
RigidBodyBuilder::new_dynamic() RigidBodyBuilder::new_dynamic()
.translation(0.0, 3.0, 0.0) .translation(0.0, 3.0, 0.0)
@ -111,9 +112,8 @@ fn movement_axis(input: &Res<Input<KeyCode>>, plus: KeyCode, minus: KeyCode) ->
fn player_look_system( fn player_look_system(
time: Res<Time>, time: Res<Time>,
mut state: ResMut<State>, mut state: ResMut<State>,
mut bodies: ResMut<RigidBodySet>,
mouse_motion_events: Res<Events<MouseMotion>>, mouse_motion_events: Res<Events<MouseMotion>>,
mut query: Query<(&mut Player, &mut Transform, &RigidBodyHandleComponent)>, mut query: Query<&mut Transform, With<Player>>,
) { ) {
let mut delta: Vec2 = Vec2::zero(); let mut delta: Vec2 = Vec2::zero();
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) { for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
@ -123,21 +123,15 @@ fn player_look_system(
return; return;
} }
for (mut player, mut transform, handle) in query.iter_mut() { // TODO: need to keep the camera level
let body: &mut RigidBody = bodies.get_mut(handle.handle()).unwrap(); for mut transform in query.iter_mut() {
player.yaw -= delta.x * MOUSE_SENSITIVITY * time.delta_seconds(); let yaw = -delta.y * MOUSE_SENSITIVITY * time.delta_seconds();
player.pitch += delta.y * MOUSE_SENSITIVITY * time.delta_seconds(); let pitch = -delta.x * MOUSE_SENSITIVITY * time.delta_seconds();
player.pitch = clamp(player.pitch, -89.9, 89.9); let quat_yaw = Quat::from_axis_angle(Vec3::unit_x(), yaw);
// println!("pitch: {}, yaw: {}", options.pitch, options.yaw); let quat_pitch = Quat::from_axis_angle(Vec3::unit_y(), pitch);
let yaw_radians = player.yaw.to_radians(); let orientation_change: Quat = (quat_pitch * quat_yaw).normalize();
let pitch_radians = player.pitch.to_radians(); transform.rotate(orientation_change);
transform.rotation = Quat::from_axis_angle(Vec3::unit_y(), yaw_radians)
* Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians)
* 10.;
body.set_angvel(Matrix3x1::new(pitch_radians, yaw_radians, 0.), true);
} }
} }