Page 1 of 1

Clock() for Frame Independent Animation

Posted: Sat Sep 20, 2008 8:06 am
by torleif
Is it safe to use clock()?

A script kiddie tool I used a while ago it simply used ticks (ie, clock()) to do get sub second time. Some research seems to indicate it's bad, it might change if you swap to another computer that fails on a multiple of 66MHz

I want to have a character affected by many forces at once, IE: wind, gravity, weapons and jumping. Traditionally I get the players original position, and then add the time on this. But with these forces, I need to get a small amount of time since last update. Being dead accurate isn't important, but if your game runs twice as slow as it should there are going to be problems. Irrlichts getFPS() has problems, and I want to keep the code cross compatible.

How do you solve this problem?

Posted: Sat Sep 20, 2008 10:33 am
by hybrid
Irrlicht has a virtual timer interface which will give you all necessary information.

Posted: Sat Sep 20, 2008 10:46 am
by B@z
ISceneManager->getTimer()->getTime()
or get RealTime()

its better to use because you can slow it down (slow mtion) or stop it (pause)

Posted: Sat Sep 20, 2008 11:16 am
by ehenkes
but be careful with setTime, because animators use it, too.

Posted: Sun Sep 21, 2008 2:34 am
by torleif
Thanks for your replies, Irrlichts timer tends to jump, but I realized such a small jump in time isn't noticeable. It still can be used for animations as long as I don't divide by the difference (newtime-oldtime is often 0):

Code: Select all

endTime = timer->getTime()
main loop{
  // I use this variable to animate stuff 
  difference = timer->getTime() - endTime
  // do stuff here

  // end loop
  endTime = timer->getTime()
}
I was scared off by irrlichts getTimer function because:

Code: Select all

Itimer* timer = device->getTimer();
while(main loop) {
  std::cout << timer->getTime() <<"\n";
}
Would print out:

Code: Select all

120 
120 
120 
120 
120 
160 
160 
160 
160 
160 
160 
The time isn't updated every render loop.

Posted: Sun Sep 21, 2008 3:31 am
by vitek
torleif wrote: I was scared off by irrlichts getTimer function because:

Code: Select all

Itimer* timer = device->getTimer();
while(main loop) {
  std::cout << timer->getTime() <<"\n";
}
Would print out:

Code: Select all

120 
120 
120 
120 
120 
160 
160 
160 
160 
160 
160 
The time isn't updated every render loop.
The system timer is only so accurate. The real issue is that the loop executes more frequently than the system timer updates. In a real program your main loop will do something that takes up some time, then you won't see the problem.

Assuming the high performance counters are being used, you can check the timer update interval with a call to QueryPerformanceFrequency(). If the counters are not being used, then the GetTickCount() API is used (on windows) and I believe that function is subject to a 15ms update interval.

Travis

Posted: Sun Sep 21, 2008 9:07 am
by rogerborg
If you update to the SVN trunk, you'll get a fixed version that uses the HPC on Windows.