Problem with timers and FPS

Started by
12 comments, last by Etus 21 years, 2 months ago
Hello, I have a problem regarding camera movement and animations. I''m currently developing a demo for NeHe''s contest, and I bumped into a problem - the movement and animation of things are tied to the FPS. I want to make the speed of movement and animation fixed - I don''t want it to change when the FPS changes. I looked at several tutorials, but none helped me. I want to create a speed that looks like 26 FPS. Thanks, Yuval
Advertisement
It's curious, because the Nehe Base code uses an update function wich has a parameter that contains the time ellapsed since the previous call, thus you can calculate an animation without being tied to FPS.

[edit] ah, maybe you don't use the base code, in this case just have a look in the source.

----
David Sporn AKA Sporniket

[edited by - davidsporn on January 22, 2003 4:55:39 AM]
Thanks a lot - I didn''t know that.
I was using a very old version of NeHe''s basecode.

Anyway, it''s working now.

- Yuval
There are several ways to fix this problem. I haven''t looked up the updated code, but i ran into a similar problem before. The easiest way i found, was to base movement on the FPS. If its lower then you want, the unit of movement is increased. If the fps is higher then you want, the unit of movement is lowered. I dont know how nehe did it but i wrote a little thing about it a few threads down, the guy that said somthing about the CPU being used 95%.
quote:Original post by ThePretender
I dont know how nehe did it but i wrote a little thing about it a few threads down, the guy that said somthing about the CPU being used 95%.


From my memory, basically the solution is following :

in your main loop, you have 2 variables to store :
- the current time (GetTickCount()) just before calling the update function : let''s call it current_timer
- the last time the update function was called (initialised at the same value as current_timer) : let''s call it previous_timer

init :
current_timer = GetTickCount()
previous_timer = current_timer

loop:
current_timer = GetTickCount()
call your update with (current_timer - previous_timer)
previous_timer = current_timer

You may consider having a maximum valid value for (current_timer - previous_timer), in case the user switch to another app.

----
David Sporn AKA Sporniket
this is a little off subject but how do u display the fps info or any text on the screen. is there a way to add a few lines of code to display the text on the HUD? every time i have tried i have ended up displaying text somewhere in the 3 dimensions of my scene.
Please have a look at lesson 17 : 2D Texture Font.

That should address what you are asking for.

----
David Sporn AKA Sporniket
I have a little problem with the soltution mentioned above (decreasing movement rate when fps is high). if you do so, when you have higher fps, the program will call the movement (or other transformation) routine more times, than it does at lower fps, and it takes more hw resources. i dont really know if im right, but i had to tell this, becouse im really curious about it. so? what do you think?
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
In my programs, the update of the program state and the rendering of the screen are separated.
My update() function returns true if rendering is necessary, I then call render() only in this case.

In your case, you have to choose an "ideal" best FPS (let's say 50 FPS), hence a maximal time period before rendering another frame (here 20 millisec). Thus you keep track of the time elapsed since last rendering, and the update function return true when (among other conditions) the elapsed time is longer than the time limit.

----
David Sporn AKA Sporniket

[edited by - davidsporn on January 24, 2003 2:05:00 AM]
the other way is to keep watching fps, and calling the update routine if needed (e.g. in every 3th frame. i think its better in action oriented games where high framerate is essential)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

This topic is closed to new replies.

Advertisement