I've searched around the Internet, and eventually read a suggested article, "Fix your Timestep!". In accordance with the article, I came up with an update method, but I still have problems. Whenever I instantiate more objects, in my scene, I get much slower physical interaction. I've fixed other sub-systems, that they were wasting cycles during the update process, and 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.
What are the mistakes I'm making in my update process? What is the correct way to update a physics frame rate? Here is my code:
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)(GetTickCount());
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;
}
I've read that you shouldn't tie your physics frame rate to the render frame rate, so I use this->cPhysics->Update();
. I've used my previous approach, but with a slight change; I'm not sure if what I'm doing is right, but it solved my performance issue. First, I changed currentTime
to get time in milliseconds, instead of seconds. At the end, I've moved all of my update code to a time step loop, except for the renderers update. I still do not know if this is the correct way to go.
Core::Update
is called by a higher level loop which is the game loop, anyway, I can't understand why you're sayingyou're doing your entire engine loop here
because I have no idea how else I can do this! And I've profiled most of other parts and I know my physics update rate is the problem. I'll check your article. \$\endgroup\$