Scaling a value based on FPS?

Hi

I was just wondering if there was a way to scale values based on FPS, if the entities move at fixed time intervals rather than based on frame count. For example, if player A gets 120 fps and player B gets 170 fps is there a way to scale the players speed so that both players would have an equal advantage?

CopyableCougar4

You would use delta time.
Basically the time that has passed since the last update.

Then instead of moving objects a set amount per frame you would move them based on milliseconds.

example:


public void update(int deltaTime)
{
    this.x += dx * deltaTime;
}

Edit:
It’s a very commonly used method for gameloops, so it should be easy to find examples on google :slight_smile:

Hi

That is probably going to be my best option :slight_smile: Thanks :slight_smile:

CopyableCougar4

A word of caution with this method though:
If you for some reason have a huge lag spike or if you minimize the game (LWJGL) then your game will try to catch up and update with a deltaTime of thousands of milliseconds!
Not a huge problem but remember it :slight_smile:

Hi

Thanks for the word of caution, but as of now the window is fullscreen and hitting escape will destroy the display :slight_smile: Also, as of now I took your idea and added it with only about 7-8 lines of code and it works beautifully :slight_smile: (It was noticeable before because two different sections of the world caused different FPS counts and the speed boost used to be noticeable).

CopyableCougar4

I have a nice little explanation on delta time here (not very technical but it’s only meant to help you understand what delta time is), if you’re interested check it out. :slight_smile: