Need constant speed in my game

Started by
5 comments, last by Term 14 years, 10 months ago
i have a problem with my c++ program using opengl graphics. in my game, you can move and turn some shapes and my problem is, that this does not work with a constant speed. the speed of all my animations and stuff depends on the cpu. when there is much to calculate, the whole game becomes slower and if the cpu does not have to calculate much things it runs faster. what is the main technique using opengl, with which you can claim a constant speed in your game? i wish that my game only sets down the FPS and not the tempo of the game!!! how can i achieve this? i'm using the GLUT FRAMEWORK. all animations are calculated in the callback function of the GlutIdleFunc. at the end of the GlutIdleFunc there is a usleep(1000) comment, which should work like "every 1 ms this function should be called." but now, if the calculations in the game are too much, this function is called later than every 1 ms. what is the main trick one should do, to achieve a constant speed in a game (with a dynamic framerate of course...) like in every commercial game.
Advertisement
You need to figure out the elapsed time since the last frame, and then move your characters/objects based on that time. For instance, if our character should move 10 units/second along the X axis:
int prevFrameTime = 0;while (1) { // game loop		int curTime = glutGet(GLUT_ELAPSED_TIME); // glut time is in milliseconds		double elapsedTime = (curTime - prevFrameTime)/1000.0; // calculate seconds since last frame		character.x += 10.0 * elapsed;		prevFrameTime = curTime; // current time will be prev time for next frame}

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

In plainest terms, you need a timer.

Telling GLUT to call your function after a specific time will not work because, as you pointed out, the function may take longer to execute than the time you allotted it. In such a case, you need to calculate how long your frame is taking to render and update accordingly.

I will present a generic example, for illustration purposes only. (actualy usage may vary based on various factors). Say you require a frame-rate of 30fps, each frame should take 1/30 (or 0.033) seconds. Say you call the required frame-rate ReqF. Hence ReqF = 0.33 At the begining of the frame you set the time to T. At the end of the frame, you set the time to NewT. The time your frame took to render is now DeltaT = NewT - T. Supposed you found out that DeltaT is 0.20. In that case you still have ReqF-DeltaT time left before you should call the next frame. You can either do some other calculations meanwhile, or simply put your function to sleep for that amount of time: Sleep(ReqF-DeltaT).

though not directly related, have a look here on how to calculate FPS: http://www.gamedev.net/community/forums/topic.asp?topic_id=44929

I hope that helps.

kind regards,
Sajid
@SwiftCoder: I think the OP wanted to maintain a fixed FPS while your code maintains a fixed game-speed (tempo) but variable fps?
Quote:Original post by Fugitive
@SwiftCoder: I think the OP wanted to maintain a fixed FPS while your code maintains a fixed game-speed (tempo) but variable fps?
You are correct that this fixes the speed (tempo), rather than the frame-rate. However, I believe this was what the OP wanted - though his wording is a little unclear.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by Term
all animations are calculated in the callback function of the GlutIdleFunc. at the end of the GlutIdleFunc there is a usleep(1000) comment, which should work like "every 1 ms this function should be called."


You have fallen in to the trap of not understanding how sleep works. Calling usleep(1000) says "stop executing my program for at least this long and perhaps longer". If you call usleep(1000) and your app takes 3hrs to start running again it has followed the correct definition of usleep. If you call usleep(1000) and your app starts running in 999 useconds then usleep is faulty. In other words Don't use sleep or any of its derivatives for this kind of thing.
yeah, thanks. i really knew, that usleep is a bad solution for the problem of a correct animationflow.

i implemented your ideas and it works perfect, guys. thanks for your help!

i now just store every clockinformation and it's absolutely irrelevant how often the idle function is called during the time, because every animation now only depends on the clocks. i don't have to care about fps and stuff and the animations have a precise timing.

This topic is closed to new replies.

Advertisement