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.

Code Snippets / [DBP] - Timer Based Movement, once again

Author
Message
ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 27th Feb 2014 13:23
Hey guys,

just wanted to share another piece of code that provides a pretty stable method for timer based movement. It combines two different approaches: Scaling things using the time delta between two frames, and executing a fixed set of discrete steps for certain other operations.

The basic "library" (which mainly consists of an init and an update function) is very small and simple and doesn't really do much:




The usage is explained in the comments in the beginning. To summarize it very shortly:
-Call game_init() in the beginning of your program and game_update() in the main loop.
-When dealing with floats, you can just multiply any changes (e.g. movement speed of a character) with game.timeFactor
-When dealing with integers or more complex operations that need to be executed a fixed amount of times each frame, you can use game.fullUpdates, which is an integer and is usually either 0 or 1, but can also get higher in case the game's FPS rate falls below 50 (the exact configuration can be changed using the three constants in the code above).

Note that the GAME__MAX_TIME_DIF constant defines an upper boundary for the time delta between two frames. If the game gets slower than that, it will actually slow down instead of keeping everything timer based. This is meant to prevent "jumping" of values (and objects), which could for instance cause the player character to clip through walls.

I wrote a small example demonstrating how to use it exactly.





Feel free to use the code in any of your projects. In case you don't like the "namespace" (everything starting with 'game'), simply use search & replace on the word "game" in the code above and replace it by "timing" or whatever you want, that should do the job.

Le Verdier
12
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 27th Feb 2014 22:48 Edited at: 27th Feb 2014 22:52
Interesting, except one thing..
Timer() resolution is only about 16 milliseconds that is, imho, not suitable for a game
use PerfTimer() for hires timing. perfimer is double integer
also use preffreq() (in dbp7.7) or QueryPerformanceFrequency in Kernel32.dll to get the frequency..



ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 28th Feb 2014 00:57
Quote: "Timer() resolution is only about 16 milliseconds"


I don't think this is the case.



..although for some reason "wait x" seems to wait x+1 instead of x ms.
Anyway, the timer() command seems to do just what you'd expect from it - return the system time in ms - accurately, at least on (all of) my system(s) and with DBP 7.X.

However, it's certainly true that more accuracy is preferable. I wasn't aware you could get the frequency like that, so thanks for the input.

Le Verdier
12
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 28th Feb 2014 02:06 Edited at: 28th Feb 2014 02:08
I ran this on my (old) laptop and I have this result,


but maybe a faster machine has different result, if any one could confirm ?

Attachments

Login to view attachments
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 1st Mar 2014 13:22 Edited at: 1st Mar 2014 13:23


Here's mine.

Le Verdier, out of interest what result do you get using Hitimer()?

"Get in the Van!" - Van B

Attachments

Login to view attachments
Le Verdier
12
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 1st Mar 2014 17:39 Edited at: 1st Mar 2014 17:51
Yes it's far better. All hardwares/OS are not equals, even for a basic clock..It's this kind of little thing that became a hassle for coders like us..

I tried Hitimer() and I get
1
2
3
4
5
6
7
8
9
10

With an adjusted code:


But for me, 1ms resolution is still not enough accurate for a Main Loop

ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 29th Mar 2014 18:20 Edited at: 30th Mar 2014 00:10
What... suddenly I've got the same problem Le Verdier had with timer(). Its resolution went down to 16ms, without any changes to the system whatsoever. Worked without problems yesterday, and now it doesn't. Haven't changed a thing. Same DBP as before, same hardware as before, no Windows updates (that I know of)... how's that possible?

Guess I'm going to use hitimer as well then, but still... weird.


Edit: Now it's back to normal again. Without a restart or anything. Compiling the exact same code.

Edit²: Aaaand 16ms steps again. Ugh.

Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 7th Apr 2014 10:13
A side note to consider which you may be aware of is the that Timer() uses the system clock which commences once the computer has booted, unlike HiTimer() which counts time from when the application starts. Much safer on systems which do not get switched off which could lead to the return value rolling over, thus not exceeding the 32bit value range.

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Apr 2014 00:04
@Le Verdier,

You can use a parameter with HITIMER to get higher resolution if you need it. When I check code for performance, I tend to use a value of 1000000 instead of the default of 1000 and it works fine.

@ShellfishGames,

It can depend on what you are running on your system, as other programs can change the clock resolution.

You can also do it yourself too by using the SET TIMER RESOLUTION command from the same plug-in that HITIMER comes from.

Le Verdier
12
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 10th Apr 2014 02:32
Thank for these tips, this can be more handy than double variables..

!!!

Login to post a reply

Server time is: 2024-04-19 01:09:39
Your offset time is: 2024-04-19 01:09:39