I've searched around here and Google and at last I looked at Fix your Timestep! article on the web which most of the people suggest. In according to that article I came up with an update method like this, but I still have problems. Whenever I instantiate more and more objects in my scene, I'll get much slower physical interactions. I've fixed other sub-systems that they were wasting cycles during update process and now I've tracked my physics performance drop to this point. I think the way I'm updating my physics is somehow wrong because I'm using a fixed timing of `1.0 / 60.0` for my Box2D engine. Can someone help and point out my mistakes in my update process?
void Core::Update()
{
// Input
this->cInput->Update();
// Physics
this->cPhysics->Update();
// Keep frame rate
static const double desiredTime = 1.0 / 60.0;
static double currentTime = (double)time(NULL);
static double accumulator = 0.0;
double newTime = (double)time(NULL);
double frameTime = newTime - currentTime;
if(frameTime > 0.25){ frameTime = 0.25; }
currentTime = newTime;
accumulator += frameTime;
while(accumulator >= desiredTime)
{
accumulator -= desiredTime;
}
// Render
this->cRender->Update();
}
I've read that you shouldn't tie your physics frame rate with render frame rate and that's why I've put `this->cPhysics->Update();` above.
**Edit**
My new update method is something like this:
// Keep frame rate
static const double desiredTime = 1.0 / 60.0;
static double currentTime = this->frameStart;
static double accumulator = 0.0;
double newTime = (double)(GetTickCount());
double frameTime = newTime - currentTime;
if(frameTime > 0.25){ frameTime = 0.25; }
currentTime = newTime;
accumulator += frameTime;
while(accumulator >= desiredTime)
{
// Input
this->cInput->Update();
// Physics
this->cPhysics->Update(desiredTime);
// Scripts
this->cScripts->Update();
accumulator -= desiredTime;
}
Actually I've used my previous approach, but with a slight change. I'm not sure what I'm doing is right but it solved my performance issue. First I changed `currentTime` to get a `frameStart` variable which is fetched before game loop with `GetTickCount() / 1000`. It means I'm getting this in seconds and while I'm setting a `GetTickCount()` value for my `newTime` variable in milliseconds. At the end, I've moved all of my update codes except render's update to time step loop. Would someone tell me if this is correct?