keep the same speed

Hi all!!

Im an opengl beginner, and i dont know how to keep the same speed of an opengl aplication/game.

I search a lot on internet about it, but i have not find the answer.

I have looked for “gluttimerfunc” and “timer” in opengl, i have found things, but only about to repeat something, not to control the speed of the game.

I want to control it because i want the same speed on all PCs, and i want the same speed in all game (not depending of the things i have loaded in the screen).

Sorry for my english, and thanks a lot !! :wink:

OpenGL is not for managing mouse/keyboard/timers etc. So you should probably search for solution for SDK of your platform.
If it’s Windows, then use QueryPerformanceCounter and QueryPerformanceFrequency.
Each frame you push physics/ai and all other updates forward by the amount of ime that have actually passed and render current state.
If you use GLUT, GLFW, SDL or other library then search for timer support in that library’s documentation. The better library you use, the better timer support it will probably have.

I use GLUT, beacause of that i have search about “gluttimerfunc”.

But i have not found an example or something using that function to keep the same global speed of the game.

Here is a code snippet using glutTimerFunc().

  1. Register your timer callback function.
  2. Call glutPostRedisplay() inside your callback to draw the scene
// register glut callback functions
glutTimerFunc(33, timerCB, 33); // redraw every 33 millisec (30fps)

void timerCB(int millisec)
    glutTimerFunc(millisec, timerCB, millisec); // for next timer event


songho: that’s not the way to do it. GLUT specs state that: “The number of milliseconds is a lower bound on the time before the callback is generated”, so using glutTimerFunc makes your application work no faster than you decide, but it will not prevent it from going slower. So if you setup your timer to update your game at 30FPS you will limit your game to 30FPS and additionally if hardware is too slow for 30FPS all action will slow down and this is exactly what mi-go is trying to avoid.

The proper way is to post redisplay as fast as you can, but measure time passed since last frame and update all physics by that time.
AFAIK there is no other timer support in GLUT so you have to use some plaform-dependent solution like QueryPerformanceCounter.

You are totally right. But I just explained how to use glutTimerFunc().

And, it is not always necessary running fullspeed in a certain application, such as a GUI application with a small rendering window. Actually, the limited framerates are desired for this case.

I also have a high-resolution timer class to measure elapsed times in 1 micro-sec accuracy. (QueryPerformanceCounter() for Windows, gettimeofday() for *nix). Anyone who is interested in, get it from:

You propably mean 1 us resolution. I doubt there is any operating system that can really provide 1 us accuracy :wink:

You need to factor elapsed time into your movement equations, the simplest way is to measure elapsed time since the last update and multiply all movement by that elapsed time.

Depending on how sophisticated your motion model is you may have to do more like factor time into accelerations etc, but is should at least be clear now how this is done.

Thanks all !!!

Because im a beginner and the game im doing now is simple, I will use now the GlutTimeFunc like Songho has said; only to limit the game go faster than I want. I think all PC would run the game without problem.

But now… im thinking…
that will work like i want?
The movement in the game will be allways the same?

I mean… usign the GlutTimeFunc maybe the game still run faster (the movement) but only a 30FPS.
Those are diferents things, arent those?
The speed of the game is a thing and the FPS that im showing is another, isnt it?

I think is more complicated, and i have to adjust all the movements in the game, but im not sure.
Maybe i have to do that dorbie said, that i think is the same that this page of NeHe:
(see the use of “adjust”)

Don’t use glutTimerFunc to force the rendering, simply use glutPostRedisplay at the end of your rendering function.
Use glutTimerFunc to update your objects position only.

This is simply because you can’t expect all computers to render at least at 30 fps. Some will be able, and some won’t. If you’re pretty sure your computers will be fast enough, then you can try to force the rendering at 1/30 sec or anything else, but this is generally not advised for games. You don’t want to make a real-time movie, do you ?

yes… i think is the better way… use glutTimerFunc to update my objects position.

I think it will work fine!