Angus Moore
Angus Moore

Reputation: 4191

Jumping algorithm in Java

I have made a kind of gravity in a 2D Java game and I am attempting to create a jumping algorithm. I have had a look around and cannot seen to find any help for an algorithm which moves up and down in a wave.

Here is the code in Game.java:

package game;

import java.awt.*;

public class Game extends GameLoop{

    public void init(){
        setSize(864,480);
        Thread th = new Thread(this);
        th.start();
        offscreen = createImage(864,480);
        d = offscreen.getGraphics();
        addKeyListener(this);
    }

    public void paint(Graphics g){
        d.clearRect(0, 0, 864, 480);
        d.drawImage(background, 0, 0, this);
        d.drawImage(person, x, y, this);
        g.drawImage(offscreen, 0, 0, this);
    }

    public void update(Graphics g){
        paint(g);
    }
}

And here is the code in GameLoop.java:

package game;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;

public class GameLoop extends Applet implements Runnable, KeyListener{

    public int x, y;
    public Image offscreen;
    public Graphics d;
    public boolean up, down, left, right;
    public BufferedImage background, w1, w2, w3, w4, w5, w6, w7, w8, person, s1, s2;
    public int counter, counter2;

    public void run(){
        x = 100;
        y = 100;
        try {
            background = ImageIO.read(new File("background.png"));
            w1 = ImageIO.read(new File("walk1.png"));
            w2 = ImageIO.read(new File("walk2.png"));
            w3 = ImageIO.read(new File("walk3.png"));
            w4 = ImageIO.read(new File("walk4.png"));
            w5 = ImageIO.read(new File("walk5.png"));
            w6 = ImageIO.read(new File("walk6.png"));
            w7 = ImageIO.read(new File("walk7.png"));
            w8 = ImageIO.read(new File("walk8.png"));
            s1 = ImageIO.read(new File("stancel.png"));
            s2 = ImageIO.read(new File("stancer.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        person = s1;
        while(true){
            x = 100;
            y = 100;
            while(true){
                if (y <= 304 && up != true){
                    y+=10;
                }
                counter++;
                counter2++;
                if (counter >= 20){
                    counter = 0;
                }
                if (counter >= 0 && counter <= 5 && right == true){
                    person = w1;
                }
                if (counter > 5 && counter < 10 && right == true){
                    person = w2;
                }
                if (counter >= 10 && counter <= 15 && right == true){
                    person = w3;
                }
                if (counter > 15 && right == true){
                    person = w4;
                }
                if (counter2 >= 20){
                    counter2 = 0;
                }
                if (counter2 >= 0 && counter2 <= 5 && left == true){
                    person = w5;
                }
                if (counter2 > 5 && counter2 < 10 && left == true){
                    person = w6;
                }
                if (counter2 >= 10 && counter2 <= 15 && left == true){
                    person = w7;
                }
                if (counter2 > 15 && left == true){
                    person = w8;
                }
                if (left == true){
                    x-=4;
                }
                if (right == true){
                    x+=4;
                }
                if (up == true){
                    y-=4;
                }
                if (down == true){
                    y+=4;
                }
                if ( x <= -10 ){
                    x = -10;
                }
                if ( y <= 0 ){
                    y = 0;
                }
                if ( x >= 824 ){
                    x = 824;
                }
                if ( y >= 304 ){
                    y = 304;
                }
                repaint();
            try{
                Thread.sleep(20);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            }
        }
    }

    //@Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == 37){
            left = true;
        }
        if(e.getKeyCode() == 38){
            up = true;
        }
        if(e.getKeyCode() == 39){
            right = true;
        }
        if(e.getKeyCode() == 40){
            down = true;
        }
    }

    //@Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == 37){
            left = false;
            person = s2;
        }
        if(e.getKeyCode() == 38){
            up = false;
        }
        if(e.getKeyCode() == 39){
            right = false;
            person = s1;
        }
        if(e.getKeyCode() == 40){
            down = false;
        }
    }

    //@Override
    public void keyTyped(KeyEvent e) {
    }
}

I think I need a counter , counter3 as a double, but I am not sure of an algorithm that will go up to a certain point on the y axis, then move down. Any help is appreciated.

Upvotes: 3

Views: 6516

Answers (3)

Stephen C
Stephen C

Reputation: 718836

I'm not entirely sure by what you mean by "moves up and down in a wave".

But anyway, in real world physics, the altitude of an object in free-fall (ignoring air resistance) follows a parabola; i.e. it obeys a formula like:

altitude =  maxAltitude - (acceleration * (t - timeOfMaxAltitude))^2  

(There are other ways of formulating the laws of motion, but this approach is probably what you need for a simple game.)


A 1/2 sine wave looks rather like a truncated parabola, and could be used instead in a game. However, you need to realize that it is not an accurate simulation of real-world physics, especially for high jumps.

Upvotes: 2

Daniel F. Thornton
Daniel F. Thornton

Reputation: 3685

One of the simplest wave functions is our old friend sine:

enter image description here

So for a jump motion, you would probably like the first half of a sine period. You could use java.util.Math, passing a sequence of numbers from 0 to 180 (how many depends on the number of frames you want the jump to last). e.g.:

Math.sin(0.0);   // = 0.0 
Math.sin(30.0);  // = 0.5
Math.sin(60.0);  // = 0.9
Math.sin(90.0);  // = 1.0
Math.sin(120.0); // = 0.9
Math.sin(150.0); // = 0.5
Math.sin(180.0); // = 0.0

Upvotes: 5

fgb
fgb

Reputation: 18569

You can use a variable to represent the vertical velocity. When you jump, set the vertical velocity to a negative value. On each frame, add the vertical velocity to the vertical position, and add a constant to the velocity to simulate gravity. When there is something underneath, set the velocity back to 0. Experiment with the constant values to get an appropriate jump height.

Upvotes: 1

Related Questions