• Advertisement
Sign in to follow this  

Physics interpolation problem

This topic is 4714 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

Hello, I am writing some simple physics for my game. I have read this article about how/why to fix your timestep. But, I'm a little confused about the end, in the section "The Final Touch." The author is demonstrating how to interpolate between two physics states. Here's the source snippet from the article in question:
float t = 0.0f;
const float dt = 0.01f;

float currentTime = 0.0f;
float accumulator = 0.0f;

State previous;
State current;

while (!quit)
{
    float newTime = time();
    float deltaTime = newTime - currentTime;
    currentTime = newTime;
    
    accumulator += deltaTime;
    
    while (accumulator>=dt)
    {
        previousState = currentState;
        integrate(currentState, t, dt);
        t += dt;
        accumulator -= dt;
    }
    
    const float alpha = accumulator / dt;
    
    State state = currentState*alpha + previousState*(1.0f-alpha);
    
    render(state);
}

If I understand the code correctly, when the author interpolates between the two physics states, he is getting a state that actually isn't what the state should be. Shouldn't he be calculating the "current" physics state and the next physics state after it so that he can interpolate ahead of the "current" physics state to the actual physics state. Instead, it appears he is going back before the "current" physics state to the previous physics state. Please, shed some light on this. Slaru

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Slaru
If I understand the code correctly, when the author interpolates between the two physics states, he is getting a state that actually isn't what the state should be. Shouldn't he be calculating the "current" physics state and the next physics state after it so that he can interpolate ahead of the "current" physics state to the actual physics state. Instead, it appears he is going back before the "current" physics state to the previous physics state.

The code in question calculates physics state for "now" and then, over the course of time it takes to trigger another physics update, the renderer interpolates the state from "one step before now" to "now", which by the time the interpolation is fully done becomes "one step before now" itself. Essentially yes, there's length-of-single-physics-step delay in what the viewer gets to see and what "really" *is* ... like the article states, "this (approach) will add a latency of up to dt to your physics simulation, but the visual results are definitely worth it."

Share this post


Link to post
Share on other sites
Quote:
Original post by tolaris
like the article states, "this (approach) will add a latency of up to dt to your physics simulation, but the visual results are definitely worth it."

Thanks. I'm ready to shoot myself now for missing that line.

Slaru

Share this post


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

  • Advertisement