Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.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.

share|improve this question
up vote 3 down vote accepted

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: 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.

share|improve this answer
+1 if only for physical unit consistency; if you don’t have it, you have a problem. – sam hocevar Dec 9 '11 at 0:44
Thanks a bunch. I'm somewhat new to gamedev but I think I'm on the right track thanks to your help. – Lienau Dec 9 '11 at 1:41

Have a read of this too:

share|improve this answer
A link alone is not an answer. This is better suited as a comment. – bummzack Dec 9 '11 at 10:06
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. – Le Comte du Merde-fou Dec 9 '11 at 10:20
But your answer becomes worthless once the link goes down. See also this meta discussion. – bummzack Dec 9 '11 at 10:44

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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