1
\$\begingroup\$

I have the following game loop:

while (!GameEnded)
  {
    CurrentTime = GetCurrentTime();
    uint64_t elapsedTime = CurrentTime - LastTime;
    if (elapsedTime > 25000000)
    {
        elapsedTime = 25000000;
    }

    Lag = elapsedTime;
    LastTime = GetCurrentTime();

    ReadInput();

    if (IsExitButtonPressed())
    {
      Game.State = END;
    }

    switch (Game.State)
    {
      case INTRO:
        break;

      case CUTSCENE:
        break;

      case RUNNING:
      {

        for ( ; Lag >= testPhysicStep; Lag -= testPhysicStep)
        {
            cpVect BallPosition = cpBodyGetPosition(Game.Ball.Body);
            Game.Ball.PreviousPosition.x = BallPosition.x - Game.Ball.Size.x / 2;
            cpSpaceStep(Game.Space, Game.PhysicsStep);
        }

        if (Lag >= 0.0f)
        {
            cpVect BallPosition = cpBodyGetPosition(Game.Ball.Body);
            Game.Ball.PreviousPosition.x = BallPosition.x - Game.Ball.Size.x / 2;
            cpSpaceStep(Game.Space, Game.PhysicsStep);
        }

        double Alpha = GetElapsedTimeMS(testLag) / Game.PhysicsStep;
        printf("Alpha: %f\n", Alpha);

        StartDrawing();
        {

            cpVect BallPosition = cpBodyGetPosition(Game.Ball.Body);
            Game.Ball.Position.x = BallPosition.x - Game.Ball.Size.x / 2;
            Game.Ball.Position.y = BallPosition.y - Game.Ball.Size.y / 2;


            cpVect DrawPosition = cpv(cpflerp(Game.Ball.PreviousPosition.x, Game.Ball.Position.x, Alpha), cpflerp(Game.Ball.PreviousPosition.y, Game.Ball.Position.y, Alpha));
            DrawTexture(Game.Ball.Texture, DrawPosition, Game.Ball.Size);

            Game.Ball.PreviousPosition = Game.Ball.Position;



        }
        EndDrawing();

        break;
      }
      case END:
      {
        DestroyGame();
        GameEnded = true;
        break;
      }
    }

I got inspired from this post link, to not save up the remainder for the next frame and just run a physic step ahead and interpolate between previous and current step.

But even with this approach I can see stutturing, see my output below, also the physic step is 1.0f / 180f:

Elapsed time: 16621740
Accumulator: 16621740
Previous position before update: 299.444458
Simulate step: 2
Lag remainder: 5510628
Lag remainder(ms): 5.510628
Alpha: 0.991913
Current physic position: 302.777771
Drawing position: 302.768786


Elapsed time: 16798049
Accumulator: 16798049
Previous position before update: 302.777771
Simulate step: 3
Lag remainder: 131381
Lag remainder(ms): 0.131381
Alpha: 0.023649
Current physic position: 307.222229
Drawing position: 306.137391


Elapsed time: 16593017
Accumulator: 16593017
Previous position before update: 307.222229
Simulate step: 2
Lag remainder: 5481905
Lag remainder(ms): 5.481905
Alpha: 0.986743
Current physic position: 310.555573
Drawing position: 310.540842
***Drawing difference > 3.5 STUTTERS: 4.403452***


Elapsed time: 16727820
Accumulator: 16727820
Previous position before update: 310.555573
Simulate step: 3
Lag remainder: 61152
Lag remainder(ms): 0.061152
Alpha: 0.011007
Current physic position: 315.000000
Drawing position: 313.901116
\$\endgroup\$
  • \$\begingroup\$ It looks like you have three different variables tracking the ball's position - can you break down for us what each is used for, so we don't have to guess? \$\endgroup\$ – DMGregory Dec 12 '19 at 3:04
  • \$\begingroup\$ It's not clear if you set CurrentTime as well as LastTime. \$\endgroup\$ – Jay Dec 12 '19 at 4:24
  • \$\begingroup\$ I updated now. I'm just using Game.Ball.PreviousPosition and Game.BallPosition to interpolate @DMGregory \$\endgroup\$ – Gavriil Dec 12 '19 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.