Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

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

share|improve this answer
1  
+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
add comment

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

share|improve this answer
1  
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. –  Jimmy Shelter Dec 9 '11 at 10:20
1  
But your answer becomes worthless once the link goes down. See also this meta discussion. –  bummzack Dec 9 '11 at 10:44
add comment

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
add comment

Your Answer

 
discard

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.