Skip to content

Instantly share code, notes, and snippets.

@Happy-Ferret
Forked from anpage/gist:8466445
Created October 8, 2017 10:25
Show Gist options
  • Save Happy-Ferret/3a850df37d772a18c71931e778a55295 to your computer and use it in GitHub Desktop.
Save Happy-Ferret/3a850df37d772a18c71931e778a55295 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