I am trying to get the time stepping working consistently in an asteroids project. The application runs fairly smoothly but when really looked at there are some pretty slight variances in motion. I have looked at http://www.koonsolo.com/news/dewitters-gameloop/ and https://gafferongames.com/post/fix_your_timestep/. I tried to write something similar to the first link but did not really notice a difference.
Here is the first article:
const int FRAMES_PER_SECOND = 25;
const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
DWORD next_game_tick = GetTickCount();
// GetTickCount() returns the current number of milliseconds
// that have elapsed since the system was started
int sleep_time = 0;
bool game_is_running = true;
while( game_is_running ) {
update_game();
display_game();
next_game_tick += SKIP_TICKS;
sleep_time = next_game_tick - GetTickCount();
if( sleep_time >= 0 ) {
Sleep( sleep_time );
}
else {
// we are running behind!
}
}
This article said there would be no problems on computer's that are fast enough, and this does not seem to be true. Perhaps the slight variations are as good as it is going to get?
Here is the second article:
double t = 0.0;
double dt = 0.01;
double currentTime = hires_time_in_seconds();
double accumulator = 0.0;
State previous;
State current;
while ( !quit )
{
double newTime = time();
double frameTime = newTime - currentTime;
if ( frameTime > 0.25 )
frameTime = 0.25;
currentTime = newTime;
accumulator += frameTime;
while ( accumulator >= dt )
{
previousState = currentState;
integrate( currentState, t, dt );
t += dt;
accumulator -= dt;
}
const double alpha = accumulator / dt;
State state = currentState * alpha +
previousState * ( 1.0 - alpha );
render( state );
}
As I understand it this program takes whats left over in the accumulator and blends it with an interpolation. I have read that this is a pretty famous article. I am wondering about the variables previousState and currentState. I assume the passed in variable to integrate is a reference to hold the value for next time? And in the final line before the render, the 2 states that are being multiplied by the 2 alphas are what, maybe my x and y?
The variable Previous is previousState and current is currentState?
Will this be even any better though as my understanding is I would need a previous and current x and y for every object and there is a function call for each draw.
I would also have to run the while over and over again and and than the two lines below it over and over again. These two procedures in themselves would seem to slow the game down more than what it is now.
I could really use some help, please.
Thank you,
Josheir