DEV Community

Discussion on: The Bevy Game Engine

Collapse
 
paulgoetze profile image
Paul Götze • Edited

Nice article, thanks!

Just a small hint: In your implementation the player movement is currently dependent on the actual framerate the game is running with. I.e. if the framerate is low, the player moves slow and vice versa.

You'd probably want to calculate the translation difference based on the time that passed, by passing a Time into the system, like:

fn player_movement(
    keyboard_input: Res<Input<KeyCode>>,
    time: Res<Time>,
    mut query: Query<(&mut Transform, &Player)>,
) {
    let v = 200.0; // velocity in units per second, v = ds / dt => ds = v * dt
    let delta = v * time.delta_seconds;

    for (mut transform, _player) in &mut query.iter() {
        let translation = transform.translation_mut();

        if keyboard_input.pressed(KeyCode::Right) {
            *translation.x_mut() += delta;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

With this, the player will always be moved the right distance after a rendering update, no matter how high the framerate.

Collapse
 
ethanyidong profile image
Ethan Tang

Yup, thanks for pointing that out! I didn't want to add too much complexity to this first example, but I'm definitely going to point this out in the next article.