Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Jumpy Movement [need help]  (Read 163 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann

Junior Member


Medals: 1
Projects: 2



« Posted 2013-02-24 15:04:15 »

Hello Everyone, Recently i have been having trouble with smooth movement. i have tried multiple ways but i continuesly get jumpy movement. i'm not sure if it has something to do with my game loop or is it something to do with how i move my player. Down below is my gameLoop how i move my player and my Input class. i would love some help thanks alot Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
public void run() {
      long lastTime = System.nanoTime();
      double nsPerTick = 1000000000.0 / 60.0;

      int ticks = 0;
      int frames = 0;

      long lastTimer = System.currentTimeMillis();
      double delta = 0;

      init();

      while (running) {
         long now = System.nanoTime();
         delta += (now - lastTime) / nsPerTick;
         lastTime = now;
         while (delta >= 1) {
            ticks++;
            tick();
            delta--;
         }

         try {
            Thread.sleep(16);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }

         frames++;
         render();

         if (System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer += 1000;

            System.out.println("FPS " + frames + " TICKS " + ticks);

            ticks = 0;
            frames = 0;
         }
      }
   }


Player Movement:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void tick(ArrayList<grassBlock> grass){
      x += velX;
      y += fallSpeed;
     
      if(Falling){
         fallSpeed += Gravity;
      }
     
      if(fallSpeed == 5){
         fallSpeed = 5;
      }
   }


Input class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public class Input implements KeyListener {

   public Game game;

   public Input(Game game) {
      this.game = game;
   }

   public void keyPressed(KeyEvent e) {
      int key = e.getKeyCode();
      if (key == KeyEvent.VK_A) {
         game.getPlayer().setVelX(-4);
      }else if (key == KeyEvent.VK_D) {
         game.getPlayer().setVelX(4);
      }
   }

   public void keyReleased(KeyEvent e) {
      int key = e.getKeyCode();
      if (key == KeyEvent.VK_A) {
         game.getPlayer().setVelX(0);
      }else if (key == KeyEvent.VK_D) {
         game.getPlayer().setVelX(0);
      }
   }

   public void keyTyped(KeyEvent e) {
   }

}


Thanks for help
Offline Lordpomeroy

Junior Member


Medals: 1
Projects: 1



« Reply #1 - Posted 2013-02-24 15:13:38 »

If you have more than the desired time between two ticks you are performing the
1  
tick();
action twice.
This means if you have a delta of 1.00000001 you will do the action two times. This might lead to your jumps.
Try to use the delta in your player class to determine the distance he fall or walked.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
NegativeZero (2 views)
2013-06-19 03:31:52

NegativeZero (4 views)
2013-06-19 03:24:09

Jesse_Attard (13 views)
2013-06-18 22:03:02

HeroesGraveDev (57 views)
2013-06-15 23:35:23

Vermeer (57 views)
2013-06-14 20:08:06

davedes (55 views)
2013-06-14 16:03:55

alaslipknot (50 views)
2013-06-13 07:56:31

Roquen (71 views)
2013-06-12 04:12:32

alaslipknot (56 views)
2013-06-10 19:30:18

HeroesGraveDev (73 views)
2013-06-09 04:36:03
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!