• Advertisement

Archived

This topic is now archived and is closed to further replies.

Problem with timers and FPS

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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%.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
quote:
Original post by orbano
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)

If nothing is updated, why rendering again the same image ?


----
David Sporn AKA Sporniket

Share this post


Link to post
Share on other sites
I suspect he means input, ai and physics updating, with object movement interpolated between rendered frames.

Share this post


Link to post
Share on other sites
What I do, is I add a possibility for each ''animated'' object or objects to lock its updates. For example, I have a particle system, and I have a UpdateSystem function for it, where I check whether or not to update the system based on the time alredy passed. So, I can say, ''lock number of updates per second to 30'' for this particle system ,and it just do so. You can do similar thing (if you don''t know how, reply again with request or mail me to acolyte@ozforces.com.au and I''ll send you the code)

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
OK, folks, the "correct" method is this:

Find the number of milliseconds elapsed between the last frame and this frame. This will be the delta time (delta means "change in").

Say you have something traveling at a velocty of 0.2 (in units per millisecond) and its position is 80.0 (in units). We are pretending we are only in one dimention at the moment. When you get back to your update loop, you take the velocity and multiply it by the delta time value and add that to the posion. Say it took 50 milliseconds between frames, you would find that your new position would be 180.0 units.


What you should not do:

- don''t base your movement values off Frames per Second (or per millisecond or any other frames/time). Frames per time is a bad unit. It is better to use time per frame. This is because it requires a division (which takes a long time to process, relitivly) to convert ms/frame to frames/sec (most timers give results in elappsed time and not FPS). Then when you move things, you are using a division again to convert framse/sec to ms/frame.

ThePretender is basing his movement values off FPS. He says move more with a lower FPS, and move less with a higher FPS. The way to achive that is to use a division. At best two unnessary divisions per frame, at worst, potentialy hundreds (objects updated + 1).

- Don''t do updates only on every n''th frame.

First of all, you are duplicating the functionality of vSync (verticle trace syncronisation). This is already included on the graphics card and can be turned on or off by either the user or the program. It is best to provide a default, but let the user decide.

Next, you will be waisting valuble cycles looping around waiting for the elapsed time to be big enough. Basicly vSync is tuned to give you the optimal frame rate without waisting peformance.

If you want to peform calculations that are not frame critical (eg: AI), then try multi-threading or do it every n''th frame if you must, just make sure your graphics and update (physics, etc) are opperating every frame and vSync will take care of the rest.

Also note that if you have a interpolation function, it can eaisly be slower than your normal physics functions, so it is worth while to do physics every frame and optomise the physics engine, rather than having two, half-optomised movement engines.


Hope you all got that.

Do not meddle in the affairs of moderators, for they are subtle and quick to anger.


ANDREW RUSSELL STUDIOS
Cool Links :: [ GD | TG | MS | NeHe | PA | SA | M&S | TA | LiT | H*R ]
Got Clue? :: [ Start Here! | Google | MSDN | GameDev.net Reference | OGL v D3D | File Formats | Asking Questions | Go FAQ yourself ]

Share this post


Link to post
Share on other sites

  • Advertisement