2
votes

background information

I've been in recent development of a game using LWJGL and I was curious as to how many frames per second I could get from running the game without using the Display.sync(60) function (Which limits fps to 60). Upon commenting out the code, I stumbled upon the game speed updating at 9000 some fps, which, in turn, made the game tick run 9 thousand times per second.

question

How should I implement a game timer to prevent the fps creating issues with how fast the game actually runs? I am aware I need to separate the two timers, but how/which way is the most efficient of going about doing so (in java)?

1
See chapter 2 of Killer Game Programming in Java from fivedots.coe.psu.ac.th/~ad/jgvandale
Pretty informative read. I appreciate the help. The diagram was similar to what I was looking for.Ramity

1 Answers

3
votes

TheCodingUniverse offers a great tutorial here: http://thecodinguniverse.com/lwjgl-frame-rate-independent-movement/

However, the basic answer is that you move the frame by saving the last System.currentTime() and then compare it to this System.currentTime(). Then when moving it would be like object.move(x * delta, y * delta);

To quote from the above link,

// Under the class definition
private static long lastFrame;
private static long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
private static double getDelta() {
long currentTime = getTime();
double delta = (double) currentTime - (double) lastFrame;
lastFrame = getTime();
return delta;
}
// In initialization code
lastFrame = getTime();
// In game loop
long delta = getDelta();
movement_x += dx * delta;

I hope that helps :).