1
\$\begingroup\$

Below is an image of my gameworld. As we can see, the player starts at position 0. each cell represents 1 game world unit. My goal is to get the player to move 1 unit (the red cell) in 1 second in 'real world' time using the LIBGDX framework.

enter image description here

LibGDX uses something called 'continuous rendering' by default, which means its 'main loop' runs as fast as the underlying OS is able to. The range could be anywhere from 0-80 frames per second. I am capping the FPS to 60 with the below 'main' loop (In LIbGDX the main loop is called render)

@Override
public void render(float deltaTime) {

    /*
     * Get the time it took to render the last frame and make sure our delta
     * time is never larger than 0.0166666666666667 (60 FPS). This will
     * ensure that on fast OS the game at only 60 FPS.
     */
    float dt = Math.min(Gdx.graphics.getDeltaTime(), 1 / 60f);

    // **********************UPDATE*********************

    player.update(dt);


    // ************************RENDER********************

    // clear the screen and set it to cornflower blue
    Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f,
            0xff / 255.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    worldRenderer.render();


}

Below is the code for my player object's update method. As we can see it is operating solely on the delta time for now. In a perfect world of 60 FPS, this would work perfectly but unfortunatly FPS dips will always happen (especially on mobile platforms). How can I add a calculation on top of the delta time to move the player 1 unit in real time regardless of framerate?

@Override
public void update(float deltaTime) {

    // handle user input **always should come first**
    if (Gdx.input.isKeyPressed(Keys.SPACE)) {
        position.x += deltaTime;
    }

}

Below shows what is happening with my current code, if we had a perfect 60 FPS the position should = 1, but as we can see it does not because the FPS varies slightly from frame to frame.

enter image description here

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

The deltaTime is the time it takes for the system to render one frame, thus if you are using deltaTime correctly, your character should move the same amount regardless of the frame rate. For example, if you are moving your character by the raw deltaTime value (+= delta):

If you have 1 update in 1 second, delta will be 1f, and you character will move by 1. If you have 60 updates in 1 second, delta will be 1/60f, and in 1 second your character will move 60 * (1/60f) which is still 1.

The error you show in the 2nd image is perfectly understandable, but I really think that it is so minimal you don't need to worry. You're talking about 1 100th of error.

Another thing to mention, Math.min code you have isn't helping you out very much. You are essentially restricting the whole point of the deltaTime value. If it takes libGDX less than 1/60 of a second to render a frame, that will be reflected in the delta value. For example: If you have two ticks that take 1/120 of a second (this is an extreme and will never happen), by setting your deltaTime to 1/60, you are making libGDX think that 2/60 seconds went by, when really 2/120 seconds went by. So you really shouldn't be restricting your deltaTime value.

\$\endgroup\$
1
  • \$\begingroup\$ Accepting your answer as its the most reasonable to my question. For other readers, its important to note that delta time will fluctuate per each render cycle, which is why we see the distance is not equal to 1 in the post above after 60 render cycles. \$\endgroup\$
    – Shijima
    Commented Dec 9, 2014 at 18:12
0
\$\begingroup\$

You'll have to measure elapsed time independently of the game loop.

long startTime, time;
startTime = System.currentTimeMillis();
while(true) // game loop
{
time = System.currentTimeMilles();
if((time - startTime) > 1000) doSomething();
}
\$\endgroup\$
0
\$\begingroup\$

this is physic. S=Vt , here if t =1 second and s =1unit so it will move by 1 unit/second speed , libGDX render method is called 60 times in 1 second so you can move your character by 1/60 unit each time when render is called and the player will continue to move by 1 unit/second speed

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .