2
\$\begingroup\$

I have tried to write a jump method that sketches out a parabolic jump but it results in either no movement, or a jerky upward movement when I try to scale up dt. I have the following things defined:

    x = 0;
    y = 250;
    pos.set(x, y); // position of player
    velocity.set(2, 12); // player velocity
    gravity.set(0, -2); // player gravity

Then I have the key input processor:

private void handleInput(float dt)
{
    if(Gdx.input.isKeyPressed(Keys.D))
        x += velocity.x;

    if(Gdx.input.isKeyPressed(Keys.A))
        x -= velocity.x;

    if(Gdx.input.isKeyJustPressed(Keys.W))
        jump(dt);

    pos.set(x, y);
    bound.setPosition(pos);
}

This is the jump method which takes in a Gdx delta time method:

private void jump(float dt)
{
    y += velocity.y * dt;
    velocity.set(velocity.x, velocity.y + gravity.y);

    pos.set(x, y);
    bound.setPosition(pos);
}

This is all called in the update method which is called once per frame in the render method:

public void update(float dt)
{
    handleInput(dt);
    movement(dt);
    collision(dt);
}
\$\endgroup\$
2
  • \$\begingroup\$ Please note that you can edit your questions, instead of deleting & reposting them. Frequently posting & deleting questions can trigger some of the site's automated anti-spam measures, so editing is usually your best bet. ;) \$\endgroup\$
    – DMGregory
    Jan 20 '19 at 21:52
  • \$\begingroup\$ @DMGregory oh okay, my bad. I've been a long time reader but I just started posting, thank you for the tips. \$\endgroup\$ Jan 20 '19 at 22:24
1
\$\begingroup\$

Here I created jump function which increases velocity toward upwards. Movement function, on the other hand, updates sprite(the player)'s position each frame. Trick is movement function uses velocity multiplied with Time Delta (seconds between each frame) to calculate position on the next frame.

  • movement function runs every frame, so it goes in main loop.
  • jump is called once (and must be only once) key is pressed, not each frame while key is pressed.

Gravity is there always, decreases vertical velocity each frame.

Code with inline comments:

private void jump(float dt)
{
    velocity.set(velocity.x, velocity.y + jumpPower);//define   jumpPower in your code
}

public void movement(float dt) //I believe should you have this
{
    pos.set(x + (velocity.x * dt), y + (velocity.y * dt));//set position every frame       
    bound.setPosition(pos); //?set sprite position (not sure)
    velocity.set(velocity.x, velocity.y * dt * gravityPower)//decrease vertical velocity every frame
}
\$\endgroup\$
2
  • \$\begingroup\$ This is a good start for an answer, a bit more explanation would help! \$\endgroup\$
    – Almo
    Mar 2 '19 at 5:14
  • 1
    \$\begingroup\$ @Almo please let me know if it's improved \$\endgroup\$
    – Burak
    Mar 2 '19 at 18:21
0
\$\begingroup\$

It could be that you only update the 'y' velocity when you call jump. Try updating 'y' every frame. Then when you call jump just increase the 'y' velocity.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.