Jump to content
  • Advertisement
Sign in to follow this  
shane1985

Decoupling Rendering and Physics

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a side scroller, and am running into problems with physics and rendering. If I update the physics 60 times per second, and render it after every update, it all runs fine, and works as it should. If I run one of my other programs in the background to slow the rendering down (while keeping Vsync on), the rendering looks horrible. Of course if I turn Vsync off, and it runs at ~130 FPS, I get bad visual artifacts. So basically, if it renders at a different rate than the physics are updated, the rendering doesn't look good at all. Anyone have a solution for something like this? Thanks -Shane

Share this post


Link to post
Share on other sites
Advertisement
Lock the physics update frequency, and then interpolate/extrapolate animation in the renderer. If you can put up with a 1 cycle physics lag, then in each physics update you'd have


currentState = nextState;
nextState = physicsStep(currentState);


Which lets you interpolate between currentState and nextState in the rendering. This is how Shellshock did it, IIRC (look at their article on gamasutra). The alternative is to just extrapolate based on previousState and currentState.

Share this post


Link to post
Share on other sites
Actually I read something like that before on a different article, but I don't completely understand it.

What are currentstate and nextstate?

-Shane

Share this post


Link to post
Share on other sites
The state of your physics simulation. That's just pseudocode; what it might look like is if you have


class PhysicsObject
{
float3 currentPos, nextPos;
timestamp currentTime,nextTime;
//velocity, force accumulation buffers, etc.
void update(); //uses velocity/forces/currentPos to compute the nextPos, sets currentPos to the old value of nextPos
float3 getInterpolatedPosition(timestamp now)
{
float t = (float)(now-currentTime)/(float)(nextTime-currentTime);
return currentPos * (1.0f-t) + nextPos * t;
}
};


For update, you might have something like time-corrected verlet or whatever you are using.

Share this post


Link to post
Share on other sites
Okay, in my game I have an update function, that is inside the while loop with the accumulator like this.

while (accumulator > TIMESTEP)
{
accumulator -= TIMESTEP;
GS->Update(TIMESTEP);
}

// Render code

How would I implement interpolation with this type of system?

Share this post


Link to post
Share on other sites
For positions, you do a lerp (like I showed in that code snippet, for getposition), and for rotations you do a slerp or something (depends on how you represent rotations).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!