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.

12 thoughts on “LWJGL Frame-Rate Independent Movement

  1. Jada

    I read a lot of interesting articles here. Probably you spend a lot
    of time writing, i know how to save you a lot of time, there is
    an online tool that creates high quality, SEO friendly posts
    in minutes, just type in google – laranitas free content source

    Reply
  2. Roman

    It’s hard to find your blog in google. I found it on 15 spot, you should build quality backlinks ,
    it will help you to get more visitors. I know how to help you, just search
    in google – k2 seo tips and tricks

    Reply
  3. shori zand

    Nice post. I was checking continuously this blog and I am impressed! Very useful information specially the last part :) I care for such info a lot. I was seeking this certain information for a long time. Thank you and good luck.

    Reply
  4. SCOPE

    I have been making prototype games for a while and never knew this before. Well this is a curse and a dream. Now I have to modify all my previous games which probably relied on old fashioned programming methods like frame counts. Well thanks for the tutorial… I did wonder why my games ran different on different machines seeing as they were all coded based off my computer which is very fast…

    Reply
  5. Nate Steger

    Delta is important because each persons computer runs slightly different. Delta ensures each movement will be virtually identical across each PC. Any variance is negligible and practically undetectable to the naked eye. All modern games calculate framerate like this from indie games like Minecraft to AAA studio titles.

    Reply

Leave a Reply to shori zand Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>