fps standarization issue

Started by daniel_nieto, February 26, 2011, 22:22:53

Previous topic - Next topic

daniel_nieto

hello i just want to ask how can i have a game that runs at the same velocity on different computers, basically i have a 2d sprite and it's moving 1px every iteration of main loop, it's synchronized at 1000 fps and with vsync enabled, i works really fast on my desktop pc but on computers from my school runs very slow, in my pc the sprite takes 1second to go from left to right completely, on other computer it takes 9seconds or more... what can i do to make the sprite always move with the same velocity???

avm1979

Short answer: put Display.sync(60) in your game loop. 

That'll set it to run at 60 frames per second, regardless of computer speed.  Unless each iteration ends up taking longer than 1/60ths of a second, in which case it'll go slower, but it doesn't sound like you have that problem.

Longer answer: you want to have each loop iteration take a certain amount of time, and move your sprite based on the time elapsed.  Rendering and advancing the game state will take some of that time, which needs to be kept track of.  You'll need to Thread.sleep() for the rest of that time.  Display.sync() takes care of all this for you.  You'll still need to change your logic to move things based on time and not frames elapsed.

For example, if you decided to go with 60 fps, you might have (pseudo code):
float elapsed = 1f/60f; // 1/60th of a second elapses every frame
Vector2f velocity = new Vector2f(100f, 0); // speed is 100 units per second

sprite.x += velocity.x * elapsed;
sprite.y += velocity.y * elapsed;


Make sense?

kappa

you can also have read up on the LWJGL Basics tutorials found here. Part 4 should answer your question.

arielsan

If you want even more information related to this timing issue, go to http://gafferongames.com/game-physics/fix-your-timestep/.