I realize this has been asked multiple times before, and I spent most of today reading the forums and the gaffers article trying to figure it out, so forgive me if this is redundant.
I'm attempting to just do a basic decoupling of the physics from the graphics/fps so that the physics engine runs at essentially the same speed no matter how fast the game loop. This is what I have:
Code:
const float dt = 0.015f;
time_t currentTime = time(NULL);
float accumulator = 0.0f;
while(device->run()) {
if (receiver.IsKeyDown(irr::KEY_ESCAPE))
break;
if (receiver.IsKeyDown(irr::KEY_SPACE) && totalboxes == 0) {
worldObj box("box.x", 0.0f, 12.0f, 100.0f, 1.0f, 0.2f, 0.7f);
boxes.push_back(box);
totalboxes++;
}
time_t newTime = time(NULL);
time_t deltaTime = difftime(newTime, currentTime);
currentTime = newTime;
accumulator += deltaTime;
while (accumulator>=dt) {
world->Step(dt, 8, 1);
accumulator -= dt;
}
}
It works, sort of. The simulation seems to run at the same speed independent of the frame rate, but it does so in a very "jumpy" way. In that, a falling box will fall in large steps and not a smooth falling animation.
If anyone is willing to further explain this process, and perhaps show me what is wrong, I would be most grateful.
Thanks.