Skip to main content
GameDev.net gamedev.net
🔒 Locked

Physics interpolation problem

Started by Slaru May 30, 2005 at 8:29 PM 1 replies 1.9k views
Original Post
Slaru
Slaru
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
tolaris
tolaris
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."
Slaru
Slaru
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.