0
\$\begingroup\$

I've been doing some stupid stuff in Slick2D+LWJGL and I've run into a problem. I press the UP arrow and my character continues to go up until I let go :/ Here's the code:

if (inp.isKeyDown(Input.KEY_UP) && !jumping) {
    jumping = true;
    velY=-0.4f;
} else {
    jumping = false;
    velY=0.2f;
}

No idea what's happening :/ Help?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Presumably this code is called once every frame. What is likely happening is that while you hold down the up arrow, you are constantly toggling the state of jumping, which means you take the positive branch of the if every even frame and the negative branch every odd frame.

That also means you constantly toggle the velocity between -0.4 and 0.2. Every frame you presumably process that velocity and offset the player by it; however, because your upwards velocity exceeds your downwards velocity, you'll have a net upwards movement.

For example:

  • On frame 0 you starting hold up. That sets your velocity to -0.4 (and jumping to true) and then you add that velocity to the player's position, giving you a delta in of Y of -0.4.

  • The next frame you are still holding up, but jumping is now true, so you take the negative branch, setting velocity to 0.2 and jumping to false. When you apply velocity to position, you have a net delta of -0.4 + 0.2 = -0.2.

  • Now, the third frame you are still holding up but jumping is now false, so you take the positive branch, set velocity to -0.4 and jumping to true. When you apply velocity to position you have a net delta of -0.2 - 0.4 = -0.6.

That's why your player always moves up.

To fix this, you could restructure the condition as follows:

if (inp.isKeyDown(Input.KEY_UP)) {
  if (!jumping) {
    jumping = true;
    velY=-0.4f;
  }
} else {
  jumping = false;
  velY=0.2f;
}

This means that if the up key is held, you start jumping if you have not already. If the up key is not held, you cancel the jump.

\$\endgroup\$
5
  • \$\begingroup\$ Seems like the same thing is happening :/ As I hold down up, my "character" continues to go up. \$\endgroup\$ Commented Apr 25, 2014 at 7:05
  • \$\begingroup\$ Do you ever set the velocity elsewhere? If you don't reduce it to zero over time, of course he will still go up since you imparted a constant velocity. \$\endgroup\$
    – user1430
    Commented Apr 25, 2014 at 7:09
  • \$\begingroup\$ Use your debugger to examine the state of the data every frame (or print it to a log file). That should help you diagnose what other areas of the code are problematic. \$\endgroup\$
    – user1430
    Commented Apr 25, 2014 at 7:10
  • \$\begingroup\$ You can also search this site for "2D jumping" and similar to find other questions discussing how to implement a good jump. You may find them better suited than your current approach. \$\endgroup\$
    – user1430
    Commented Apr 25, 2014 at 7:13
  • \$\begingroup\$ I switched velY=0.2f; to velY+=0.001f; and it works how I wanted it to :) 0.1f or even 0.01f is extremely fast for some reason :/ but whatever, Thanks a bunch for the help :) \$\endgroup\$ Commented Apr 25, 2014 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .