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 / [DBC] Timer Based Movement

Author
Message
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 15th Nov 2009 14:11 Edited at: 2nd Apr 2010 23:06
Well, if there is a DBP version there is also a DBC version!

This is very weird... A ton of people suddenly wanted a function for timer based movement...

There are two types of timer-based movement. One is, like the DBP version, when you multiply the variables by a factor calculated by the system time and the DBP loop using vectors. The other one, like this one, directly affects the loop speed.

cometSYNC()

This function will affect the synchronization so your main loop will run at a constant speed of a given value. You can use this function instead of using "sync", but never use both! The sync rate must be specified using an integer value, the update value must be specified using an integer value, and the return value is specified using an integer value.

cometSYNC() will count the loops actually performed, and subtract the result from the loops it should have done. From that it calculates which synchronizations should be evenly skipped the next loop.

Pros

-Very easy to implement
-variable function

Cons

-Too unstable for Net Games

SYNTAX

return value=cometSYNC(sync rate,update)

how to use

Replace every "sync" command with "cometSYNC()". The return value returns the FPS rate. The sync rate is the rate you want your loop to run at. The update value specifies after how long the function should update the counter in milliseconds. The more objects you have in your game, the higher this value should be. I found a value of 500-1000 runs well. If you have no idea what to set this to, then just leave it at 500

Function Code

Compressed (I could even post this in the 20-liner!):



Uncompressed:



Example




Enjoy!

TheComet

Profit
18
Years of Service
User Offline
Joined: 19th Feb 2006
Location: United States
Posted: 18th Nov 2009 22:31
you are crazy for giving this away for free.


HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 19th Nov 2009 12:54
Well I think its great that someone is kind enough to release what they have done.

Plus it helps everyone, make for better coding and someone else may look at this and think I can do it better now I know how its done.

so to TheComet well done and thank you for sharing it.

Dark Physics makes any hot drink go cold.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Nov 2009 00:10
This is exactly what I was looking for in my game. Thanks!

Login to post a reply

Server time is: 2024-04-19 18:30:15
Your offset time is: 2024-04-19 18:30:15