Clock() for Frame Independent Animation

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Clock() for Frame Independent Animation

Post 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?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Irrlicht has a virtual timer interface which will give you all necessary information.
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post 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)
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

Post by ehenkes »

but be careful with setTime, because animators use it, too.
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

If you update to the SVN trunk, you'll get a fixed version that uses the HPC on Windows.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply