Tag Archives: frame-independent

LWJGL Frame-Rate Independent Movement

By the end of this tutorial you will have learned what frame-rate independent movement is and how you can implement it.

Why use LWJGL frame-rate independent movement?

In 1995 developers wrote their games to run as fast as they possibly could. This worked well when they were working with one machine, but it turned out that every time a faster computer was released, the game suddenly ran a lot faster. This is why the system clock is used to determine how fast the game should run.

How does it work?

Firstly, the system time is stored in a variable of type long called ‘lastFrame’. ┬áThen, in the game loop, the amount of time that has passed since the last frame is retrieved and returned. The code looks like this.

// 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;

The delta variable represents the elapsed time since the last frame update. The higher the delta is, the lower the frame-rate will be. The lower the delta is, the higher the frame-rate will be. If the frame-rate is capped to 60 frames-per-second, a delta value lower than 16 should never exist.

Conclusion

This concludes the tutorial for frame-independant movement. Should you have any questions or remarks, put them in the comments section below.