[Solved] Synchronizing Animate Time with FPS

Blitz3D Forums/Blitz3D Programming/[Solved] Synchronizing Animate Time with FPS

RustyKristi(Posted 2016) [#1]
How do I adjust the animate time depending on framerate (and if possible movement) so it plays or feels the same say.. 30fps. I see that animate takes float as the speed but kind of confused how I can calculate and adjust it according to current framerate.

Please correct me if I'm wrong here, just an example.

FPS: 60 = Animation Speed: 0.5?
FPS: 30 = Animation Speed: 1.0?
FPS: 10 = Animation Speed: 3.0?

I know there's already a movement adjustment example here depending on framerate and I'm curious how this could be done with animate time.


Bobysait(Posted 2016) [#2]
It would be a good usage, but it sets the animation speed for the whole animation length (a loop can be very different from an other, so once the animation is launched, it can get laggy or accelerated)

What you can do is set the animation speed to "1" (or any coef that fits the animation) and use a time coeficient on the UpdateWorld


; before the loop, get the current time, it could prevent from too high number or the looping to negative values ;
; also offset it by a value (here I put 10) to prevent from a "0" on the first loop)
    Local Start_Time = MilliSecs() - 10
    Local Current_Time% = 0
    Local Last_Time = 0
    Local Delta_Time
    Local Anim_Coef#
    Local BASE_FPS# = 60; if your animations are supposed to be played at 30 fps, then set it to 30
                        ; If your animation are not all at the same fps, then use the animation variable divided by this variable to set the @Animate speed factor
                        ; ex : your animation needs to be played at 25 fps -> Animate (Entity, 1, 25/BASE_FPS)

Repeat/While (xxx)/For ... (the start of your loop ^^ whatever it looks like)

    Last_Time = Current_Time
    Current_Time = MilliSecs() - Start_Time
    Delta_Time = Current_Time - Last_Time
    Anim_Coef = Float(Delta_Time * REQUIRED_FPS) / 1000
    
    
    ; Update animation according to the time spent between the two loops
    UpdateWorld (Anim_Coef)
    
    [...]
    Flip
    
Until (xxx)/Forever/Wend/Next


using this, your animations will be set correctly whatever the time of the loop.


RustyKristi(Posted 2016) [#3]
Thank you, very good information Bobysait!