Search code examples
javalwjglframe-ratetimedelta

LWJGL Runs slower at higher FPS? (Delta)


I have recently started on developing my own game engines for fun. I implemented a method to load Blender .OBJ models from files, and I am successfully rendering them. However, when doing a stress-test I ran into an unusual predicament with my Delta-time based fps system. While running at 1500FPS, it takes me 4 seconds to look from one end of a wall of models to another. If I cap the FPS at 120FPS, however, it only takes my 0.84 seconds to look from one end of the wall to another. As I explored further it would seem that, in fact, the game-movement speed decreases as FPS increases.

Here is my Timer class:

class Timer
{

    long lastFrame;

    int fps;
    long lastFPS;

    int delta;

    int fpsToReturn;



    public Timer()
    {
        lastFPS = getTime();
    }

    public long getTime()
    {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }

    public int getDelta()
    {
        long time = getTime();
        delta = (int) (time - lastFrame);
        lastFrame = time;

        return delta;
    }

    public void updateFPS()
    {
        if (getTime() - lastFPS > 1000)
        {           
            fpsToReturn = fps;

            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }

    public int getFPS()
    {
        return fpsToReturn;
    }
}

And of course movement is just something along the lines of:

camera.rotX += (0.5 * timer.getDelta());

Does anyone have any ideas? Is this how delta-time is supposed to work? When running at 16FPS it returns around 65, at 120FPS Delta is returning around 8-9, while uncapped FPS it always returns 0 or 1 if that makes a difference or helps you spot if something is wrong with the uncapped FPS. I really appreciate any help, thank you guys.


Solution

  • Solved my own question, and glad I learned in the process.

    My issue, which I later discovered when I was implementing angular movement, was that I was using the method getDelta() and getFPS() every time i needed it more than once-per-frame, which was throwing off the delta variable. I solved the issue by using a static variable, one for FPS and one for Delta, and updating each variable at the end of each frame.

    Pseudo Code:

    public static double globalDelta;
    
    while(nextFrame) //Loops once per frame
    {
        updateGame();
    
        globalDelta = calculateDelta();
    }