Fixed rate logic with Box2d ?

BlitzMax Forums/BlitzMax Programming/Fixed rate logic with Box2d ?

Armitage 1982(Posted 2009) [#1]
Hi

I know this already been discussed a huge amount of time but none of the solutions proposed are appropriated in this case.

My game logic need to run at 60 FPS in order to have the same physics engine (Box2D) refresh rate.

By starting my project, it seemed more efficient to stick with the default flip method (flip -1).

This works well but as you can imagine lead to a few tearing at 60 Hz
(oddly enough: only in windows mode on my laptop and only in full-screen mode on my desktop machine).

Due to the external nature of Box2d, I cannot add delta-timing (whatsoever) even if I do have some tweening code in my game.

Do you know a Fixed Rate Logic code which proved to limit the game logic to 60 FPS but would allow me to set a screen refresh rate above 60hz (like 85hz were flip-1 doesn't create any tearing on both machine) ?

Box2D self-positioning objects at 60hz so we shouldn't notice any differences if we display 60 frames at 85 FPS.
I'm using OpenGL


Armitage 1982(Posted 2009) [#2]
I use the first code I found

Global newTime:Int = MilliSecs()
Global oldtime:Int = newTime
Global dt:Float = 1000.0 / 60.00	'60 FPS logic
Global deltaTime:Float, accumulator:Float

While Not KeyHit(KEY_ESCAPE)

   newTime = MilliSecs()
   deltaTime= newTime - oldtime
   oldtime = newTime
   accumulator:+deltaTime

   While accumulator >= dt
      update()
      accumulator:-dt
   wend

   draw()

   Flip -1

Wend


EDIT
Working, it is not necessary to use VSync with Box2d.
If I use VSync I need to Tween the scrolling position or then I get a few unwanted frame skip.

Now all I need maybe is a crossplatform code to detect the current refreshing rate ?


Armitage 1982(Posted 2009) [#3]
I added an option for VSync to switch between -1 and 1 mode.
Thus because I have no idea on how to correctly use and implement the delta-timing.

Of course I try

   While accumulator >= dt
      update()
      accumulator:-dt
   wend

   Local alpha:float=accumulator/dt

   draw(alpha)

   Flip 1


And multiply alpha with any X and Y values I got but this give me very ugly results especially with targeting camera.
At least I hope this VSync option help in worst cases to find acceptable rendering.

But nearly nobody is playing with VSync and different refreshing mode to obtain best result.

Is there crossplatform code to detect the current refreshing rate ?
What am I doing wrong with the alpha parameters ?

I also notice that I need to isolate my built-in Level Editor as well as my Control Manager from the Fixed Rate Logic or else movement code are sluggish.


Pete Rigz(Posted 2009) [#4]
There's a tweener.mod in my timelinefx module for implementing a fixed rate timing solution. Fixed rate timing does not use delta, it interpolates between an objects old and new positions. Take a look at a basic example here: http://www.rigzsoft.co.uk/index.php?option=com_content&view=article&id=25


Armitage 1982(Posted 2009) [#5]
Thanks a lot Pete
Very interesting module !

If only TimelineFx was here before I spawn my own like I already said before :)
But I'm nearly sure as soon as this game done I will switch for this one far better.

I talk about time delta because Box2d as the ability to set his own timestep.
But using this feature (a non fixed timestep) lead to unpredictable physics reaction (bridge could break at any time or object teleport them-self if the delta is too different from the last one).