Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use integration for a fixed timestep game loop? (Gaffer on Games)

http://gafferongames.com/game-physics/fix-your-timestep/

http://www.koonsolo.com/news/dewitters-gameloop/

In the last game loop from Glenn Fiedler's Fix Your Timestep! article, he uses an update loop that advances the game logic by a fixed delta time. Since the delta time is fixed, why does he integrate based on delta time? In a fixed timestep based game, can't movement be as simple as:

if ( keyboard pressing "W" ) {
    velocity += acceleration
}
position += velocity

instead of passing in a delta time varaible:

if ( keyboard pressing "W" ) {
    velocity += integrated(acceleration, delta_time)
}
position += velocity * delta_time

Because delta time in a "fixed" timestep loop has no purpose, it would be like multiplying everything by 1. In a "variable" timestep based game you would have to use delta time and integrate for motion but in this case it would not matter. Notice how the variable for delta time is set to a constant and never changed, the game logic is already deterministic and it doesn't seem like you need to multiply velocity and acceleration by delta time anywhere in the code. What I took away from the article was "Fix Your Timestep!," as the name implies, so you have deterministic game logic instead of floating point inaccuracies and exploding physics that come with variable timesteps. I was just confused over why delta time was being passed into the update function since it seems to go against the purpose of the article. Both articles in fact make the same argument against delta time.

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 ); // integration
        t += dt;
        accumulator -= dt;
    }

    const double alpha = accumulator / dt;

    State state = currentState * alpha + 
    previousState * ( 1.0 - alpha );

    render( state );
}

deWiTTERS last game loop does the same thing: fixed timestep, interpolate rendering, render skipping. However it does not mention integration like the other one.

like image 216
YeOldeBitwise Avatar asked Apr 09 '17 02:04

YeOldeBitwise


People also ask

What is fixed Timestep?

That fixed timestep is known as fixedDeltaTime within Unity. By default it has a value of 0.02, meaning there are always 50 physics steps and FixedUpdate calls for every second of the game.

What is a time step in physics?

Timestep defines how small the time interval is inbetween physics simulations. In game engines this reflects how often functions() need be run. Physics(simulations) uses mathematical models to predict the future.


2 Answers

Assuming that acceleration increases or decreases linearly versus time, you could base velocity change on average acceleration

Δv = (a0 + a1)(Δt)/2

If the game includes factors like aerodynamic drag, then acceleration is affected by velocity^2, and usually something like Runge Kutta 4 is used to update velocity change and position change for each time step.


My impression is that most PC based games use an independent fixed frequency for the physics engine that is ideally independent of frame rate. For these games, even if the display is sluggish the actual game play is the same if the player can deal with the visuals. I've seen older racing games that get this right, where graphics performance doesn't affect the physics, and newer racing games where lowering graphics settings improves the in game car performance, which is a bug.

Link to example Windows code for running a thread at just about any reasonable fixed frequency, with no drift over time.

16.66ms frames times. How do you get a perfect 60 fps when sleep() only goes by whole milliseconds?

In the days of MSDOS, since a PC runs the timer at (105/88) Mhz = 1.19318 Mhz, games use it as a high precision timer counter.

like image 178
rcgldr Avatar answered Sep 21 '22 18:09

rcgldr


The reason he does it the more complicated way is to decouple the physics simulation frequency from the rendering frequency. This way, you can render at a variable frame rate (120 FPS, 60 FPS, etc) but still update the game logic and physics at a constant rate.

One of the benefits of this is that the game logic will behave the same no matter the FPS (many old games get buggy if you run them on modern computers because their physics depends on the FPS). Another benefit is that you can use the integration logic to interpolate some extra frames between the physics simulation steps, which results in a smoother animation.

like image 27
hugomg Avatar answered Sep 19 '22 18:09

hugomg