Timing

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Timing

Post by RustyNail »

For the Linux version of my engine, I use gettimeofday(), as it seems to be the only available way (correct me if i'm wrong), but I noticed something strange happening - while moving, every-so-often, the world would jump away and I'd be left floating in the middle of nowhere... A simple print-to-log cleared it up (in Seconds):

Code: Select all

0.018559
0.016289
0.016252
0.016225
4294
0.016227
0.016258
0.016161
0.028836
It seems that every hundred frames or so, my counter jumps a huge ammount...
Any idea on what could be causing it?
(My timing works through two unsigned int counters: totalElapsed, and elapsed, elapsed is calculated by subtracting totalElapsed from the value returned by gettimeofday(), and then totalElapsed gets set to that value...)
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

What does gettimeofday() return? Is it an unsigned int?

If it's not then you should use whatever variable type they're using...

I had a problem in the code for the PS3 demo disc i did where i was using an standard int instead of a 64-bit int that the timing function returns which meant that if the PS3 had been on for a while before running the demo disc it would get stuck showing the credits list instead of showing the menu :oops:
Image Image Image
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

gettimeofday() returns a struct timeval, which contains two long int variables:
seconds & microseconds. i multiply seconds by 1000*1000 and add it to the microseconds...
That might be the problem there... :roll:
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Yup, you're overflowing your u32. You'll always get these wrapping issues, regardless of the granularity of your counter. You should try to minimise them and deal with the wrap when it happens, which it will.

Note that Irrlicht isn't immune. Timer::getRealTime() returns a u32 ms value, and so will wrap every ~49.7 days. On Windows, it'll wrap after 49.7 days of uptime; on Linux, it'll wrap every 49.7 calendar days, regardless of uptime.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

Hmmm...
So, make it happen less than every 57 frames (on average), i do believe i'm going to have to ditch microseconds :-/ ... milliseconds, here I come...

Any suggestions on how to deal with overflows? :roll:
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Code: Select all

if (currentTime < lastTime) 
  cout << "overflow, panic!\n";
or you could deal with it in a more sensible way.. like take lastTime from u32(-1) and add it to currentTime to get your delta time
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

:) Going to have to think about it, but since I currently usually don't keep my computer on for 47calendar days, I don't think I will have a problem for now :)

Now the I've got timing working in Linux...
How would I do timing on Windows?

Code: Select all

timeGetTime();

doesn't give me enough resolution... and neither does

Code: Select all

timeBeginPeriod(1);
timeGetTime();
timeEndPeriod();
And I cannot get QueryPerformanceCounter(); to work... (at least under wine)... I have problems with stuffing a LARGE_INTEGER into an unsigned int...
Any Ideas?
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

RustyNail wrote::) Going to have to think about it, but since I currently usually don't keep my computer on for 47calendar days, I don't think I will have a problem for now :)
Sorry, I was unclear: using gettimeofday() (and milliseconds) will wrap every ~49.7 days, depending on the date, not on how long your machine has been on for. It could wrap the very first time you do it after booting.

Fortunately, dealing with the wrap is trivial:

Code: Select all

u32 diff = now - last; // All u32 values

// Very high positive values are actually negative values, i.e. it's wrapped.
if(diff > UINT_MAX / 2)
{
    // "Unwrap" the negative value back to a small positive value.
    diff = UINT_MAX - diff;
}
Now the I've got timing working in Linux...
How would I do timing on Windows?
Have you looked in Irrlicht's os.cpp? (WinCE code elided)

Code: Select all

	u32 Timer::getRealTime()
	{
		if (HighPerformanceTimerSupport)
		{
			// Avoid potential timing inaccuracies across multiple cores by 
			// temporarily setting the affinity of this process to one core.
			DWORD affinityMask;
			if(MultiCore)
				affinityMask = SetThreadAffinityMask(GetCurrentThread(), 1); 

			LARGE_INTEGER nTime;
			BOOL queriedOK = QueryPerformanceCounter(&nTime);

			// Restore the true affinity.
			if(MultiCore)
				(void)SetThreadAffinityMask(GetCurrentThread(), affinityMask);
			if(queriedOK)
				return u32((nTime.QuadPart) * 1000 / HighPerformanceFreq.QuadPart);

		}

		return GetTickCount();
	}
I'm very surprised that you need more than millisecond accuracy. What's that for?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

I really don't need more than millisecond accuracy, it's just that when I'm running under windows, my frame gets drawn in less than a millisecond (or less than the minimum difference that timeGetTime(); provides) in some cases, which works mayhem with my movement code (it's exetremely jumpy) & my displayed FPS becomes something like: "0.23542319"...
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Try putting vsync on in the device creation ;)
Image Image Image
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

also ur movement shud be based on delta, not frame rate,

if u are using the timer its changing? then its frame based
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

It's in my own OpenGL engine, and i'm not that pro :P
(of course I could look at NeHe, but i'm lazy...)
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

The movement is based on the delta, but when the time provided by timeGetTime() is equal to the previous time, the delta is equal to zero, and so no movement occurs, hence the sucky quality of the movement...

EDIT: Irrlicht's method with QPC() doesn't work either... :(
or maybe it's just WINE...
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Rogerborg wrote:I'm a bit confused as to how you're seeing the result of those 0 delta frames. Can you elaborate?
Last edited by rogerborg on Thu Mar 06, 2008 8:47 am, edited 1 time in total.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
RustyNail
Posts: 168
Joined: Fri Jun 02, 2006 1:49 pm
Contact:

Post by RustyNail »

My WIN32 program, when it has no messages waiting for it, calls a function which updates the timer, gets the difference between the old value of timeGetTime(), and the current value & uses it to move the camera...
So I think that the scene could be rendered in less than a millisecond...
I have recently discovered that both the Flu and my Algebra teacher have exact the same effect on my health: it quickly degrades.
Post Reply