Skip to content

Commit

Permalink
Air resistance for walk controller (more realistic stopping behaviour)
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 9, 2023
1 parent 3592e63 commit 6a3e244
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::GameSet;

pub struct PhysicsPlugin;

const AIR_RESISTANCE: f32 = 0.5;

impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Expand Down Expand Up @@ -32,11 +34,11 @@ pub struct WalkController {
}

fn walk_system(time: Res<Time>, mut query: Query<(&mut Velocity, &mut WalkController)>) {
for (mut velocity, controller) in query.iter_mut() {
for (mut velocity, mut controller) in query.iter_mut() {
let dspeed = controller.target_velocity - velocity.0;
let accel = controller.acceleration.min(dspeed.length() * 100.0);

velocity.0 += dspeed.normalize_or_zero() * accel * time.delta_seconds();
let cur_vel = velocity.0;
velocity.0 += (dspeed.normalize_or_zero() * accel - cur_vel * AIR_RESISTANCE) * time.delta_seconds();
velocity.0 = velocity.0.clamp_length_max(controller.max_speed);
}
}
4 changes: 2 additions & 2 deletions src/shepherd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{player::{DOG_SPEED, DOG_ACCELERATION}, common_storage::CommonStorage

const SHEPHERD_PATH: &str = "test/Knight.png";

const SHEPHERD_SPEED: f32 = DOG_SPEED;
const SHEPHERD_ACCEL: f32 = DOG_ACCELERATION;
const SHEPHERD_SPEED: f32 = DOG_SPEED * 0.5;
const SHEPHERD_ACCEL: f32 = DOG_ACCELERATION * 0.5;

const IGNITE_RADIUS: f32 = 5.0;

Expand Down

0 comments on commit 6a3e244

Please sign in to comment.