2
\$\begingroup\$

I'm making a 2D platformer on Android using libGDX. One thing I've had a problem with recently is gravity and jumping. I found a few tutorials on the internet and was able to come up with this...

public static final int JUMP_HEIGHT = 64;
public static final float JUMP_GRAVITY= -6.0f;
public static final float MAX_GRAVITY = 20.0f;
public static final float GRAVITY= 19.0f; 

private void updateGravity(float delta) {
    if(Gdx.input.isTouched() && !jumping) {
        jumping = true;
        gravity = Config.JUMP_GRAVITY;
    }

    if(jumping) {
        // change frame to jump
        sprite.setRegion(walkFrames[0]);
        sprite.translate(0, (-1*gravity));
        // runner has landed?
        if(sprite.getY() <= idleY) {
            jumping = false;
            sprite.setPosition(Config.RUNNER_X, idleY);
        }
        // slowdown / fall
        if (gravity < Config.MAX_GRAVITY) gravity += Config.GRAVITY*delta;

    }
}

The code works and my character will jump and fall smoothly. Although the time it takes for him to jump and comedown is independent from delta the height he jumps is not. If the device lags while jumping he will jump only a portion of how high he should. For the life of me I cannot get it to make sure it jumps a specific height (JUMP_HEIGHT) and in a specific time independent of the delta.

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

Keep in mind your units.

What is your gravity variable supposed to represent? Gravity (in physics) is acceleration, which is distance / time squared. You're translating your unit by some arbitrary number (gravity), and not taking into account time. So you're treating your acceleration value as if it were a distance value.

A naive solution is simply to multiply your gravity by delta (so imagine your gravity is in distance/time), so it's more-or-less framerate independent, but there will be integration errors. Also if your framerate is variable, your end jump height might be different. Usually what I do in these situations is that if your translation would put you beyond your jump height, just clamp that frame's jump height to the max jump height. This would mean that your guy jumping would move at a constant speed (other than the final frame if you do clamping) instead of accelerating up or down.

Now if you want your jumping to be more an acceleration rather than a velocity, you want to do some math like this article describes: http://gafferongames.com/game-physics/integration-basics/ It would involve you adding some velocity/acceleration variables to your character and doing a bit more math, but it's pretty simple. In that scenerio you would always apply gravity to the player, and your jump would be a separate acceleration impulse upwards.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +1 if only for physical unit consistency; if you don’t have it, you have a problem. \$\endgroup\$ Dec 9, 2011 at 0:44
  • \$\begingroup\$ Thanks a bunch. I'm somewhat new to gamedev but I think I'm on the right track thanks to your help. \$\endgroup\$
    – Lienau
    Dec 9, 2011 at 1:41
0
\$\begingroup\$

Have a read of this too: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html

\$\endgroup\$
3
  • 1
    \$\begingroup\$ A link alone is not an answer. This is better suited as a comment. \$\endgroup\$
    – bummzack
    Dec 9, 2011 at 10:06
  • \$\begingroup\$ I'd argue that it is an answer because the linked page explains exactly what needs to be done to rectify the problem, and any further text I may add would have just repeated info from that. \$\endgroup\$ Dec 9, 2011 at 10:20
  • 1
    \$\begingroup\$ But your answer becomes worthless once the link goes down. See also this meta discussion. \$\endgroup\$
    – bummzack
    Dec 9, 2011 at 10:44
0
\$\begingroup\$

And I redirect you to the top answer on another post on this site: Good 2D Platformer Physics

The webpage he recommends is pure gold.

\$\endgroup\$

You must log in to answer this question.

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