3

I am trying to implement a fixed timestep loop so that the game refreshes at a constant rate. I found a great article at http://gafferongames.com/game-physics/fix-your-timestep/ but am having trouble translating that into my own 2d engine.

The specific place I am referring to is the function in the last part "The Final Touch" which is what most people recommend. This is his function:

double t = 0.0;
    const 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;   // note: max frame time to avoid spiral of death
         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 );
    }

For myself, I am just moving a player across the screen keeping track of an x and y location as well as velocity rather than doing calculus integration. **I am confused as to what I would apply to the updating of the player's location (dt or t?). Can someone break this down and explain it further?

The second part is the interpolation which I understand as the formula provided makes sense and I could simply interpolate between the current and previous x, y player positions.

Also, I realize I need to get a more accurate timer.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Pladnius Brooks
  • 1,248
  • 3
  • 19
  • 36

3 Answers3

1

If you can get at least microsecond accuracy, try this:

long int start = 0, end = 0;
double delta = 0;
double ns = 1000000.0 / 60.0; // Syncs updates at 60 per second (59 - 61)
while (!quit) {
    start = timeAsMicro();
    delta+=(double)(start - end) / ns;
    end = start;

    while (delta >= 1.0) {
       doUpdates();
       delta-=1.0;
    }
}
Susan Yanders
  • 854
  • 1
  • 9
  • 18
0

See: http://en.wikipedia.org/wiki/Integral & http://en.wikipedia.org/wiki/Numerical_integration

The function is a numerical technique (2nd link) for approximating the integral function (1st link).

  • Sorry, I updated my question. I worded it badly. I was wondering how the main function was using time and delta time and how I would apply it to the moving of a 2d player with an x and y position. – Pladnius Brooks May 07 '12 at 04:12
  • @chrisaycock I disagree. It answered the question as originally stated. Also, SO will not let me post comments--only answers. – Jonathan Leonard May 07 '12 at 04:48
  • 1
    @JonathanLeonard: answers that only contain links are frown upon. You need to provide a real answer and use the links as *references* to support your claims or provide further explanations. – Matthieu M. May 07 '12 at 08:58
  • @MatthieuM. Did you see the second sentence which explains what the links are? – Jonathan Leonard May 07 '12 at 17:45
  • @JonathanLeonard: I did. I find it a bit short. What is obvious for you may not be for the OP, else there would not have been a question in the first place. – Matthieu M. May 07 '12 at 17:48
-3

You should review your high school physics. Simply put velocity is change in distance over change in time(dxdt), acceleration is change in velocity over change in time(dvdt) If you know dxdt you can get the distance by integrating it w/ respect to t, if you know dvdt you can get the velocity by integrating it with respect to t. Obviously this is a very simple explanation, but there are tons of references out there with more details if you so desire.

user439407
  • 1,666
  • 2
  • 19
  • 40
  • Sorry, I updated my question. I worded it badly. I was wondering how the main function was using time and delta time and how I would apply it to the moving of a 2d player with an x and y position. – Pladnius Brooks May 07 '12 at 04:12
  • I still recommend you revisit high school physics, an hour should be enough provided you have the proper mathematical background. I dont have the code so I cannot tell you what "integrate" does exactly, but my guess is that updates currentState, recalculating a new position based on the acceleration and velocity of the object for the chunk of time dt.(IE you calculate distance by taking the integral of your velocity with respect to time, you calculate your velocity by taking the integral of your acceleration with respect to time. That is probably what he is doing in this snippet) – user439407 May 07 '12 at 07:44