Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Slick Jittery Movement  (Read 2346 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2011-08-23 04:11:30 »

Posted this on Slick Forum too but its just so mind boggling

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  
public final class LolGame extends BasicGame { 
     float xPos = 100.0f;
     float yPos = 100.0f;
     float xChange = 4.0f;
     float yChange = 5.0f;
     
     public LolGame() {
          super("Jitter in da house");
     }
     
    @Override
     public void init(GameContainer gc) throws SlickException {
          gc.setForceExit(true);
         gc.setShowFPS(true);
         gc.setClearEachFrame(true);
         gc.setTargetFrameRate(60);
         gc.setVSync(true);
     }
     
     public static void main(String[] args) throws SlickException {
         AppGameContainer app = new AppGameContainer( new LolGame() );
         app.setDisplayMode(800, 600, false);
         app.start();
         System.exit(0);
     }

     @Override
     public void update(GameContainer gc, int delta) throws SlickException { }
     
     @Override
     public void render(GameContainer gc, Graphics g) throws SlickException {
        g.setColor(Color.white);
        g.fillOval(xPos, yPos, 20, 20);
       xPos+=xChange;
        yPos+=yChange;
        if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
        if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange;
    }
 }


this will jitter... why ?

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 76
Projects: 15


★★★★★


« Reply #1 - Posted 2011-08-23 09:08:51 »

its probably because you're updating your logic in the render method and not controlling it via the delta value, not doing this will make movement dependant on the fps thus jittery as the fps moves up or down.

What you should try is remove the following from the render method
1  
2  
3  
4  
xPos+=xChange;
yPos+=yChange;
if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange;


and put the following in the update method

1  
2  
3  
4  
5  
xPos += xChange * delta;
yPos += yChange * delta;

if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange;


You'll probably also need to lower the xCharge and yCharge values to slow down the speed to what you want as its now multiplied by delta.
Offline Cero
« Reply #2 - Posted 2011-08-23 11:09:38 »

ok now I know this is how you're supposed to do it, but how could you ever predict what delta would be

when lowering changeX I would have to know

Also, I just tried it
using * delta
and using setsmoothupdate(true) and * delta

both times I print out the delta each frame to see it.

without smoothupdate they range from 15 to sometimes 35
with it, it seems stead at 16

and here is the kicker: it doesn't matter, both stil jitter

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
float xChange = 0.4f;
float yChange = 0.5f;
...
gc.setSmoothDeltas(true); // or not, doesn't matter actually
...
public void update(GameContainer gc, int delta) throws SlickException {
       xPos+=xChange * delta;
       yPos+=yChange * delta;
       if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
       if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange;
    }


still jitters D=


And whats strange is that, i wanted to make a small new game, and got this behavior. However my main game doesn't have that problem.
My main game also only uses the render and not the update, because I knew of the delta, but I ported my whole game from Java2D, and obviously changing all the values in the game to fit delta would be outrageous.
And my game works fine. Even if I copy this ball bouncing code into my game, it doesn't jitter.
I think its because there is so much going on and in this empty example not, so that fps changes are greater here.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 76
Projects: 15


★★★★★


« Reply #3 - Posted 2011-08-23 11:25:30 »

ok now I know this is how you're supposed to do it, but how could you ever predict what delta would be
You don't, the whole point is that its suppose to be variable so that it adjusts your logic to move at a constant pace.

try without the lines gc.setTargetFrameRate(60); and gc.setVSync(true); to see if you can get rid of the jitter, should help narrow down where the issue is.

You can also try setting gc.setMaximumLogicUpdateInterval(50) or even 30 to avoid any big jumps, so that the delta doesn't go too big thus avoid jumping.

My main game also only uses the render and not the update, because I knew of the delta, but I ported my whole game from Java2D, and obviously changing all the values in the game to fit delta would be outrageous.
Sounds like you were doing it wrong with Java2D too, being frame dependant on one system might work fine, but results would probably be different on another computer.
Offline Cero
« Reply #4 - Posted 2011-08-23 11:38:07 »

try without the lines gc.setTargetFrameRate(60); and gc.setVSync(true); to see if you can get rid of the jitter, should help narrow down where the issue is.

Seems to have no jitter now, with 4000+ frames
but a soon as I enable vsync or sync to framerate, it jitters again

using gc.setMaximumLogicUpdateInterval(30); its better but still jitters here and there

I mean this is as basic as it gets, it should be straightforward to get it right

Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #5 - Posted 2011-08-23 11:42:33 »

You're right, you cannot know the delta beforehand. But you probably know how fast you want your objects to move, and from there you can say something like: "I know how far I want my ball to move in 1 second". Lets say you want to move 5 pixels in 1 second (1000 ms). You take 5/1000, and then you know your multiplicator. In this instance with 5 pixels pr second, it would be x += 0,005 * delta

Offline Cero
« Reply #6 - Posted 2011-08-23 17:21:38 »

meaning that if I have value of change now, I do this value / 16.666 * delta so get the result I want
1000ms / 60 fps = 16.666
and it does work, obviously
still some jitter... but like I thought the busier a game gets, the more this problem vanishes.

I never thought in movement per time, but per frame, and since I force 60 fps, it seems fixed.
god I hate working with different hardware / PCs sometimes, consoles don't have this problem D=

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.

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

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

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

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

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

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

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

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

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
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!