Fix apparent jitter in player movement
All checks were successful
CI / Formatting (push) Successful in 1m15s

Remember: `time.delta_secs()` needs to be included in ALL movement calculations!!! This one was pretty well hidden because the player movement was properly interpolated and the camera lerped after it, but it was still barely noticeable!
This commit is contained in:
Michael Bradley 2025-05-22 23:17:53 -04:00
parent b42563a94d
commit d73683359a
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4

View file

@ -1,6 +1,7 @@
use avian2d::prelude::*; use avian2d::prelude::*;
use bevy::{ use bevy::{
input::mouse::{AccumulatedMouseScroll, MouseScrollUnit}, input::mouse::{AccumulatedMouseScroll, MouseScrollUnit},
math::curve::EaseFunction::SmoothStep,
prelude::*, prelude::*,
}; };
@ -14,19 +15,19 @@ pub fn move_player(
) -> Result { ) -> Result {
let acceleration = 500.0; let acceleration = 500.0;
let delta_time = time.delta_secs(); let delta_v = acceleration * time.delta_secs();
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) { if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
velocity.y += acceleration * delta_time; velocity.y += delta_v;
} }
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) { if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
velocity.y -= acceleration * delta_time; velocity.y -= delta_v;
} }
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) { if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
velocity.x -= acceleration * delta_time; velocity.x -= delta_v;
} }
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) { if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
velocity.x += acceleration * delta_time; velocity.x += delta_v;
} }
Ok(()) Ok(())
@ -39,10 +40,14 @@ pub fn quit(mut exit: EventWriter<AppExit>) {
/// Follow the player character with the camera /// Follow the player character with the camera
pub fn move_camera( pub fn move_camera(
time: Res<Time>,
mut camera: Single<&mut Transform, (Without<Player>, With<IsDefaultUiCamera>)>, mut camera: Single<&mut Transform, (Without<Player>, With<IsDefaultUiCamera>)>,
player: Single<&Transform, (With<Player>, Without<IsDefaultUiCamera>)>, player: Single<&Transform, (With<Player>, Without<IsDefaultUiCamera>)>,
) { ) {
camera.translation = camera.translation.lerp(player.translation, 0.05); camera.translation = camera.translation.lerp(
player.translation,
SmoothStep.sample_clamped(time.delta_secs() * 15.0),
);
} }
/// Adjust the camera zoom based on the scroll wheel input /// Adjust the camera zoom based on the scroll wheel input