Hey everyone,
this is probably something quite simple but I can’t seem to get it right.
I need to get my physics simulation properly frame-rate independent.
On some systems it will run with nearly 60 frames per second while on others it will run on quite a bit below 30.
I need the simulation speed to be persistent however.
Here is what I am doing at the moment:
private var _nFixedTimeStep : Number = 1.0 / 60;
private var _iSubTimeSteps : int = 1;
private var _last_step : Number = 0;
public function update(dtMilliseconds:Number):void
{
if ((getTimer() - _last_step) > _nFixedTimeStep)
{
// calculate _iSubTimeSteps for framerates below _nFixedTimeStep
_iSubTimeSteps = (dtMilliseconds / 1000) / _nFixedTimeStep;
if (_iSubTimeSteps < 1)
_iSubTimeSteps = 1;
// step and remember last step
awpDynamicsWorld.step(dtMilliseconds/1000.0, _iSubTimeSteps, _nFixedTimeStep);
_last_step = getTimer();
}
}
As you can see I calculate the number of subtimesteps for lower frame-rates. Originally I thought just setting this to a higher number will take care of it automatically but then, at high frame-rates, it will run way too fast.
Therefore I calculate them for lower frame-rates. This seems to work fine but I do get jerky behaviour which comes from when my frame-rate is just below the 60 I presume? Not pretty. Am I over-engineering this?
edit: I should add that the frame-rate is not stable, it varies quite a bit during gameplay. Unfortunately I have no influence on that. Is there a way to compensate that? Having a stable simulation speed at various frame-rates even when it changes mid game?