Public Group

# Need help with animation using glut

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

## Recommended Posts

Hi, everybody. I have a task of writing an Atari Asteroids type of game using the platform independent glut library. The work has begun, but the more code I write the more there are questions to appear. Most of them concerning implementing a smooth animation. So, ok, there is an idle callback function glutIdleFunc() and there is glutTimerFunc() function also. Using the last one didn't satisfy me, because it is kinda lame for the purpose, it still can be used to mark some time intervals though, but not for frame by frame animation. The first one is ok, but it has no brakes, so i have to use windows Sleep(int msecs) function. But I have a strong fealing that I'm doing something wrong, not the way it should be done. And the animation in both cases isn't that smooth. I always notice some flaws, skipping frames for example. I tried and looked for some glut tutorials on the net, but all of them were either too simple (as a crazy fast rotating cube for example, in this case the glutIdle() without brakes was used) or not about glut but about windows API timer functions instead. First of all I would like to ask, if some of you have a working example of a game written on glut with a source code, please do not hesitate and send it on my e-mail. I would really appreciate that. And of course I'm asking you to explain me what should I do to get my game to run smooth? Thanks a lot in advance. P.S. Sorry for my English. P.P.S. Any good links on the topic are welcome. [Edited by - Tamior on August 10, 2005 1:13:54 PM]

##### Share on other sites
Animation should always be related to timers and not the framerate. The best way to do this, in my opinion, is not by using glutTimerFunc. I personally always use a function that uses the current time to calculate how much an object should transform. For example, i define a cube that must move from position A to B in 3 seconds. Animation can then be performed by getting the start time at point A. Then during each frame (e.g. in the Idle function) I calculate the current time and place the cube at A + (B - A) * ((tcur - tstart) / ttotal). Where A and B are the position vectors, tcur the current time, tstart the start time and ttotal the duration of the entire animation (3 sec. in my example). And of course I make sure animation stops when ((tcur - tstart) / ttotal) exceeds 1.0.
Obviously, there are many other possibilities.

Tom

##### Share on other sites
Ok, but what to count time with? As I understand there are no platform independent ways to implement this, at least using glut (or am I wrong?). So what functions I should use in Windows for this purpose?
And then again what to do with interactive animation (which is the major concern in any game), when you don't know where exactly some controllable entity should be and what time it will take it to get there, how many stops and turns will be there. With that in mind it seems much more difficult. And again I'm asking for a good working example. Please mail to thamior at mail.ru.

##### Share on other sites
Quote:
 Original post by TamiorOk, but what to count time with? As I understand there are no platform independent ways to implement this, at least using glut (or am I wrong?). So what functions I should use in Windows for this purpose?And then again what to do with interactive animation (which is the major concern in any game), when you don't know where exactly some controllable entity should be and what time it will take it to get there, how many stops and turns will be there. With that in mind it seems much more difficult. And again I'm asking for a good working example. Please mail to thamior at mail.ru.Nevertheless, thank you Tom. I just need more information.

To count the time, I use this function (it ain't pretty but works on linux and windows):
#if defined(WIN32)LARGE_INTEGER	perf_freq;LARGE_INTEGER	perf_start;#else // Linux/Unix/Irixstruct timeval  tv_start;#endif// during initialization of your program:void aInitialize(){#if defined(WIN32)	QueryPerformanceCounter(&perf_start);	QueryPerformanceFrequency(&perf_freq);#else // Linux/Unix/Irix	gettimeofday(&tv_start,0);#endif}// get time in secondsdouble aTime(){#if defined(WIN32)	LARGE_INTEGER perf_counter;	QueryPerformanceCounter(&perf_counter);	return (double)(perf_counter.QuadPart - perf_start.QuadPart) / (double)perf_freq.QuadPart;#else // Linux/Unix/Irix	struct timeval tv;	gettimeofday(&tv,0);	return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0;#endif}

I agree that for interactive animation, my idea doesn't work. In that case, you could, for example, measure the time of the previous frame(s) and use that to determine the stepsize of the camera controlled by the user to make sure the speed of the camera (in world units/second) is more or less stable. This applies to other interactive objects as well. Anyway, these are simple examples. I don't have more complex ones. Perhaps free game engines like Irrlicht contain examples that are more complex.

Tom

##### Share on other sites
Thanks again for not loosing interest in my problem, Tom, but I'm sure there must be a simpler solution to this problem, because the game itself is very simple, it is just plain asteroids, not some elaborate first person shooter.

##### Share on other sites
For asteroids, just use a timer callback to maintain your framerate. The function will call glutPostRedisplay() and will be called every 30 ms.

<div class="source">
// Register it with GLUT in the beginning of your application// ...glutTimerFunc(30, timer, 0)// have the function look like this:void timer(int value){   glutPostRedisplay();   glutTimerFunc(30, timer, 0);   return;}

Voila now you are posting redisplay signals approximately every 30 milliseconds.

"Ok, but what to count time with? As I understand there are no platform independent ways to implement this, at least using glut (or am I wrong?)."

glutGet(GLUT_ELAPSED_TIME) will return the number of milliseconds elapsed since the beginning of your application. This can be very useful and is platform independent.

Mark

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633332
• Total Posts
3011402
• ### Who's Online (See full list)

There are no registered users currently online

×