Here's my Scene::poll function, which is called every frame.
Code: Select all
void Scene::poll()
{
float elapsedTime = g_clock.seconds() - m_lastTime;
float fCycles = elapsedTime / m_timeStep;
int cyclesToTry = std::round(fCycles);
for(int i = 0; i < cyclesToTry; ++i)
cpSpaceStep(m_space, m_timeStep);
m_lastTime += m_timeStep * cyclesToTry;
}
m_timeStep = 1./120;
Everytime poll is called, cpSpaceStep should be called 2 times.
elapsedTime would be (1/60), divided by m_timeStep (1/120) equals to 2.
Unfortunately, FPS is not always 60, it might be 58, 59, 61, 62, or anything close.
That way, elapsedTime will not always be 1/60 and then cyclesToTry will not be 2.
Using the round function:
int cyclesToTry = std::round(fCycles);
I've managed to keep 2 cycles most of the time, however sometimes it gets out of sync.
For every frame I get:
1, 3, 1, 3, 1, 3... cycles
And it should be:
2, 2, 2, 2, 2, 2... cycles
The amount of cpSpaceStep calls is the same for every 2 frames, however the movement completely loses its fluidity as there're a lot of moving objects on screen.
I've tried a few things to get rid of this 1,3,1,3.., but without success.
Does anyone have an idea to solve this?
Thanks