-1

Basically I want to make gravity system in Rust Bevy for my player, but I can't figure out how can I do that so I came up with this idea to multiply the -y value with the seconds. If you have any other idea please tell me. Hope you understand what I'm trying to say.

fn player_2_keyboard_event_system(
    kb: Res<Input<KeyCode>>,
    mut query: Query<&mut Velocity2, With<Player2>>,
    time: Res<Time>,
) {
    if let Ok(mut velocity2) = query.get_single_mut() {
        velocity2.x = if kb.pressed(KeyCode::A) {
            -1.
        } else if kb.pressed(KeyCode::D) {
            1.
        } else {
            0.
        };

        if let Ok(mut velocity2) = query.get_single_mut() {
            velocity2.y = if kb.pressed(KeyCode::S) {
                -1.
            } else if kb.pressed(KeyCode::W) {
                1.
            } else {
                -0.1 * time.delta_seconds() // Something like this!!! 
            }
        }
    }
}

I was trying to make gravity in Rust Using Bevy-Engine, but I can't figure out how I can do that. So I came up with this idea.

Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • What's wrong with this approach? What did you hope to achieve, and what is the actual outcome? I fail to find an actual question in your post. – Finomnis Dec 04 '22 at 11:04
  • Your current question is "How to use time in your Rust code", followed by an example where you use the time in your Rust code. So you answered your own question already? – Finomnis Dec 04 '22 at 11:06
  • why don't you use a physics engine, there is currently a good one already available: https://github.com/dimforge/bevy_rapier – Hartmut Dec 26 '22 at 07:33

1 Answers1

0

That seems like a good approach, except that time.delta_seconds() will give you the time since the last frame update, and that can be unstable sometimes, so the physics will be affected. If you want precise physics you shouls define your own physics timestep, calculate for as many timesteps as possible within time.delta_seconds(), and save any remainder time for the next frame.