# Game Timer / Game Clock

This topic is 860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello!

Well I am trying to implement a game clock to my game so that the rendering and the updating of the player is based upon the elapsed time. The problem is that the elapsed time varies a little bit every frame so when I am multiplying with the elapsed time the player is sort of hacking. Now is moving forward but since the velocity vector is not the same every frame it is sort of hacking forward. If I change the multiplication of the variable "elapsedTime" with a constant value the  movement is smooth. I don't know how to fix this issue because I don't want the player to move the same amount every frame. I want to player to move based upon the elapsed time.

Here is some relevant code to look at: http://pastebin.com/f064xR7e

By hacking I mean non smooth movement. It looks like the player is moving 3 steps forward and 1 step back or something like that.

Regards!

Edited by Skyzyx

##### Share on other sites

(I'm guessing that when you say "hacking" you mean "non-smooth movement" - hacking normally means something else around here.)

One approach is to fix your timestep - http://gafferongames.com/game-physics/fix-your-timestep/

Generally speaking, if your frame rate is quite high, the fact that it varies a little from frame to frame shouldn't matter and won't make your movement uneven. Lots of games have shipped with variable frame rates and they looked fine. Sometimes in 2D games the problem is actually about the rendering - if your position is in floating point but you render to integer coordinates, then the truncation of the float to an int makes the perceived motion look jerky.

##### Share on other sites

In the code you pasted I can't see where you calculate the floating point value for elapsed time that you pass to setPlayerTimeBasedVelocity().

I think you might be doing too much integer math, like not converting to floats early enough. For example, in your routine CalculateElapsedTimeMS() you are using a high precision timer but then convert to milliseconds at the end with an integer divide by a million, which will throw away any extra precision you were getting from System.nanoTime. Try making that routine return a double and do all your time and velocity math as floating point values everywhere.

Edited by jwezorek

##### Share on other sites

I tried changing it to floating point values but the issue still remains. Note that I changed from milliseconds to seconds now: http://pastebin.com/vegNcwdM

But like you said. The difference is very small so I don't understand why this is an issue. You can see a few calculated values of elapsed time in seconds below.

0.014438041
0.015371792
0.016594417
0.016946709
0.024986375
0.012801459

##### Share on other sites

You're not showing the rendering code. If you're rendering to integer pixel boundaries then movement won't appear smooth.

##### Share on other sites

Ok I don't think that I am rendering to integer pixel boundaries but here is the rendering code. I have not linked my shaders though but the only thing I am doing in them is setting the color and multiplying by the projection matrix. Also the velocity vector doesn't need to have integer values. Even if I set the values to like (0.154f, 0.78f, 0.14784f) it works fine as long as the velocity vector is constant and not varying according to the time variable. I hope I have provided you with enough information and thanks for the help btw!

https://vimeo.com/177815657

GameClock:          http://pastebin.com/Qftx5A8P

Player:                   http://pastebin.com/DxNi3wU7

Camera:                http://pastebin.com/0muxcE75

MyGLRenderer:    http://pastebin.com/tPetWSEn

Edited by Skyzyx

##### Share on other sites

Okay, so it's not 2D, which wasn't clear from the original post.

It's interesting, looking at your elapsed time values, that you're getting some frames taking twice as long as others. People tend to perceive changes of frame rate more than they perceive slowness so it might be the case that the movement is smooth but it appears otherwise.

Secondly, there's clearly some vertical 'bumping' going on which is unrelated to the forward velocity, and that makes it look worse. Can you disable that and see how it feels? Or is that actually the problem, and you don't know how that is happening?

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013601
×