Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Timer based?

Author
Message
Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 1st May 2009 00:22
hey, i\'m thinking of starting a 3d project, i havent really used 3d yet, its been mostly text based. anyways, i want to make a sort of space sim, i want to know if this would be a good way to keep the program at a steady pace



Thanks, Nightmare

Omricon W.I.P
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 1st May 2009 02:39
The primary reason for timer based movement is so that whatever computer your game is running on, everything runs at the same speed.

Do what you would have in your main loop is,



and the function would be



Generally this code will suffice to keep your objects moving at a consistent rate no matter what computer it's running on.

Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 2nd May 2009 01:39
Ah right, i think i understand, thanks

Omricon W.I.P
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 2nd May 2009 10:34
KISTech, why ABS?

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 2nd May 2009 22:42
Quote: "KISTech, why ABS?"


The timer() function does eventually roll over and start at 0 again, and if you're running your game during that moment in time then the result of the comparison will be a negative number. ABS forces it positive.

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 2nd May 2009 23:57 Edited at: 3rd May 2009 00:14
ah, understood. but, how does ABS take care of the timer() rollover? and, when does timer() roll over? docs state timer() returns integer and max integer = 2,147,483,647. is this where it rolls or is it some other value (ie, 1,000,000,000)?. it seems that ABS and the method posted, while it will return a positive value, will still allow a massive value when timer() rolls over.

in your code example, if flast# = whatever the max value of timer() can be, give or take, and it rolls over, then:

if timer() = 1 and flast# = 2147483647 then diff# = 2147483646 = massive problem.

is it, then, best practice to check that timer() > flast# every loop?

edit: apparently my sig was 4 pixels too tall. replaced
and, what mod is running around with that precise of a ruler? heh.


KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd May 2009 19:41
I see your point. The main purpose of it I think, and I'm only guessing because I'm not the one that came up with it, is if (timer() - flast#) returns a negative number and you're doing a comparison on that result, then you'll have to wait until that negative number rolls back around to positive. If flast# is very close to the max value of timer() then it may never actually trigger the comparison. If you ABS the result, you at least get one loop where the comparison matches during the rollover period, and then things run along normally.

qwe
21
Years of Service
User Offline
Joined: 3rd Sep 2003
Location: place
Posted: 14th Jun 2009 20:54 Edited at: 14th Jun 2009 20:54
should we then replace

diff# = abs(timer() - flast#)

with

if timer() > 0 then diff# = timer() - flast#

? anyone know when it rolls over?
Image All
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 15th Jun 2009 00:25 Edited at: 15th Jun 2009 00:32
it should roll over at 86,399,999. that is the millisecond before midnight. this is assuming timer( ) returns the number of milliseconds that have passed in the day.

the real problem isn't when timer( ) rolls over, but what can we do that is better than timer( ). as timer( ) works on milliseconds, multiple CPU cycles could occur, as it goes as fast as multiple times per millisecond. there are only 1000 milliseconds in a second, and sometimes i get over 4000 frames per second. timer( ) does not have the ability to calculate the exact amount of time between frame 1 and frame 2 because it returns an integer, which can't help me with fractions-of-a-second frames. the result of this is that some times your timer-based movement could think no time has passed (because less than 1.0 milliseconds have passed) and render some redundant frames.


Remember those old guys? They made epic renders, I think one of them was called DaVinci, and all they used was MS Paint. Sometimes it's just skill....
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 15th Jun 2009 00:41
Does perftimer() or IanM's timer() roll over?

<-- Spell based team dueling game!
qwe
21
Years of Service
User Offline
Joined: 3rd Sep 2003
Location: place
Posted: 15th Jun 2009 01:51
the value returned is more accurate then PRINT can output. is it a double float?
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 15th Jun 2009 05:23 Edited at: 15th Jun 2009 05:24
Quote: "Does perftimer() or IanM's timer() roll over?"


Any timer function will eventually roll over because an integer (or long integer or whatever is being used) can only hold so large a number that eventually it will reach its max value and return to 0.

i like orange
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 15th Jun 2009 15:27
Store the 'last' value as an integer and do a purely integer operation for the comparison, this way the rollover is not an issue.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Jun 2009 21:09 Edited at: 15th Jun 2009 21:11
Quote: "Does perftimer() or IanM's timer() roll over?"

My hi-resolution timers do, but as the runtime to rollover for a 2GHz machine is approx 292471 YEARS, I didn't think it was worth worrying about

The ticker commands in the same plug-in also use the hi-res timers.

However, if you read them into a standard integer or dword value, instead of into a double integer, then they will appear to rollover at 47 days from boot-time. With a simple fix to my plug-in, I could make that 47 days after your game starts. In fact, I will do that.

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 15th Jun 2009 22:05
292,471 years... That's a LOT of milliseconds!!!

I never really paid attention to the details of when it rolled over, but suffice to say it's HIGHLY unlikely you'll ever actually see it happen, and even if it does, the original code would only be affected for one loop.

I have to add that I've gotten better results by taking the values of factor# over time and using the average. It smooths movement tremendously.



IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Jun 2009 23:37
Here's an even simpler method of avoiding rollover:


Call the InitialiseTimer() at the start of your program, and use GetTimer() instead of TIMER() and you won't roll over until your game has been running for 47 days non-stop.

(If you are familiar with 2's complement maths then you'll know what's going on in that code. If not, don't worry - it's magic )

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 16th Jun 2009 01:58
What exactly is the issue with the timer rolling over? You're not going to ever get a negative difference unless somehow your application freezes up for 23.5 days.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 16th Jun 2009 05:28
The only way I would see it being an issue is if you have a check like this "if timer()>tim+1000" and TIMER() rolls back to zero before a second is up it would take days for TIMER() to be more than tim+1000 again... but that would be so hugely monumentally never-happen-again kinda rare.

dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 16th Jun 2009 07:33
Quote: "The only way I would see it being an issue is if you have a check like this "if timer()>tim+1000" and TIMER() rolls back to zero before a second is up it would take days for TIMER() to be more than tim+1000 again... but that would be so hugely monumentally never-happen-again kinda rare"


Only if tim+1000 is < 2^31.

But as Benjamin and IanM say, it isn't an issue if you correctly handle the timer. In most cases you should only need to call Timer() once a frame to retrieve the elapsed time then add this to your counters or whatever. As long as the elapsed calculation uses an integer or DWord there won't be any issues with the rollover.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 16th Jun 2009 08:57
Yeah I'm not going to worry about it.

Login to post a reply

Server time is: 2024-11-25 04:52:27
Your offset time is: 2024-11-25 04:52:27