I want my player to jump and i tried my best to do it and i couldn't. Basicaly my class are splitted(i have speparate player class and keyboard class) which i thought would make my life easier but didnt.

I made it so that if i press space bar, then i would fly(this is for temporarily and i wanted it to jump) and also i added gravity.

It would be great if you guys posted what to add on what class and explain what it does cause im a noob at java.


My Player Class

package com.vescorspel.game.MyFirstGame.entities.creatures;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.vescorspel.game.MyFirstGame.Handler;
import com.vescorspel.game.MyFirstGame.gfx.Animation;
import com.vescorspel.game.MyFirstGame.gfx.Assets;

public class Player extends Creatures{

private Animation animRight, animLeft;

public Player(Handler handler, float x, float y) {
    super(handler, x, y, Creatures.DEFAULT_CREATURE_WIDTH, Creatures.DEFAULT_CREATURE_HEIGHT);

    bounds.x = 32;
    bounds.y = 32;
    bounds.width = 92;
    bounds.height = 96;

    animRight = new Animation(100, Assets.DerpDino_right);
    animLeft  = new Animation(100, Assets.DerpDino_left);

public void tick() {


private void getInput(){
    xMove = 0;
    yMove = 5;

        xMove = -speed;
        xMove = speed;
        //this makes my player fly
        yMove = -speed;

public void render(Graphics g) {
    g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);   

private BufferedImage getCurrentAnimationFrame(){
    if(xMove < 0){
        return animLeft.getCurrentFrame();
        return animRight.getCurrentFrame();


My KeyManager Class

package com.vescorspel.game.MyFirstGame.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyManager implements KeyListener{

private boolean[] keys;
public boolean left, right, jumping;

public KeyManager(){
    keys = new boolean[256];

public void tick(){
    left = keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_D];
    jumping = keys[KeyEvent.VK_SPACE]; 

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;

public void keyTyped(KeyEvent arg0) {   


2 Answers 2


You need to implement Kinematics - Movement Obliquely in his game. Of course, if you want to simulate our world. Remind your world your rules. Or if you prefer to use a ready-made physical engine like JBox2D.

The explanation is very long and I'm not in the area of ​​physics, so I'm being corrected if I'm wrong about anything, please.

First define the real-world scale in your game. How many pixels are equal to one meter? This is important for calculations, it is called discretization. Define a constant in the code, for example:

private final double PIXELS_POR_METER = 10.0;

Second, all objects with mass will be actively acting on gravity in your game and make contact with the ground, simple example:

// Game gravity
private final GRAVITY = -10; // 10 m / s²

// Initial velocity in leap angle factor
// Example angle definition: jump only - 90 degrees, jump + right / left - 45 degrees
V0y = v0 + Math.sin (angle);
// Where v0 is the initial velocity of the body, usually velocity of the character

Double time = 1.0 / getFps (); // search about this getFps () function, it returns how many frames are redrawn every second in your game.
y + = v0y * time + GRAVITY * time * time / 2; // displacement to each frame

// Contact with the floor
If y <= y_floor {y = y_floor};

This operation is only for the Y axis. Usually in a 2D game is enough because the offset on the X axis is controlled by the user. What does not happen in the real world.

These calculations are in real world scale. Finally, make it into game coordinates before you draw.

// Position in pixels unit
Double x_real = x / PIXELS_POR_METER;
Double y_real = y / PIXELS_POR_METER;

That's it, remembering that any mistake warn me so I can fix it.

Study for understand more: kinematics formulas

  • \$\begingroup\$ i don't need kinetics(i also have gravity) i just need how to implement jump in the most simple way which fits my code \$\endgroup\$
    – Sree
    Nov 13, 2016 at 2:17
  • \$\begingroup\$ Eight code lines? Step1: Define constants. Step2: Define the angle in getInput() function. Step3: use formulas before render function, if you prefer create update() function. See, If you simply increment and decrease the y, you will not have the effect of acceleration and deceleration in your jump. But, again, your world, your rules. \$\endgroup\$
    – Bill Rock
    Nov 13, 2016 at 14:37

I will tell my logic that would work with any type of framework with little modifications.

Void jump()
    static bool j=false;// is jumping
    If( key ==' ' && j == false)
          J = true;
    If (j && obj.y < maxheight)
        Obj.y ++;
    If(obj.y > maxheight)
    If ( obj.y< minheight)
        J = false;

Basically it set the signal variable to false initially. It means the ball is on the ground

If we hit a space the variable is set to true, obj will rise and get the max height...after that it will decrease..once it touched the ground the control variable is set to false..

This should be called in a game loop to make it work..

this Is all about jumping...you have to transform this code to make it work in java

  • \$\begingroup\$ Since you have gravity..at the falling phase of the ball decrement it with the value of gravity...this example is very weak when comes to physics realism...but it's a very minimal code you can have to get started \$\endgroup\$ Nov 13, 2016 at 7:42
  • \$\begingroup\$ what i did now is that if i press the 'Space Bar', then my y value decreases(which makes my flayer fly) but i just want it for a certain amount of Milli second. How to do that \$\endgroup\$
    – Sree
    Nov 13, 2016 at 12:27
  • \$\begingroup\$ this is the code if(handler.getKeyManager().jumping){ yMove = - 4; } \$\endgroup\$
    – Sree
    Nov 13, 2016 at 12:27
  • \$\begingroup\$ When it hits the maximum value, get the time, and compare it with the actual time as it proceeds. If it is excess then bring the ball to ground \$\endgroup\$ Nov 13, 2016 at 12:34
  • \$\begingroup\$ You can use sine wave to smoothify your jump animation \$\endgroup\$ Nov 14, 2016 at 15:13

You must log in to answer this question.

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