Tweaked acceleration, speed, weight, gravity values and added momentum and inertia to jumps

This commit is contained in:
Franklin 2023-09-12 12:03:32 -04:00
parent 146be24a7c
commit c15592ec3f
6 changed files with 79 additions and 26 deletions

View File

@ -1,6 +1,11 @@
pub const MAX_LINEAR_PLAYER_VELOCITY: f32 = 10.0;
pub const PLAYER_ACCELERATION: f32 = 10.0;
pub const PLAYER_JUMP_FORCE: f32 = 1500.0;
pub const PLAYER_ACCELERATION: f32 = 12.0;
pub const PLAYER_JUMP_FORCE: f32 = 2500.0;
/// Time in ms that player must be grounded in order to jump again
pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 100;
pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 75;
pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.0;
pub const PLAYER_CROUCH_SPEED_MULTIPLIER: f32 = 0.25;
pub const PLAYER_INITIAL_WEIGHT: f32 = 350.0;

View File

View File

@ -1,4 +1,5 @@
pub mod player_movement;
pub mod spawn_player;
pub mod camera_player_sync;
pub mod player_vertical_sync;
pub mod player_vertical_sync;
pub mod camera_effects;

View File

@ -1,7 +1,7 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::{comps::core::markers::player::Player, constants::player_values::{MAX_LINEAR_PLAYER_VELOCITY, PLAYER_ACCELERATION, PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE}};
use crate::{comps::core::markers::player::Player, constants::player_values::{MAX_LINEAR_PLAYER_VELOCITY, PLAYER_ACCELERATION, PLAYER_JUMP_COOLDOWN_MS, PLAYER_JUMP_FORCE, PLAYER_CROUCH_SPEED_MULTIPLIER, PLAYER_SPRINT_SPEED_MULTIPLIER}};
#[derive(Component)]
pub enum PlayerLinearYState {
@ -59,31 +59,33 @@ pub struct PlayerMovementInput {
pub fn move_player(player_movement_input: PlayerMovementInput, mut query: Query<(&mut Velocity, &mut ExternalImpulse, &mut PlayerLinearYState, &Transform), With<Player>>, time: Res<Time>,) {
for (mut player_velocity, mut player_external_force, mut player_linear_y_state, player_transform) in &mut query {
let crouch_multiplier = if player_movement_input.down {
0.25
PLAYER_CROUCH_SPEED_MULTIPLIER
} else {
1.0
};
let sprint_multiplier = if player_movement_input.sprint && !player_movement_input.down {
2.5
PLAYER_SPRINT_SPEED_MULTIPLIER
} else {
1.0
};
let local_z = player_transform.local_z();
let forward = -Vec3::new(local_z.x, 0.0, local_z.z);
let right = Vec3::new(local_z.z, 0.0, -local_z.x);
if player_movement_input.front {
player_velocity.linvel = apply_movement_acceleration_to_vec(forward, player_velocity.linvel, time.delta_seconds(), sprint_multiplier);
if !player_linear_y_state.is_jumping() && !player_linear_y_state.is_falling() { // Only let player move when grounded
if player_movement_input.front {
player_velocity.linvel = apply_movement_acceleration_to_vec(forward, player_velocity.linvel, time.delta_seconds(), sprint_multiplier);
}
if player_movement_input.back {
player_velocity.linvel = apply_movement_acceleration_to_vec(-forward, player_velocity.linvel, time.delta_seconds(), 1.0);
}
if player_movement_input.right {
player_velocity.linvel = apply_movement_acceleration_to_vec(right, player_velocity.linvel, time.delta_seconds(), 1.0);
}
if player_movement_input.left {
player_velocity.linvel = apply_movement_acceleration_to_vec(-right, player_velocity.linvel, time.delta_seconds(), 1.0);
}
}
if player_movement_input.back {
player_velocity.linvel = apply_movement_acceleration_to_vec(-forward, player_velocity.linvel, time.delta_seconds(), 1.0);
}
if player_movement_input.right {
player_velocity.linvel = apply_movement_acceleration_to_vec(right, player_velocity.linvel, time.delta_seconds(), 1.0);
}
if player_movement_input.left {
player_velocity.linvel = apply_movement_acceleration_to_vec(-right, player_velocity.linvel, time.delta_seconds(), 1.0);
}
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_linear_y_state = PlayerLinearYState::Jumping;

View File

@ -1,22 +1,22 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::comps::core::{markers::player::Player, camera::MainCamera};
use crate::{comps::core::{markers::player::Player, camera::MainCamera}, constants::player_values::PLAYER_INITIAL_WEIGHT};
use super::player_movement::PlayerLinearYState;
pub fn spawn_player(mut commands: Commands) {
commands.spawn(Player)
.insert(RigidBody::Dynamic)
.insert(GravityScale(2.5))
.insert(GravityScale(2.0))
.insert(Collider::capsule_y(2.0, 2.0))
.insert(Restitution::coefficient(0.0))
.insert(Friction { coefficient: 0.0, ..Default::default() })
.insert(TransformBundle{ local: Transform::from_xyz(3.0, 5.0, 2.0), global: Default::default() })
.insert(Velocity::zero())
.insert(Damping { linear_damping: 1.0, angular_damping: 1.0 })
.insert(LockedAxes::ROTATION_LOCKED_Z | LockedAxes::ROTATION_LOCKED_X )
.insert(ColliderMassProperties::Density(2.0))
.insert(LockedAxes::ROTATION_LOCKED_Z | LockedAxes::ROTATION_LOCKED_X | LockedAxes::ROTATION_LOCKED_Y)
.insert(ColliderMassProperties::Mass(PLAYER_INITIAL_WEIGHT))
.insert(ExternalImpulse {
impulse: Vec3::ZERO,
torque_impulse: Vec3::ZERO,

View File

@ -3,12 +3,15 @@ use bevy_rapier3d::prelude::*;
pub fn spawn_obstacles(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,) {
let mesh = shape::Box::new(3.0, 7.0, 3.0).into();
let box_1_mesh = shape::Box::new(3.0, 7.0, 3.0).into();
let box_2_mesh = shape::Box::new(3.0, 7.0, 3.0).into();
let box_3_mesh = shape::Box::new(3.0, 7.0, 3.0).into();
let box_4_mesh = shape::Box::new(3.0, 7.0, 3.0).into();
commands
.spawn(Collider::from_bevy_mesh(&mesh, &Default::default()).unwrap())
.spawn(Collider::from_bevy_mesh(&box_1_mesh, &Default::default()).unwrap())
.insert(RigidBody::Fixed)
.insert(PbrBundle {
mesh: meshes.add(mesh.into()),
mesh: meshes.add(box_1_mesh.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
perceptual_roughness: 1.0,
@ -17,4 +20,46 @@ pub fn spawn_obstacles(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>,
transform: Transform::from_xyz(-5.0, 3.5, -10.0),
..default()
});
commands
.spawn(Collider::from_bevy_mesh(&box_2_mesh, &Default::default()).unwrap())
.insert(RigidBody::Fixed)
.insert(PbrBundle {
mesh: meshes.add(box_2_mesh.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
perceptual_roughness: 1.0,
..default()
}),
transform: Transform::from_xyz(-5.0, 3.5, 10.0),
..default()
});
commands
.spawn(Collider::from_bevy_mesh(&box_3_mesh, &Default::default()).unwrap())
.insert(RigidBody::Fixed)
.insert(PbrBundle {
mesh: meshes.add(box_3_mesh.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
perceptual_roughness: 1.0,
..default()
}),
transform: Transform::from_xyz(5.0, 3.5, -10.0),
..default()
});
commands
.spawn(Collider::from_bevy_mesh(&box_4_mesh, &Default::default()).unwrap())
.insert(RigidBody::Fixed)
.insert(PbrBundle {
mesh: meshes.add(box_4_mesh.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
perceptual_roughness: 1.0,
..default()
}),
transform: Transform::from_xyz(5.0, 3.5, 10.0),
..default()
});
}