2.2.3 Delta Time

The exercise above has one major problem: the position will be updated at every frame. Should you test this application on two different computer, one with a good graphics card and 60FPS, and another without a dedicated graphics card running at 30FPS, the movement will be twice as fast on the good computer! We won’t mind for a small exercise, but this could be a serious gaming disadvantage for the girl with the slower computer.

We can fix this problem with the concept of delta time. The delta time is the time that has passed between this update and the previous one. (Remember the mouse wheel delta, being the distance traveled by the mouse wheel between this frame and the previous one.) The delta time can be retrieved with the next method:

 
Time.d();  

Would you like to move an object at a speed of 1 unit per second? Try this code:

 
if(Kb.b(KB_RIGHT)) point.x += 1 * Time.d();  

Exercise
Adapt the previous exercise, so that the delta time is used. Once you’ve tested your app, replace the number one with a float variable defined on top of your application. Use two extra keys to alter the variable and hence control the movement speed.