mouse look and walking in same entity

This commit is contained in:
Felix Ableitner 2021-01-02 02:55:47 +01:00
parent f85b5cf7af
commit f2cb9f0cae

View file

@ -1,4 +1,8 @@
use bevy::{input::mouse::MouseMotion, prelude::*}; use bevy::{
input::mouse::MouseMotion,
prelude::*,
render::camera::{Camera, PerspectiveProjection, VisibleEntities},
};
use bevy_rapier3d::{ use bevy_rapier3d::{
na::Matrix3x1, na::Matrix3x1,
physics::RigidBodyHandleComponent, physics::RigidBodyHandleComponent,
@ -26,62 +30,77 @@ struct State {
} }
#[derive(Default)] #[derive(Default)]
struct Player; struct PlayerMarker;
#[derive(Bundle)]
pub struct PlayerBundle {
pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform,
pub global_transform: GlobalTransform,
player_marker: PlayerMarker,
rigid_body: RigidBodyBuilder,
collider: ColliderBuilder,
}
impl Default for PlayerBundle {
fn default() -> Self {
PlayerBundle {
rigid_body: RigidBodyBuilder::new_dynamic()
.translation(0.0, 3.0, 0.0)
.lock_rotations(),
collider: ColliderBuilder::cylinder(PLAYER_HEIGHT / 2., PLAYER_WIDTH).friction(0.0),
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
player_marker: Default::default(),
}
}
}
const PLAYER_SPEED: f32 = 320.0; const PLAYER_SPEED: f32 = 320.0;
const MOUSE_SENSITIVITY: f32 = 1.0; const MOUSE_SENSITIVITY: f32 = 1.0;
const PLAYER_HEIGHT: f32 = 1.8;
const PLAYER_WIDTH: f32 = 0.25;
fn init_player( fn init_player(commands: &mut Commands) {
commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands commands
.spawn(Camera3dBundle { .spawn(PlayerBundle::default())
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 3.0)), .with_children(|parent| {
..Default::default() parent.spawn(Camera3dBundle {
}) transform: Transform::from_translation(Vec3::new(0.0, PLAYER_HEIGHT / 2., 0.0)),
.with(Player::default()); ..Default::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)
.lock_rotations(),
)
.with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0));
} }
fn player_movement_system( fn player_movement_system(
time: Res<Time>, time: Res<Time>,
mut bodies: ResMut<RigidBodySet>, mut bodies: ResMut<RigidBodySet>,
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Transform, &RigidBodyHandleComponent), With<Player>>, // TODO: need to use better queries which filters by parent.player_marker
camera_transform_query: Query<&Transform, With<Camera>>,
mut player_query: Query<&RigidBodyHandleComponent, With<PlayerMarker>>,
) { ) {
for (transform, handle) in query.iter_mut() { for camera_transform in camera_transform_query.iter() {
let body: &mut RigidBody = bodies.get_mut(handle.handle()).unwrap(); for body_handle in player_query.iter_mut() {
let axis_straight = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A); let body: &mut RigidBody = bodies.get_mut(body_handle.handle()).unwrap();
let axis_strafe = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W); let axis_straight = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A);
let axis_strafe = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W);
let rotation = transform.rotation; let rotation = camera_transform.rotation;
let mut speed_2d: Vec3 = let mut speed_2d: Vec3 =
(strafe_vector(&rotation) * axis_straight) + (forward_vector(&rotation) * axis_strafe); (strafe_vector(&rotation) * axis_straight) + (forward_vector(&rotation) * axis_strafe);
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;
}
let velocity_2d = speed_2d * time.delta_seconds();
let velocity_3d = Matrix3x1::new(velocity_2d.x, body.linvel().y, velocity_2d.z);
body.set_linvel(velocity_3d, true);
} }
let velocity_2d = speed_2d * time.delta_seconds();
let velocity_3d = Matrix3x1::new(velocity_2d.x, body.linvel().y, velocity_2d.z);
body.set_linvel(velocity_3d, true);
} }
} }
@ -113,7 +132,7 @@ fn player_look_system(
time: Res<Time>, time: Res<Time>,
mut state: ResMut<State>, mut state: ResMut<State>,
mouse_motion_events: Res<Events<MouseMotion>>, mouse_motion_events: Res<Events<MouseMotion>>,
mut query: Query<&mut Transform, With<Player>>, mut query: Query<&mut Transform, With<Camera>>,
) { ) {
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) {