Implement basic jumping

This commit is contained in:
Felix Ableitner 2021-01-03 17:16:39 +01:00
parent 8cda16d117
commit 001ce7e98c

View file

@ -1,13 +1,10 @@
use bevy::{
input::mouse::MouseMotion,
prelude::*,
};
use bevy::{input::mouse::MouseMotion, prelude::*};
use bevy_rapier3d::{
na::{clamp, wrap, Matrix3x1},
physics::RigidBodyHandleComponent,
physics::{ColliderHandleComponent, RigidBodyHandleComponent},
rapier::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodySet},
geometry::ColliderBuilder,
geometry::{ColliderBuilder, NarrowPhase},
},
};
use std::f32::consts::{FRAC_PI_2, PI};
@ -73,16 +70,40 @@ fn init_player(commands: &mut Commands) {
});
}
// TODO: this is incomplete, currently just checks that the player is touching anything
fn is_on_ground(
narrow_phase: &Res<NarrowPhase>,
collider_handle: &ColliderHandleComponent,
) -> bool {
if let Some(n1) = narrow_phase.contacts_with(collider_handle.handle()) {
for _n2 in n1 {
return true;
}
}
return false;
}
fn player_movement_system(
time: Res<Time>,
mut bodies: ResMut<RigidBodySet>,
mut rigid_body_set: ResMut<RigidBodySet>,
keyboard_input: Res<Input<KeyCode>>,
narrow_phase: Res<NarrowPhase>,
camera_transform_query: Query<&Transform, With<CameraData>>,
mut player_query: Query<&RigidBodyHandleComponent, With<PlayerMarker>>,
mut player_physics_query: Query<
(&RigidBodyHandleComponent, &ColliderHandleComponent),
With<PlayerMarker>,
>,
) {
for camera_transform in camera_transform_query.iter() {
for body_handle in player_query.iter_mut() {
let body: &mut RigidBody = bodies.get_mut(body_handle.handle()).unwrap();
for (body_handle, collider_handle) in player_physics_query.iter_mut() {
let is_on_ground = is_on_ground(&narrow_phase, collider_handle);
if !is_on_ground {
continue;
}
let body: &mut RigidBody = rigid_body_set.get_mut(body_handle.handle()).unwrap();
if keyboard_input.pressed(KeyCode::Space) {
body.apply_impulse(Matrix3x1::new(0.0, 0.5, 0.0), true);
}
let axis_strafe = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A);
let axis_straight = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W);