There's tons of thread about it, and no real clear solution. I tried everything, and it's always jerky.
First to clarify some things:
-I use Win32/SDL on fast computers, one thread for both physics and rendering.
-I try to use fixed timestep to keep a coherent simulation. I render at what I can. With logs, I'm certain that my physics actually simulate 60 times per second, and rendering get about 180 frames per seconds.
-I use LARGE_INTEGER and QueryPerformanceCounter() for maximum precision.
-I don't think interpolating between rendering frames when physic didn't change do anything, the monitor don't refresh faster than the timestep anyway, so I guess always rendering at the last position is OK.
-Gaffer article is ok, but there's some things missing like events input and I get lost when he begin to talk about mathematical integration there.
Here is the code I have working the best until now, but sometime there's a strange big jerkiness happening. Seems to happens on certain framerate multiples. I actually used the same code for a DirectX10/PhysX engine and the same thing happened, I though it was something else but now that the exact same thing happens on my very different 2D SDL engine, I'm certain it's because of this timestepping problem.
What the hell is wrong with that!?
First to clarify some things:
-I use Win32/SDL on fast computers, one thread for both physics and rendering.
-I try to use fixed timestep to keep a coherent simulation. I render at what I can. With logs, I'm certain that my physics actually simulate 60 times per second, and rendering get about 180 frames per seconds.
-I use LARGE_INTEGER and QueryPerformanceCounter() for maximum precision.
-I don't think interpolating between rendering frames when physic didn't change do anything, the monitor don't refresh faster than the timestep anyway, so I guess always rendering at the last position is OK.
-Gaffer article is ok, but there's some things missing like events input and I get lost when he begin to talk about mathematical integration there.
Here is the code I have working the best until now, but sometime there's a strange big jerkiness happening. Seems to happens on certain framerate multiples. I actually used the same code for a DirectX10/PhysX engine and the same thing happened, I though it was something else but now that the exact same thing happens on my very different 2D SDL engine, I'm certain it's because of this timestepping problem.
Code:
bool CApplication::EventLoop(b2World* world)
{
LARGE_INTEGER frequency;
LARGE_INTEGER startTime;
LARGE_INTEGER lastFrame;
LARGE_INTEGER currentTime;
float deltaT=0;
float second=0;
UINT framePerSecond=0;
UINT lastFramePerSecond=0;
float accumulator = 0.f;
const float timeStep = 1.f/60.f;
const int velocityIterations = 10;
const int positionIterations = 8;
srand((unsigned)time(0));
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&startTime);
lastFrame = startTime;
SDL_Event event;
bool bRun = true;
while(bRun)
{
QueryPerformanceCounter(¤tTime);
deltaT = (currentTime.QuadPart - lastFrame.QuadPart) / (float)frequency.QuadPart;
second += deltaT ;
if(second>=1.f)
{
second-=1.f;
lastFramePerSecond=framePerSecond;
framePerSecond=0;
}
framePerSecond++;
if (deltaT>0.25f)
deltaT = 0.25f;
accumulator += deltaT;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
bRun = false;
else if(event.type == SDL_USEREVENT)
((UserEventFnct)event.user.data1)(event.user.data2);
else
CheckEventLoop((CVMWidget*)m_pRootObject, event); // Handle KeyPress/MouseMove, don't apply force there, simply change variables
}
while (accumulator>=timeStep)
{
accumulator -= timeStep;
((CVMWidget*)m_pRootObject)->Update(); // Call to ApplyForce() and such on bodies depending on previous events variables
world->Step(timeStep, velocityIterations, positionIterations);
world->ClearForces();
}
((CVMWidget*)m_pRootObject)->UpdatePhysPos(); // Use GetTransform() on bodies for the renderer to know the new position
((CVMWidget*)m_pRootObject)->Paint();
((CVMWidget*)m_pRootObject)->Flip();
printf("%d\n", lastFramePerSecond);
lastFrame = currentTime;
}
return false;
}
{
LARGE_INTEGER frequency;
LARGE_INTEGER startTime;
LARGE_INTEGER lastFrame;
LARGE_INTEGER currentTime;
float deltaT=0;
float second=0;
UINT framePerSecond=0;
UINT lastFramePerSecond=0;
float accumulator = 0.f;
const float timeStep = 1.f/60.f;
const int velocityIterations = 10;
const int positionIterations = 8;
srand((unsigned)time(0));
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&startTime);
lastFrame = startTime;
SDL_Event event;
bool bRun = true;
while(bRun)
{
QueryPerformanceCounter(¤tTime);
deltaT = (currentTime.QuadPart - lastFrame.QuadPart) / (float)frequency.QuadPart;
second += deltaT ;
if(second>=1.f)
{
second-=1.f;
lastFramePerSecond=framePerSecond;
framePerSecond=0;
}
framePerSecond++;
if (deltaT>0.25f)
deltaT = 0.25f;
accumulator += deltaT;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
bRun = false;
else if(event.type == SDL_USEREVENT)
((UserEventFnct)event.user.data1)(event.user.data2);
else
CheckEventLoop((CVMWidget*)m_pRootObject, event); // Handle KeyPress/MouseMove, don't apply force there, simply change variables
}
while (accumulator>=timeStep)
{
accumulator -= timeStep;
((CVMWidget*)m_pRootObject)->Update(); // Call to ApplyForce() and such on bodies depending on previous events variables
world->Step(timeStep, velocityIterations, positionIterations);
world->ClearForces();
}
((CVMWidget*)m_pRootObject)->UpdatePhysPos(); // Use GetTransform() on bodies for the renderer to know the new position
((CVMWidget*)m_pRootObject)->Paint();
((CVMWidget*)m_pRootObject)->Flip();
printf("%d\n", lastFramePerSecond);
lastFrame = currentTime;
}
return false;
}
What the hell is wrong with that!?