I have seen the document for libgdx and managed to move the character and also to implement bounds but the document does not show how to implement jumping.

I have downloaded the superjumper demo code but it is too confusing for me. Is there anyone here who can guide me?

Say for example we have a spritebatch:

Spritebatch batch;

How can I implement a jump for this spritebatch when the screen is touched?

  • \$\begingroup\$ Sorry but what do you mean with "implement a jump" ? You have drawn a sprite and want to apply a "jump" when screen is touched ? \$\endgroup\$
    – Ion Farima
    Mar 13, 2013 at 17:40
  • \$\begingroup\$ This question as it is, is too vague, you need at least a sprite to jump and a plataform (ground). you should start by studing the sprites and then the box2d physics library. \$\endgroup\$
    – petervaz
    Mar 13, 2013 at 17:52
  • \$\begingroup\$ @Ion yes i want to make a sprite jump when the screen is touched.. \$\endgroup\$ Mar 13, 2013 at 18:20
  • \$\begingroup\$ @petervaz well the superjumper demo from libgdx doesnt implement box2d..i want to know the code thats responsible for the jump of a sprite or spritebatch.. yup i also need to know how to make a platform interact (or collide) with a character...i think that will be covered in the jump explanation as well.. \$\endgroup\$ Mar 13, 2013 at 18:21
  • \$\begingroup\$ I tink that if you want to make your sprite jump without assigning it to a physic body and then detect the collision you would need to write your own engine \$\endgroup\$
    – Ion Farima
    Mar 13, 2013 at 18:37

3 Answers 3


NOTE: Apparently, in libgdx, the origin is at the lower left corner of the screen. You should switch "IncreaseYCoordinate" with "DecreaseYCoordinate" in your actual code.

Jumping, at the base, is language and framework agnostic. You should look at it that way if you wish to learn anything. "Jumping", especially in 2D games, is the process of modifying a character's (sprite's) position on the screen so that he or she appears to be jumping. That's all. You just draw your character at a different height on each Draw call, so it appears to be moving vertically (jumping).

I'm not exactly familiar with libgdx, but I presume you're doing something like:

batch.draw(someTexture, 100, 350); 

Where 100 is the X coordinate and 350 the Y coordinate of the drawn texture. This obviously makes your texture show up at the position described by the vector (100, 350). I am currently supposing that the origin (0,0) is the upper left corner of the screen (increasing the Y coordinate makes your sprite show up lower on the screen).

Now, if you were to decrease the Y coordinate, like:

batch.draw(someTexture, 100, 349);

Your texture would get drawn at a higher position on the screen.

batch.draw(someTexture, 100, 348);

And even higher, now.

Simple, is it not? The first thing you need to do is to get your sprite to continuously ascend until he should stop doing so. Then, the sprite should go back to its original position by descending. Since you have not posted any actual code, and the question is a bit broad, I can only give some rough guidelines.

  • Store your sprite's position in a Vector-like structure. Or two variables, one for the X coordinate, and one for the Y coordinate.
  • The jump should go smoothly. Keep two variables: "speed" and "originalSpeed". These should be integers;
  • Keep a state flag, that tells the Update() function when to increase or decrease the sprite's Y coordinate. This can be an enum with the values { Ascending, Descending, Standing }
  • Intercept the touch event, and when it happens, set the state flag to Ascending (if it's not already set to Ascending);
  • In the Update method, you should change the Y coordinate of the sprite considering its current state. Thus:

    if (State == Ascending) 
      //We're ascending. Decrease the Y coordinate of the sprite by the speed.
      DecreaseByValue(Speed, Value); //The character needs to jump smoothly, so the speed should decrease as he ascends.
      if (Speed <= 0) 
           ChangeState(Descending); //If speed is <= 0, then the character should fall down.
           Speed = 0;
    else if (State == Descending) 
      //We're descending. Increase the Y coordinate by the speed (at first, it's 0).
      IncreaseByValue(Speed, Value); //Increase the speed, so the character falls gradually faster.
      if (CurrentYCoordinate >= OriginalYCoordinate) 
           //If we reached the original Y coordinate, we hit the ground. Mark the character as standing.
           CurrentYCoordinate = OriginalYCoordinate;
           Speed = OriginalSpeed;
  • Drawing is done like:

    batch.draw(yourSprite, CurrentXCoordinate, CurrentYCoordinate);

Obviously, you need to enter your own values for the original coordinates and speeds. Tweak them to find the values that best suit your needs. This is not exactly the "best" way to do it, but should give you a starting point. You may want to impose some limitations on the falling speed, so that it increases only up to a certain amount. You could (and should) use different speeds for jumping and falling too.

Also, this is not libgdx related, but might give you a few hints:


I definitely recommend that you search for as many such tutorials as possible, even if they're not libgdx related. Jumping is a concept that can be learned from just about any source.

  • \$\begingroup\$ Great answer, with nice argumentation, but I think it's not the best way to simulate jumps.However it depends on what he wants :) P.S: spritebatch's origin coordinates (0,0) are in the lower left corner not in the upper one ;) \$\endgroup\$
    – Ion Farima
    Mar 13, 2013 at 19:48
  • \$\begingroup\$ Thanks for the correction, I have updated the answer to make note of it. \$\endgroup\$
    – user15805
    Mar 13, 2013 at 19:52

For making a sprite "jump", it needs to be assigned to a box2d body. Then you can apply an linear impulse to the physic body when screen is touched.

Example : For making the code more simplier, you can begin with inserting something like this in your render() method :

If (Gdx.input.isTouched()) {
          Body.applyLinearImpulse(new Vector2(0, 20), body.getPosition());


Doing so, every time you touche the screen, your ball will jump. You can change the value of the vector if you want to change direction, force of the impulse.

P.S: I strongly recommend you to read the box2d manual or see examples on internet regarding the creating and manipulating bodies in this engine.

  • \$\begingroup\$ thanks mate your help is appreciated , but i was wondering why mario didnt used any box2d thing in the superjumper demo... also it would be great if you can give the link to box2d manual !! \$\endgroup\$ Mar 13, 2013 at 18:34
  • \$\begingroup\$ As you , I'm a begginer in this type of programming ( libgdx and box2d) and I have learned the basics from examples. The manual is written in C++ so it would help you only if you know this programming language. I haven't searched for it's online version, sorry. \$\endgroup\$
    – Ion Farima
    Mar 13, 2013 at 18:41

My advice would be to have another look at the superjumper code. The jumping code in the superjumper demo is here.

Lines 69-79 set up jumping in response to hitting a platform or hitting a spring. In both cases this is done by setting the vertical velocity and setting Bob's state to jumping.

public void hitPlatform () {
    velocity.y = BOB_JUMP_VELOCITY;
    state = BOB_STATE_JUMP;
    stateTime = 0;

public void hitSpring () {
    velocity.y = BOB_JUMP_VELOCITY * 1.5f;
    state = BOB_STATE_JUMP;
    stateTime = 0;

The actual movement takes place in the update() method (lines 37-61). Here the acceleration due to gravity is applied to the velocity, then the position is updated by adding the velocity itself. Note that all updates are multiplied by deltaTime to take into account that the rate at which update() is called may not be constant.

Here's the snippet that does that.

public void update (float deltaTime) {
    velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
    position.add(velocity.x * deltaTime, velocity.y * deltaTime);

As for detecting whether the screen has just been touched, use Gdx.input.justTouched(). For example (this is illustrative more than accurate):

if (Gdx.input.justTouched() && state != JUMPING) {
  • \$\begingroup\$ thats what i wanted !! i just hope libgdx creators adds this thing in the documentation..because we as developer have to hunt down the lines which make sense to a newbie !! \$\endgroup\$ Mar 14, 2013 at 8:02
  • \$\begingroup\$ It would be useful to have an article deconstructing one of the libgdx demos with a view to spelling out what each step is doing. I'm half tempted to do that if it will help people get started. \$\endgroup\$
    – R Hyde
    Mar 14, 2013 at 9:21
  • \$\begingroup\$ go ahead mate...i am sure there are many beginners like me who are having a difficult life solving the libgdx puzzle !! \$\endgroup\$ Mar 14, 2013 at 13:55
  • \$\begingroup\$ would you implement all of this inside Bob.java? \$\endgroup\$
    – BradleyIW
    Apr 19, 2015 at 20:21

You must log in to answer this question.

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