Skip to content

Instantly share code, notes, and snippets.

@anpage
Created January 17, 2014 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anpage/8466445 to your computer and use it in GitHub Desktop.
Save anpage/8466445 to your computer and use it in GitHub Desktop.
void MovableObject::update()
{
Time newTime = Clock::now();
double frameTime = toSeconds(newTime-currentTime).count();
currentTime = Clock::now();
accumulator += frameTime;
while (accumulator >= dt)
{
prevState = currState;
Vector2d d1 = currState.vel;
Vector2d d2 = currState.vel + currState.acc * dt * 0.5;
Vector2d d3 = currState.vel + currState.acc * dt * 0.5;
Vector2d d4 = currState.vel + currState.acc * dt;
Vector2d dpdt = (1.0 / 6.0) * (d1 + (d2 + d3) * 2.0 + d4);
currState.pos = currState.pos + dpdt * dt;
currState.vel = currState.vel + currState.acc * dt;
accumulator -= dt;
}
const double alpha = accumulator / dt;
rect.setPosition(sf::Vector2f(currState.pos * alpha + prevState.pos * (1.0 - alpha)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment