I would look at this; it’ll probably help.
In my own words:
There a few different ways you can have your game cycle through it’s updates. You can have a generic while loop that updates the game logic and renders on the same cycle, like so -
while (running)
{
updateGameLogic();
render();
}
This is a horrible way to do it. When you do it like this, it is only rendering when it is ticking, which will make the game feel like crap on a slower computer.
Here is a good way to do it: seperate the rendering and ticking out into different methods, and only let it tick after a certain amount of time.
while (running)
{
long lastTime = System.nanoTime();
while (playing)
{
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (unprocessed >= 1)
{
tick();
unprocessed -= 1;
shouldRender = true;
}
Thread.yield();
if (shouldRender)
{
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer1 > 1000)
{
lastTimer1 += 1000;
frames = 0;
ticks = 0;
}
}
}
THAT ^^ is how you should do it. It won’t fix stuttering on machines that can’t render as fast (the fps won’t be as fast) but it will tick as often, since updating the game logic requires few resources.
Hope this helps.
-Nathan