Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  How to fix stuttering on 60hz?  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline nA

Senior Newbie





« Posted 2013-12-13 02:13:56 »

Hello guys!
I am SUPER new to programming and especially to game development.
Two days ago I started learning java. I follow this http://www.kilobolt.com/day-5-background-and-sprites.html tutorial.
My main class looks like this right now:
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  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
package kiloboltgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class StartingClass extends Applet implements Runnable, KeyListener {

   private Robot robot;
   private Image image, currentSprite, character, characterDown,
         characterJumped, background;
   private Graphics second;
   private URL base;
   private static Background bg1, bg2;

   @Override
   public void init() {

      setSize(800, 480);
      setBackground(Color.BLACK);
      setFocusable(true);
      addKeyListener(this);
      Frame frame = (Frame) this.getParent().getParent();
      frame.setTitle("Q-Bot Alpha");
      try {
         base = getDocumentBase();
      } catch (Exception e) {
         // TODO: handle exception
     }

      // Image Setups
     character = getImage(base, "data/character.png");
      characterDown = getImage(base, "data/down.png");
      characterJumped = getImage(base, "data/jumped.png");
      currentSprite = character;
      background = getImage(base, "data/background.png");
   }

   @Override
   public void start() {

      bg1 = new Background(0, 0);
      bg2 = new Background(2160, 0);
      robot = new Robot();

      Thread thread = new Thread(this);
      thread.start();
   }

   @Override
   public void stop() {
      // TODO Auto-generated method stub
  }

   @Override
   public void destroy() {
      // TODO Auto-generated method stub
  }

   @Override
   public void run() {
      while (true) {
         robot.update();
         if (robot.isJumped()) {
            currentSprite = characterJumped;
         } else if (robot.isJumped() == false && robot.isDucked() == false) {
            currentSprite = character;
         }
         bg1.update();
         bg2.update();
         repaint();
         try {
            Thread.sleep(17);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   @Override
   public void update(Graphics g) {
      if (image == null) {
         image = createImage(this.getWidth(), this.getHeight());
         second = image.getGraphics();
      }

      second.setColor(getBackground());
      second.fillRect(0, 0, getWidth(), getHeight());
      second.setColor(getForeground());
      paint(second);

      g.drawImage(image, 0, 0, this);

   }

   @Override
   public void paint(Graphics g) {
      g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
      g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
      g.drawImage(currentSprite, robot.getCenterX() - 61,
            robot.getCenterY() - 63, this);

   }

   @Override
   public void keyPressed(KeyEvent e) {

      switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
         System.out.println("Move up");
         break;

      case KeyEvent.VK_DOWN:
         currentSprite = characterDown;
         if (robot.isJumped() == false) {
            robot.setDucked(true);
            robot.setSpeedX(0);
         }
         break;

      case KeyEvent.VK_LEFT:
         robot.moveLeft();
         robot.setMovingLeft(true);
         break;

      case KeyEvent.VK_RIGHT:
         robot.moveRight();
         robot.setMovingRight(true);
         break;

      case KeyEvent.VK_SPACE:
         robot.jump();
         break;

      }

   }

   @Override
   public void keyReleased(KeyEvent e) {
      switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
         System.out.println("Stop moving up");
         break;

      case KeyEvent.VK_DOWN:
         currentSprite = character;
         robot.setDucked(false);
         break;

      case KeyEvent.VK_LEFT:
         robot.stopLeft();
         break;

      case KeyEvent.VK_RIGHT:
         robot.stopRight();
         break;

      case KeyEvent.VK_SPACE:
         break;

      }

   }

   @Override
   public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub

   }

   public static Background getBg1() {
      return bg1;
   }

   public static Background getBg2() {
      return bg2;
   }

}


My question is: can I somehow set DOUBLE variable to Thread.sleep()? As far as I understand I need to get Thread.sleep(16.6) somehow? I tried to switch monitor refresh rate to 50 hz and set sleep time to 20 nano seconds. It completely eliminated stuttering. But what should I do with 60hz if double is not possible?
Offline CTucker1327
« Reply #1 - Posted 2013-12-13 02:41:14 »

First, I'd look into a BufferStrategy

http://gpsnippets.blogspot.com/2010/10/how-to-use-bufferstrategy-in-java.html

Second, I would use a timer instead of Thread.Sleep.


Something like this

1  
2  
3  
4  
if((System.currentTimeMillis() - milisecondsAmount) > lastUpdate) {
    doStuff();
    lastUpdate = System.currentTimeMillis();
}


where, the milisecondsAmount would be the amount of time needed to "sleep"

Example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   @Override
   public void run() {
      while (true) {
    if((System.currentTimeMillis - 166) > lastUpdate) {
         robot.update();
         if (robot.isJumped()) {
            currentSprite = characterJumped;
         } else if (robot.isJumped() == false && robot.isDucked() == false) {
            currentSprite = character;
         }
         bg1.update();
         bg2.update();
         repaint();
   lastUpdate = System.currentTimeMillis();
   }
      }
   }


Sorry for conventions, did that in notepad.
Offline nA

Senior Newbie





« Reply #2 - Posted 2013-12-13 08:41:48 »

Thanks a lot for your answer!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
private static long lastUpdate = System.currentTimeMillis();

public void run() {
      while (true) {
         if ((System.currentTimeMillis() - 166) > lastUpdate) {
            robot.update();
            if (robot.isJumped()) {
               currentSprite = characterJumped;
            } else if (robot.isJumped() == false
                  && robot.isDucked() == false) {
               currentSprite = character;
            }
            bg1.update();
            bg2.update();
            repaint();
            lastUpdate = System.currentTimeMillis();
         }
      }
   }

I did as you mentioned but can you tell me more about System.currentTimeMillis - 166? If I leave it as it is then I see 2~ frames per second instead of 60. Everything is super slow. If I change it to 17 the speed is normal but It stutters as it does with sleep method.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2013-12-13 08:49:08 »

Use system.nanoTime() much more accurate.

Offline CTucker1327
« Reply #4 - Posted 2013-12-13 09:22:50 »

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  
private long lastRender;
   private int frames;
   private final int FRAME_CAP = 60;
   
   public void run() {
      while(true) {
         update();
         if((System.currentTimeMillis() - (1000 / FRAME_CAP) > lastRender))
            render();
      }
   }
   
   private void update() {
        robot.update();
        if (robot.isJumped()) {
           currentSprite = characterJumped;
        } else if (robot.isJumped() == false
              && robot.isDucked() == false) {
           currentSprite = character;
        }
        bg1.update();
        bg2.update();
   }
   
   private void render() {
      repaint();
      lastRender = System.currentTimeMillis();
   }



This calculates the game logic as fast as possible, but allows it to only draw as fast as the frame cap.
Offline nA

Senior Newbie





« Reply #5 - Posted 2013-12-15 18:49:45 »

Thanks a lot for trying to help me!
After modifying my class the code looks like this:
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  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
package kiloboltgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class StartingClass extends Applet implements Runnable, KeyListener {

   private Robot robot;
   private Image image, currentSprite, character, characterDown,
         characterJumped, background;
   private Graphics second;
   private URL base;
   private static Background bg1, bg2;
   private long lastRender;
   private int frames;
   private final int FRAME_CAP = 60;

   @Override
   public void init() {

      setSize(800, 480);
      setBackground(Color.BLACK);
      setFocusable(true);
      addKeyListener(this);
      Frame frame = (Frame) this.getParent().getParent();
      frame.setTitle("Q-Bot Alpha");
      try {
         base = getDocumentBase();
      } catch (Exception e) {
         // TODO: handle exception
     }

      // Image Setups
     character = getImage(base, "data/character.png");
      characterDown = getImage(base, "data/down.png");
      characterJumped = getImage(base, "data/jumped.png");
      currentSprite = character;
      background = getImage(base, "data/background.png");
   }

   @Override
   public void start() {

      bg1 = new Background(0, 0);
      bg2 = new Background(2160, 0);
      robot = new Robot();

      Thread thread = new Thread(this);
      thread.start();
   }

   @Override
   public void stop() {
      // TODO Auto-generated method stub
  }

   @Override
   public void destroy() {
      // TODO Auto-generated method stub
  }

   @Override
   public void run() {
      while (true) {
         update();
         if ((System.currentTimeMillis() - (1000 / FRAME_CAP) > lastRender))
            render();
      }
   }

   @Override
   public void update(Graphics g) {
      if (image == null) {
         image = createImage(this.getWidth(), this.getHeight());
         second = image.getGraphics();
      }

      second.setColor(getBackground());
      second.fillRect(0, 0, getWidth(), getHeight());
      second.setColor(getForeground());
      paint(second);

      g.drawImage(image, 0, 0, this);

   }

   private void update() {
      robot.update();
      if (robot.isJumped()) {
         currentSprite = characterJumped;
      } else if (robot.isJumped() == false && robot.isDucked() == false) {
         currentSprite = character;
      }
      bg1.update();
      bg2.update();
   }

   private void render() {
      repaint();
      lastRender = System.currentTimeMillis();
   }

   @Override
   public void paint(Graphics g) {
      g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
      g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
      g.drawImage(currentSprite, robot.getCenterX() - 61,
            robot.getCenterY() - 63, this);

   }

   @Override
   public void keyPressed(KeyEvent e) {

      switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
         System.out.println("Move up");
         break;

      case KeyEvent.VK_DOWN:
         currentSprite = characterDown;
         if (robot.isJumped() == false) {
            robot.setDucked(true);
            robot.setSpeedX(0);
         }
         break;

      case KeyEvent.VK_LEFT:
         robot.moveLeft();
         robot.setMovingLeft(true);
         break;

      case KeyEvent.VK_RIGHT:
         robot.moveRight();
         robot.setMovingRight(true);
         break;

      case KeyEvent.VK_SPACE:
         robot.jump();
         break;

      }

   }

   @Override
   public void keyReleased(KeyEvent e) {
      switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
         System.out.println("Stop moving up");
         break;

      case KeyEvent.VK_DOWN:
         currentSprite = character;
         robot.setDucked(false);
         break;

      case KeyEvent.VK_LEFT:
         robot.stopLeft();
         break;

      case KeyEvent.VK_RIGHT:
         robot.stopRight();
         break;

      case KeyEvent.VK_SPACE:
         break;

      }

   }

   @Override
   public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub

   }

   public static Background getBg1() {
      return bg1;
   }

   public static Background getBg2() {
      return bg2;
   }

}


Now everything runs VERY fast Sad. Should I delete override update method or else?
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #6 - Posted 2013-12-15 21:59:25 »

Off Topic:
I highly recommend spending at the very least 2-3 weeks (though 1-2 months is better) reading through The Java Tutorials before trying to make a game. It may be boring, but trying to make a game without any basic programming knowledge is a mistake that many people make. Copy & pasting code/ typing in code that you don't understand in depth won't help in the long term. Create simple useless stuff like calculator programs while reading the tutorials and I assure you that you'll get a much better understanding of the language. If you don't understand something properly do a lot of research on the topic.

Once you've got to the stage of understanding data types, operators, objects and basic inheritance, creating simple pong/snake/maze games is the way to go. Trust me; if you have a passion to want to create games, a month is nothing.

Why are all OpenGL tutorials written in Brainf**k?
Offline CTucker1327
« Reply #7 - Posted 2013-12-16 12:05:20 »

It's going to run fast, you're processing the games logic as fast as possible.
If you want certain things to be delayed, you'll have to delay them yourself, or look into doing movements based on deltatime.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (30 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!