I've seen a lot of posts asking about timer based movement recently, so I decided to post a little snippet that might be (or might not be) useful to a few people.
It allows you to move or rotate objects at a certain speed regardless of what the game FPS is at.
Type CustomTimer
next# as float
delta# as float
fps# as float `number of times per second you want this timer to update
EndType
Global objectTimer as CustomTimer
Global gameTime# as float `variable for holding the game's current timer
Global mc1 as boolean `toggle for left mouse button
Global mc2 as boolean `toggle for right mouse button
set display mode screen width(),screen height(),32
Sync On : Sync Rate 0 : backdrop on
Autocam off
Make Object Cube 1,10
Position Object 1,0,0,20
Point camera 0,0,0,20
`The the number of times per second you want to update the object Timer. Since the timer's fps
`is seperate from the game fps, this can allow for some interesting fast forward or slow motion effects when setting
`it higher or lower, without any sign of lag, jerkyness or jumping.
objectTimer.fps# = 60
`set the next update for this timer. You might be wondering why im not dividing 1000 by the fps, well, try it yourself and see :P.
`dividing the fps by a 1000 basically decreases the delay as the game fps drops so it can get more timer updates in to keep the object
`rotating at the same speed as it would if the game was running at 60 fps (or whatever fps you like your games to run at).
objectTimer.next# = Timer() + (objectTimer.fps#/1000)
Do
gameTime# = timer()
`if the game timer has past the time we set in the object timer's next time, then update the objects angle and
`set the next time it needs to update (objectTimer.next#).
if gameTime# > objectTimer.next#
x# = wrapvalue(Object angle X(1)+(1*objectTimer.delta#))
y# = wrapvalue(Object angle Y(1)+(1*objectTimer.delta#))
z# = wrapvalue(Object angle Z(1)-(1*objectTimer.delta#))
Rotate Object 1,x#,y#,z#
`calculate rotation speed of object according to fps. The delta is the most important calculation when working with timer based movement.
`so be sure you get this right.
objectTimer.delta# = (((gameTime#-objectTimer.next#)/1000)*objectTimer.fps#+objectTimer.delta#)/2
objectTimer.next# = gameTime# + (objectTimer.fps#/1000)
endif
`mouse button controls for changing the timer's fps
if mouseclick()=1
if mc1 = 0
dec objectTimer.fps#,1
mc1 = 1
endif
else
mc1 = 0
endif
if mouseclick()=2
if mc2 = 0
inc objectTImer.fps#,1
mc2 = 1
endif
else
mc2 = 0
endif
`or allow the user to change the timer fps with the scroll wheel
inc objectTimer.fps#,mousemovez()/25
`cap the timer fps
if objectTimer.fps# < 0 then objectTimer.fps# = 0
if objectTimer.fps# > 200 then objectTimer.fps# = 200
`Your usual on-screen help and debug info
text 0,0,"PLAY AROUND WITH THE SYNC RATE TO SEE THE DIFFERENCE"
text 0,20,"AT LOW SYNC RATES, THE OBJECT WILL STILL KEEP UP, BUT JUMP ALOT DUE TO ITS VERY HIGH MOVEMENT SPEED"
text 0,40,"AT HIGH SYNC RATES, THE OBJECT WILL MOVE SMOOTHLY, BUT STILL AT THE SAME SPEED AS LOW SYNC RATES"
text 0,60,"USE LEFT/RIGHT mouse buttons to increase/decrease the object timer FPS"
text 0,100,"GAME FPS: "+STR$(SCREEN FPS())
text 0,120,"OBJECT TIMER FPS: "+STR$(objectTimer.fps#)
text 0,140,"Object Timer Delta: "+STR$(objectTimer.delta#)
text 0,180,"Game Timer: "+STR$(gameTime#)
text 0,200,"Next Object Timer Update: "+STR$(objectTimer.next#)
text 0,220,"Object Movement Speed:" +STR$(1*objectTimer.delta#)
Sync
Loop
You'll notice that even at your maximum FPS (sync rate 0) the object is still rotated at a speed of 1 DBP unit per second).
While making it I realized theres potential in it for some nice time based effects too, like slow motion. I've made the snippet so you can change the FPS of the object timer seperate from the game timer so you can slow the cube time or speed it up. This is all done using timers.
No extra media is required to run this. Have fun and please let me know what you think.
EDIT:
This should work fine in DBC as well, without any modifications.