I've been trying to implement the game loop found in Gafferon Games http://gafferongames.com/game-physics/fix-your-timestep/ but I'm running into an issue regarding the interpolation step.
The current algorithm looks like:
void draw(float interpolation)
{
//Set image coordinates to that of its collision box
image_->setX(box_.getRect().x);
image_->setY(box_.getRect().y);
if(player moving)
draw(image_->getX()*interpolation + last_x*(1-interpolation),image_->getY());
else
draw(image_->getX(),image_->getY());
}
This code is called outside of the physics loop (which runs at 30FPS), and therefore is called as frequently as possible.
The last_x
variable is set inside the physics loop to the x value of the image.
Everything seems to make sense, but this piece of code:
draw(image_->getX()*interpolation + last_x*(1-interpolation),image_->getY());
So essentially, I'm constantly adding distance based off the last x (which is going to be smaller than the current x), so this effect seems natural, as it takes an "average" of the current and last position, which is obviously going to be less than the current x-position, producing the effect seen below:
(The black box is the proper position, the red box is the Sprite location with interpolation)
So the question is, where am I going wrong with this?
EDIT: Well I found out that setting the coordinates every render call
image_->setX(box_.getRect().x);
image_->setY(box_.getRect().y);
Was the root of the error in the picture. However, while the image is in the correct place, I can't see a visible difference with interpolation, no matter how much I play around with it.
So I guess the final question is: is my interpolation algorithm correct and functioning? Am I expecting interpolation to do too much?