The firt thing that comes in mind is:
Code: Select all
float fixedStep = 1.0/60.0;
while(irrdev->run())
{
physicsWorld->advance(fixedStep);
scnMgr->drawAll();
guiEnv->drawAll();
}But if game is all about physics the gamplay is much different at 120fps and 40fps because of the fixed physics step and abviously not fixed rendering rate ( fps ).
If rendering takes more time, physics should be advanced with bigger step to avoid physics slowdowns?
Lets do that:
Code: Select all
float TimeStamp = timer->getTime();
float variableStep = timer->getTime() - TimeStamp;
while(irrdev->run())
{
variableStep = timer->getTime() - TimeStamp;
float32 TimeStamp = timer->getTime();
physicsWorld->advance(variableStep);
scnMgr->drawAll();
guiEnv->drawAll();
}But ie. Box2d manual says that physics step should be 1/60 of a second, and variable timestep leads to variable, unpredictible results and debugging problems. Its abvious that in discrete calculations with big time step objects may tunnel thru walls or miss other objects critical to gameplay.
Wait, there is a solution... Just advance physics few times with a smaller step instead of one large step right?
Ok, here it is:
Code: Select all
float fixedStep = 1.0/60.0;
float TimeStamp = timer->getTime();
float variableStep = timer->getTime() - TimeStamp;
while(irrdev->run())
{
variableStep = timer->getTime() - TimeStamp;
float32 TimeStamp = timer->getTime();
float step = 0;
while(step<variableStep) {
physicsWorld->advance(fixedStep);
step += fixedStep;
}
scnMgr->drawAll();
guiEnv->drawAll();
}But wait.. something is wrong here. If physics will be calculated twice, it will take twice as much time as in previous frame which increases this iterations time and in next frame physics will have to be calculate 3 times or more. Eventualy with each and every frame application will slow down till it will become unresponsive. (I've been there it happens)
The question (finaly) is: