[2D Jumping Sprite Animation [FIXED]]

So as the title says i’m triyng to create a simple jumping animation for my 2d RPG, instead of just having left,right,up,down movements i’d like left, right, and jump.

At the moment the only variables to tell/change a players position are:
public int absX;
public int absY;

Now I know i’m going to need some other variables + forumlas, such as Dx, Dy, but if someone could point me in the direction on how to go on this, it would be much appreciated.
At the moment i just made a simple method called jump(), which (absY–) = (Up actually), 15 times, and when that characters height has raised 15 times, than i would lower it back to -15, (flat at the floor), so it looks like a animation.
But I really don’t like the way that that looks O_o.
If someone could help me please! :o

Well the variables that are needed are gravity and yVelocity.
Gravity is a constant which you should tweak to your heart’s content to get it right. Let’s make gravity 500 pixels per second.
The Y velocity will be 0 when the player is not jumping, but as soon as you hit the spacebar, you set it to a desired vertically moving speed. Let’s go with -300 pixels per second as the initial velocity.

Now every tick, you will add the yVelocity multiplied with the delta to the Y variable. Then you will add the gravity multiplied with the delta to the yVelocity. This is applying gravity your velocity and then applying the velocity to your position.

BTW: You should change absX and absY to doubles.


private final double gravity = 500;

public void update(long deltaTime) {
    double delta = deltaTime / 1000.0;
    
    if(spaceBarIsPressed)
        yVelocity = -300; //Going up 300 pixels per second.
    
    absY += yVelocity * delta;
    yVelocity += gravity * delta;
}

Ah, I figured it out, perfectly.
Thanks m8, thread closed.

Thanks about the BTW on turning my ints to doubles for player positioning, just now looking into deeper detail on a Double, thanks m8.

Glad to help :slight_smile: