Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:16:57+00:00 2026-06-14T05:16:57+00:00

I have made a kind of gravity in a 2D Java game and I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:16:59+00:00Added an answer on June 14, 2026 at 5:16 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a kind of Video Slider, where thumbnail of videos are displaying
I have made a kind of CMS using CodeIgniter. It loads the page content
I have made a jQuery toggle for a menu that I had in mind.
I have made a function that validates inputs with for different kind of strings.
I feel kind of stupid asking this but I have made a few regular
In attempting to make some Swing code more readable, I have made an InlineGridBagConstraints
I have a game that I have made in C# and it installs and
I have made an application for IPad in objective C. In this I am
i have made an application having entity framewrok. It is wpf application, now it
I have made a code that read data from flash Nand (without filesystem). fd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.