Created
January 17, 2014 00:48
-
-
Save anpage/8466445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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