Framerate and step issue

Hey Guys,

I have been working on a flash game, and we are almost done, but I ran into an issue with stepping, that is why I am posting it here.

In the game I have made it so that the frame rate can be adjusted. To keep things running at the same speed, when I modify the frame rate, I have to change the time step, the issue I am having is that when the time step gets too large, accuracy seems to be lost. It's not a massive loss, but its enough to cause problems in a situation where something is just small enough to jump over. I figured out a solution but it feels hacky to me.

What I did to solve the issue, was I shrank the size of the time step, and then stepped more then once on each frame. For instance at 40 fps, my time step is 1.32/40, in my main loop I made it do this:
Code:
var m_timestep:Number = 1.32/40
world.Step(m_timestep/2, 1);
world.Step(m_timestep/2, 10);


However, the problems only begin at a frame rate of 25 or lower so:
Code:
var m_timestep:Number = 1.32/25
world.Step(m_timestep/2, 1);
world.Step(m_timestep/2, 10);


I might be missing a setting I can change to fix this issue, logic tells me that one step of 0.0538 should be the same as two steps of 0.0538/2, what am I missing?
Are you stepping with ENTER_FRAME or a timer?
Because using the enterframe is very unstable since flash's framerate fluctuates...alot
It's on ENTER_FRAME, but I know it has nothing to do with the fluctuating frame rate. The calls to step are sequential, and nothing moves again until step is called.

In essence the problem is this, calling step(value, iterations), is not the same as calling step(value/2, iterations) twice when the value gets large enough. I also tried increasing the iterations significantly to see if that was the cause of the loss, but it wasn't. In my case I start noticing the problem when value is >= 1.32/25.
Low framerates are going to cause inaccuracies, though frankly I'm surprised they are noticable at that sort of framerate. You're "hack" fix is actually quite a good solution, imho. Calling step twice at double the framerate and same iterations should always be more physically accurate than a single step (but NOT the same as you suppose).

There are two things you seem to have missed though. Firstly, why aren't you passing iterations=10 to both? Iterations = 1 is worthless, and so you'd at least have to raise iterations on the second to compensate, but I think even that would have some odd effects. Secondly, you don't seem to be calling anything between your two steps, but there should be lots of game specific code there, to deal with stuff that happend in the first game.

Finally, have you considered desynching your physics rate and graphics rate, e.g. as in this article. You are then free to play with what framerates you want in what circumstances to always get the best results.
The logic of having the steps right after each other, and this might not be the best way, was that by having a step with an iteration of 1, followed by 10, the first step roughed out where positions should be, then the 10 finalized the positions, and then I updated the graphics. This I believed would be the most efficient way performance wise, because calling render twice wouldn't be very useful in this case.

That article you sent me is really good, interestingly enough I actually experimented with some of the stuff he outlined, using a dynamic time step, however I didn't consider the idea of using an accumulator and have the physics on a timer. However, my actual assets in the game aren't controlled by the frame rate, they are controlled by a frame advancer, because flash itself will never skip frames (which is problematic).

I think having the physics separate might be better for performance, so I am going to try it.
I tried implementing the solution in the article, but I get lots of shuttering without the interpolation method he outlined at the end. Is there anyway to do something similar with box2D in regard to states?

Original thread: https://box2d.org/forum/viewtopic.php?t=1557