This is the first code I posted in this thread, with some minor modifications.
` At the top of your program
global flast as integer
global factor# as float
` In your functions section
function tm_Update()
t = hitimer()
Diff# = t - flast
flast = t
factor# = Diff# / 1000.0
endfunction
` Then when moving anything
move object objID, speed# * factor#
To add smoothing by averaging the factors over a short amount of time. Change your function to this.
` At the top of your program add
global smooth as integer = 30
dim factors(smooth) as float
` In your functions section
function tm_Update()
t = hitimer()
Diff# = t - flast
flast = t
factor# = 0.0
for tx = 1 to smooth - 1
factors(tx) = factors(tx+1)
factor# = factor# + factors(tx)
next tx
factors(smooth) = Diff# / 1000.0
factor# = factor# + factors(smooth)
factor# = factor# / smooth
endfunction
You can change the value of smooth to whatever you like to add more or less smoothing. You'll have to test it to see what gives you the best visual effect for your particular application.
Q: Why do you divide the number of milliseconds per loop by 1000.0?
A: When setting up a game you need to know what scale you will be operating in fairly early in the design so that models can be made the appropriate size, and speeds and distances can be set.
For example, if your model is supposed to move 10 DBPro units per second, then "MOVE OBJECT Obj, Speed * factor#" should give you that speed multiplied by the proper decimal representation of the number of
seconds that have passed since the last move.
So if 14 milliseconds have passed since the last move, 14 / 1000.0 = 0.014
seconds and if Speed = 10 then in that time the object should move 10 * 0.014 =
0.14 DBPro units.
Multiplying that out we get,
1000 / 14 = 71.43 Frames per second
0.14 * 71.43 = 10.0002 DBPro units per second
Hopefully that clarifies things.
I don't think anyone has any dispute with the first code box above. It's standard among many other TBM examples I've found on the net for other game engines.
The smoothing was not my idea, but when I found it I tried it, and it smoothed out a problem I was having with jerky movement. Use it, play with it, change the smoothing value, and see if it helps. If not, pull the averaging code out and go with the original code.