I'm experimenting with building a game engine from scratch in Java, and I have a couple questions. My main game loop looks like this:
int FPS = 60;
while(isRunning){
/* Current time, before frame update */
long time = System.currentTimeMillis();
update();
draw();
/* How long each frame should last - time it took for one frame */
long delay = (1000 / FPS) - (System.currentTimeMillis() - time);
if(delay > 0){
try{
Thread.sleep(delay);
}catch(Exception e){};
}
}
As you can see, I've set the framerate at 60FPS, which is used in the delay
calculation. The delay makes sure that each frame takes the same amount of time before rendering the next. In my update()
function I do x++
which increases the horizontal value of a graphics object I draw with the following:
bbg.drawOval(x,40,20,20);
What confuses me is the speed. when I set FPS
to 150, the rendered circle goes across the speed really fast, while setting the FPS
to 30 moves across the screen at half the speed. Doesn't framerate just affect the "smoothness" of the rendering and not the speed of objects being rendered? I think I'm missing a big part, I'd love some clarification.
1000 / FPS
division could be done and the result assigned to a variable before yourwhile(isRunning)
loop. This helps save a couple of CPU instruction for doing something more than once uselessly. – Vaillancourt Apr 06 '15 at 19:19