# Fixed-Timestep still updating too fast

I'm trying to figure out if I'm approaching my simple fixed-timestep implementation correctly, following examples such as: Gaffer (without RK4), and DeWitters

Currently, I have a game object which contains position and velocity data members, with position calculated by

 position = position + (velocity * dt)


However, unless the velocity is set to a value of two decimal places, the update makes the position change in an instant (e.g. Velocity = 0.05 works, but this does not seem logical; to me Velocity should be >0?; even a value of 0.5 makes the update appear instant). Therefore, I have come to the conclusion that I have implemented by game loop incorrectly.

Note, I'm using SDL and C++, however the following is all Psuedocode:

bool quit = false;
World world;

Uint32 frameTime = SDL_GetTicks();
double accumulator = 0.0;
const double dt = 1.0 / 60.0;
int fCount = 0;
while(!quit){
fCount = 0;
double newTime = SDL_GetTicks();
double nextFrameTime = newTime - frameTime;

if(nextFrameTime > 0.25)
nextFrameTime = 0.25;

frameTime = nextFrameTime;  //Line of error, should be frameTime = newTime
accumulator += nextFrameTime;

while(accumulator >= dt){
world.Update(dt);  //A simple counter revealed Update being called 15-16 times during this loop
accumulator -= dt;
fCount++;
}
//fCount is equal to 15 when dt = 1.0/ 60.0, fCount = 8/9 when dt = 1.0/30.0
double interpol = accumulator / dt;
world.Render(interpol);
}


If there isn't any apparent issue with this code then perhaps I have messed up my position/physics calculations based on Update(dt).

Any help is appreciated

I assume GetTicks() is standard SDL. In which case when I look at the documentation is says "Get the number of milliseconds since the SDL library initialization." However all your calculations seems to be in seconds. So if you treat the output from GetTicks() as being seconds you are gonna effectively be running 1000 times faster. Or you would if it weren't for this line if(nextFrameTime > 0.25).
So you either have to change all your other calculations to be in milliseconds, e.g. dt = 1000.0 / 60.0;, or convert the output from GetTicks() to seconds by dividing it by 1000. That way all numbers related to time will be in the same unit and can be directly compared, added and subtracted easily.
Also this line frameTime = nextFrameTime; should be changed to this frameTime = newTime;. frameTime should store the previous frame's start time so you can calculate the elapsed time for the frame from that.
• @sl133 Just remember that GetTicks() will return a number of type Uint32. This number divided by 1000, another int, will always produce an int. So you'll never get the fractional part. newTime will always be a whole number of seconds, something you don't want. So you want to promote the number to double before dividing. There is at least two ways to do that, either GetTicks() / 1000.0 or (double) GetTicks() / 1000. If either side is a double the entire operation is promoted to a double. – Christer Dec 20 '15 at 21:52