Added depth to jumping, inertia stays mid air but right before landing damping is restored

This commit is contained in:
Franklin 2023-09-12 13:08:00 -04:00
parent c693d698a6
commit c31a84ea61
5 changed files with 47 additions and 20 deletions

View File

@ -17,6 +17,7 @@ pub fn capture_input(
&mut PlayerLinearYState, &mut PlayerLinearYState,
&mut PlayerLinearXZState, &mut PlayerLinearXZState,
&Transform, &Transform,
&mut Damping,
), ),
With<Player>, With<Player>,
>, >,

View File

@ -1,14 +1,21 @@
pub const MAX_LINEAR_PLAYER_VELOCITY: f32 = 10.0; pub const MAX_LINEAR_PLAYER_VELOCITY: f32 = 20.0;
pub const PLAYER_ACCELERATION: f32 = 12.0; pub const PLAYER_ACCELERATION: f32 = 20.0;
pub const PLAYER_JUMP_FORCE: f32 = 5000.0; pub const PLAYER_JUMP_FORCE: f32 = 900.0;
/// Time in ms that player must be grounded in order to jump again /// Time in ms that player must be grounded in order to jump again
pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 75; pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 75;
pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.0; pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.0;
pub const PLAYER_CROUCH_SPEED_MULTIPLIER: f32 = 0.25; pub const PLAYER_CROUCH_SPEED_MULTIPLIER: f32 = 0.25;
pub const PLAYER_INITIAL_WEIGHT: f32 = 75.0;
pub const PLAYER_INITIAL_WEIGHT: f32 = 350.0; pub const PLAYER_GRAVITY_SCALE: f32 = 4.0;
pub const PLAYER_HEIGHT: f32 = 2.0; pub const PLAYER_HEIGHT: f32 = 2.0;
pub const PLAYER_CAMERA_HEIGHT: f32 = 0.5; pub const PLAYER_CAMERA_HEIGHT: f32 = 1.0;
pub const PLAYER_CROUCH_HEIGHT: f32 = 0.0; pub const PLAYER_CROUCH_HEIGHT: f32 = 0.0;
pub const PLAYER_LINEAR_DAMPING: f32 = 3.5;
pub const PLAYER_LINEAR_DAMPING_WHILE_JUMPING: f32 = -0.5;
pub const PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER: f32 = 0.2;
pub const PLAYER_LATERAL_ACCELERATION_MULTIPLIER: f32 = 1.0;
pub const PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS: u128 = 20;

View File

@ -5,7 +5,7 @@ use crate::{
comps::core::markers::player::Player, comps::core::markers::player::Player,
constants::player_values::{ constants::player_values::{
MAX_LINEAR_PLAYER_VELOCITY, PLAYER_ACCELERATION, PLAYER_CROUCH_SPEED_MULTIPLIER, MAX_LINEAR_PLAYER_VELOCITY, PLAYER_ACCELERATION, PLAYER_CROUCH_SPEED_MULTIPLIER,
PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE, PLAYER_SPRINT_SPEED_MULTIPLIER, PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE, PLAYER_SPRINT_SPEED_MULTIPLIER, PLAYER_LINEAR_DAMPING_WHILE_JUMPING, PLAYER_LATERAL_ACCELERATION_MULTIPLIER, PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER,
}, },
}; };
@ -54,6 +54,12 @@ impl PlayerLinearXZState {
_ => false, _ => false,
} }
} }
pub fn is_sprinting(&self) -> bool {
match self {
Self::Sprinting => true,
_ => false,
}
}
} }
/// Holds all the possible ways a player can be attempting to move at any time. /// Holds all the possible ways a player can be attempting to move at any time.
@ -87,6 +93,7 @@ pub fn move_player(
&mut PlayerLinearYState, &mut PlayerLinearYState,
&mut PlayerLinearXZState, &mut PlayerLinearXZState,
&Transform, &Transform,
&mut Damping,
), ),
With<Player>, With<Player>,
>, >,
@ -98,9 +105,9 @@ pub fn move_player(
mut player_linear_y_state, mut player_linear_y_state,
mut player_linear_xz_state, mut player_linear_xz_state,
player_transform, player_transform,
mut player_damping
) in &mut query ) in &mut query
{ {
println!("{:?}", player_linear_xz_state);
let crouch_multiplier = if player_movement_input.down { let crouch_multiplier = if player_movement_input.down {
*player_linear_xz_state = PlayerLinearXZState::Crouched; *player_linear_xz_state = PlayerLinearXZState::Crouched;
PLAYER_CROUCH_SPEED_MULTIPLIER PLAYER_CROUCH_SPEED_MULTIPLIER
@ -155,7 +162,11 @@ pub fn move_player(
right, right,
player_velocity.linvel, player_velocity.linvel,
time.delta_seconds(), time.delta_seconds(),
1.0, if player_linear_xz_state.is_sprinting() {
PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER
} else {
PLAYER_LATERAL_ACCELERATION_MULTIPLIER
},
); );
} }
if player_movement_input.left { if player_movement_input.left {
@ -168,7 +179,11 @@ pub fn move_player(
-right, -right,
player_velocity.linvel, player_velocity.linvel,
time.delta_seconds(), time.delta_seconds(),
1.0, if player_linear_xz_state.is_sprinting() {
PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER
} else {
PLAYER_LATERAL_ACCELERATION_MULTIPLIER
},
); );
} }
} }
@ -176,6 +191,7 @@ pub fn move_player(
if player_movement_input.up && player_linear_y_state.is_grounded(&PLAYER_JUMP_COOLDOWN_MS) { if player_movement_input.up && player_linear_y_state.is_grounded(&PLAYER_JUMP_COOLDOWN_MS) {
player_external_force.impulse = Vec3::new(0.0, PLAYER_JUMP_FORCE, 0.0); player_external_force.impulse = Vec3::new(0.0, PLAYER_JUMP_FORCE, 0.0);
*player_linear_y_state = PlayerLinearYState::Jumping; *player_linear_y_state = PlayerLinearYState::Jumping;
*player_damping = Damping { linear_damping: PLAYER_LINEAR_DAMPING_WHILE_JUMPING, ..Default::default()};
} }
// When player velocity exceeds max linear velocity then set to max_linear_vel value // When player velocity exceeds max linear velocity then set to max_linear_vel value
if player_velocity.linvel.x.abs() if player_velocity.linvel.x.abs()

View File

@ -1,16 +1,16 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::comps::core::markers::player::Player; use crate::{comps::core::markers::player::Player, constants::player_values::{PLAYER_LINEAR_DAMPING, PLAYER_JUMP_COOLDOWN_MS, PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS}};
use super::player_movement::PlayerLinearYState; use super::player_movement::PlayerLinearYState;
/// System that captures linear y velocity and determines whether player is falling or grounded, and how long the player has been grounded for. /// System that captures linear y velocity and determines whether player is falling or grounded, and how long the player has been grounded for.
pub fn sync_player_y_state( pub fn sync_player_y_state(
mut query: Query<(&Velocity, &mut PlayerLinearYState), With<Player>>, mut query: Query<(&Velocity, &mut PlayerLinearYState, &mut Damping), With<Player>>,
time: Res<Time>, time: Res<Time>,
) { ) {
for (player_velocity, mut player_linear_y_state) in &mut query { for (player_velocity, mut player_linear_y_state, mut player_damping) in &mut query {
if player_velocity.linvel.y < -1.0 { if player_velocity.linvel.y < -1.0 {
*player_linear_y_state = PlayerLinearYState::Falling; *player_linear_y_state = PlayerLinearYState::Falling;
} else if player_velocity.linvel.y >= -1.0 && player_velocity.linvel.y <= 1.0 { } else if player_velocity.linvel.y >= -1.0 && player_velocity.linvel.y <= 1.0 {
@ -18,8 +18,11 @@ pub fn sync_player_y_state(
PlayerLinearYState::Grounded(grounded_for) => grounded_for, PlayerLinearYState::Grounded(grounded_for) => grounded_for,
_ => 0, _ => 0,
}; };
*player_linear_y_state = let new_grounded_time = previous_grounded_time + time.delta().as_millis();
PlayerLinearYState::Grounded(previous_grounded_time + time.delta().as_millis()); *player_linear_y_state = PlayerLinearYState::Grounded(new_grounded_time);
if new_grounded_time > PLAYER_JUMP_COOLDOWN_MS - PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS {
*player_damping = Damping { linear_damping: PLAYER_LINEAR_DAMPING, ..Default::default()};
}
} }
} }
} }

View File

@ -3,7 +3,7 @@ use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
comps::core::{camera::MainCamera, markers::player::Player}, comps::core::{camera::MainCamera, markers::player::Player},
constants::player_values::{PLAYER_HEIGHT, PLAYER_INITIAL_WEIGHT}, constants::player_values::{PLAYER_HEIGHT, PLAYER_INITIAL_WEIGHT, PLAYER_GRAVITY_SCALE, PLAYER_LINEAR_DAMPING},
}; };
use super::player_movement::{PlayerLinearXZState, PlayerLinearYState}; use super::player_movement::{PlayerLinearXZState, PlayerLinearYState};
@ -12,12 +12,12 @@ pub fn spawn_player(mut commands: Commands) {
commands commands
.spawn(Player) .spawn(Player)
.insert(RigidBody::Dynamic) .insert(RigidBody::Dynamic)
.insert(GravityScale(2.0)) .insert(GravityScale(PLAYER_GRAVITY_SCALE))
.insert(Collider::capsule_y(PLAYER_HEIGHT, 2.0)) .insert(Collider::capsule_y(PLAYER_HEIGHT, 2.0))
.insert(Restitution::coefficient(0.0)) .insert(Restitution::coefficient(0.0))
.insert(Friction { .insert(Friction {
coefficient: 0.0, coefficient: 0.0,
..Default::default() combine_rule: CoefficientCombineRule::Multiply
}) })
.insert(TransformBundle { .insert(TransformBundle {
local: Transform::from_xyz(3.0, 5.0, 2.0), local: Transform::from_xyz(3.0, 5.0, 2.0),
@ -25,7 +25,7 @@ pub fn spawn_player(mut commands: Commands) {
}) })
.insert(Velocity::zero()) .insert(Velocity::zero())
.insert(Damping { .insert(Damping {
linear_damping: 1.0, linear_damping: PLAYER_LINEAR_DAMPING,
angular_damping: 1.0, angular_damping: 1.0,
}) })
.insert( .insert(