Qbasicnews.com

Full Version: fps speed compensation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey everyone. i was wondering if you could share with me your different speed compensation systems for different fps?

the one i use is

each second update a float variable that is a ratio of 60 / fps

that way as fps changes the ratio changes, and when fps = 60 the ratio = 1

then this is multiplied by anything that operates at a specific speed. for instance player movement.

where playerx = playerx + playerspeed * compensationratio

so that at ideal frame rate the player will move at playerspeed speed. but if fps drops to like 30, the ratio becomes 2, so the player will walk at 2x playerspeed, so that during that timeframe the player moves at almost the same distance they normally would during ideal frame rate

the problem i'm having is, since it updates each second the player has short bursts of speed that last for a second everyonce in a while.

so, i'm wondering who else has had problems like this, and what they developed to fix it?

thanks
Your on the right way, I really don't know how to fix it in QB, I solved it in FB by using timed based movement, instead of frame based, but I calculate using ticks (milliseconds) and QB can't really do that.

Might be theres a lib somewhere...

The code i use in FB anyways:
Code:
TicksStart = SDL_GetTicks
do
    
    'Do all drawings here.
    
    TicksEnd = SDL_GetTicks
    Ticks = (TicksEnd - TicksStart) * 0.1
    TicksStart = SDL_GetTicks
Loop
Then to get movement, you do:
Player.X = Player.X + Player.Speed*Ticks

If player.speed is 2.5, then he will move 250 pixels in one second, no matter what FPS you get.
oh wow, that is incredibly smooth!!!!

heres my adaptation
Code:
endtim# = TIMER
IF starttim# = 0 THEN starttim# = endtim#

a! = (endtim# - starttim#) * 25

IF a! = 0 THEN a! = fpsr

fpsr = (fpsr + a!) / 2
starttim# = endtim#

this is basically just like yours, only it averages in the new speed with the old one because the player was appearing slightly shakey

thank you so much! it is very hard to see the slowdowns or speed ups now because it alters the speed per frame instead of per second


EDIT::
heh, now i'm getting really bad slow downs after i added background animations, i'll have to tinker with this more
ok, i think i got it

Code:
do

  fp2 = fp2 + 1
  IF fp2 = 4 THEN
    fp2 = 0

    endtim# = TIMER

    IF starttim# = 0 THEN starttim# = endtim#
    a! = (endtim# - starttim#) * 10
    IF a! = 0 THEN a! = fpsr
    setfpsr! = a!
  
    starttim# = TIMER
  END IF

  fpsr = (fpsr + setfpsr!) / 2

loop

i was having a problem with endtim#-starttim# returning a 0 quite frequently and it was throwing off the calculations, so now it takes the average of the difference over 4 frames.

before i was getting a slowdown from 30 to 12 pixels per second character speed. now at ~60fps and ~30fps i get ~24 pixels per second movement. it takes quite a hit within those 4 frames to drop the speed enough to see a noticable slow down
In DOS mode, I used a high resolution timer in QB for one build of my wtsc 3d engine... basically just like Z!re said.

Unfortunately the timer doesn't work under windows, and the default qb timer is too slow to be of any good use for time based movement.

I think the solution is .... move to fb. :wink:
What you could do is seperate logic from drawing. You update logic certain times per second, if there's time left before the next frame you can draw the frame too.