Improved camera control
This commit is contained in:
parent
9875417232
commit
f85b5cf7af
1 changed files with 30 additions and 36 deletions
|
@ -1,4 +1,4 @@
|
|||
use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*};
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::{
|
||||
na::Matrix3x1,
|
||||
physics::RigidBodyHandleComponent,
|
||||
|
@ -25,32 +25,33 @@ struct State {
|
|||
mouse_motion_event_reader: EventReader<MouseMotion>,
|
||||
}
|
||||
|
||||
struct Player {
|
||||
/// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
|
||||
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,
|
||||
}
|
||||
#[derive(Default)]
|
||||
struct Player;
|
||||
|
||||
const PLAYER_SPEED: f32 = 320.0;
|
||||
const MOUSE_SENSITIVITY: f32 = 6.0;
|
||||
const MOUSE_SENSITIVITY: f32 = 1.0;
|
||||
|
||||
impl Player {
|
||||
fn create() -> Self {
|
||||
Self {
|
||||
pitch: 0.0,
|
||||
yaw: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn init_player(commands: &mut Commands) {
|
||||
fn init_player(
|
||||
commands: &mut Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
commands
|
||||
.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()
|
||||
})
|
||||
.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(
|
||||
RigidBodyBuilder::new_dynamic()
|
||||
.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(
|
||||
time: Res<Time>,
|
||||
mut state: ResMut<State>,
|
||||
mut bodies: ResMut<RigidBodySet>,
|
||||
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();
|
||||
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
|
||||
|
@ -123,21 +123,15 @@ fn player_look_system(
|
|||
return;
|
||||
}
|
||||
|
||||
for (mut player, mut transform, handle) in query.iter_mut() {
|
||||
let body: &mut RigidBody = bodies.get_mut(handle.handle()).unwrap();
|
||||
player.yaw -= delta.x * MOUSE_SENSITIVITY * time.delta_seconds();
|
||||
player.pitch += delta.y * MOUSE_SENSITIVITY * time.delta_seconds();
|
||||
// TODO: need to keep the camera level
|
||||
for mut transform in query.iter_mut() {
|
||||
let yaw = -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);
|
||||
// println!("pitch: {}, yaw: {}", options.pitch, options.yaw);
|
||||
let quat_yaw = Quat::from_axis_angle(Vec3::unit_x(), yaw);
|
||||
let quat_pitch = Quat::from_axis_angle(Vec3::unit_y(), pitch);
|
||||
|
||||
let yaw_radians = player.yaw.to_radians();
|
||||
let pitch_radians = player.pitch.to_radians();
|
||||
|
||||
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);
|
||||
let orientation_change: Quat = (quat_pitch * quat_yaw).normalize();
|
||||
transform.rotate(orientation_change);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue