4
\$\begingroup\$
@Override
public void create() {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (x < 0 || x > 400) {
        speed = -speed;
    }
    x += speed * Gdx.graphics.getDeltaTime() * 60;
    Gdx.app.log("delta",Gdx.graphics.getDeltaTime()+"");

    batch.begin();
    batch.draw(img, x, 0);
    batch.end();

}

I had a problem and i created a new project. The problem libgdx tries to keep 60FPS and avarage deltatime is ~16ms.Some renders take +20ms then next render takes 12ms(render1 + render2 = 32ms) to achieves 60FPS. That makes game laggy. As you can see i don't have anything in the project and it is the same in desktoplaunch. How did you solve this ?

Note: I also tried reqeustRendering. But it is the same thing if the render takes more time than the avarage. I also tried to wait some times for the render if that is smaller than 16ms. It didn't help either. It doesn't have any GC problem. I use Libgdx 1.6.4.

\$\endgroup\$
  • \$\begingroup\$ The first function call in the render function in any libgdx project is Gdx.gl.glClearColor(0, 0, 0, 1);. Why are you missing that ? \$\endgroup\$ – dimitris93 Aug 5 '15 at 14:43
  • \$\begingroup\$ It was a copy/paste mistake. Thanks. Do you have any idea ? \$\endgroup\$ – Redugsix Aug 5 '15 at 14:49
  • 3
    \$\begingroup\$ Is the lag noticeable ? My project has similar deltaTime values with yours, averaging 16, but sometimes spike to 17-20 even 21 and I don't see any noticeable lag at all on the screen. If anything, it looks like it is running very smoothly. \$\endgroup\$ – dimitris93 Aug 5 '15 at 15:01
  • \$\begingroup\$ Yes it is noticable. For example avarage deltaTime is 16ms. When one deltaTime is 21ms and it is slow. Next deltaTime is 11ms and it is too fast. What is your deltaTime after +20 ms render ? \$\endgroup\$ – Redugsix Aug 5 '15 at 18:25
  • 1
    \$\begingroup\$ Obligatory go to gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$ – Gustavo Maciel Oct 15 '16 at 0:32
1
\$\begingroup\$

A guess here, but logging on the same thread as your rendering work, before you send gpu instructions, can add synchronous disk latency? Possibly in a sporadic fashion if it's buffered and dumping.

@Override
public void create() {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (x < 0 || x > 400) {
        speed = -speed;
    }
    x += speed * Gdx.graphics.getDeltaTime() * 60;

    float delta = Gdx.graphics.getDeltaTime ();

    batch.begin();
    batch.draw(img, x, 0);
    batch.end();

    Gdx.app.log("delta", delta+'"');
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for your advice but i didn't get this part "can add synchronous disk latency? Possibly in a sporadic fashion if it's buffered and dumping.". I tried your code and it did not help. It is same on phone too. That problem never happened before. I don't get it. Thank you again. \$\endgroup\$ – Redugsix Aug 5 '15 at 22:34
  • \$\begingroup\$ Apparently I was wrong. I'd assumed it was possible that you were sending data to disk for a few ms before sending your draw call to the gpu, instead of sending data/commands to the gpu before the thread waits for your log to store. A buffered log could flush sporadically rather than every frame. \$\endgroup\$ – RobStone Aug 5 '15 at 22:45
0
\$\begingroup\$

Editing, as i re-read the question again...

Try limiting the delta to 1/60, like:

float yourDelta = Math.min(Gdx.graphics.getDeltaTime(), 1 / 60f); 

Then do:

x += speed * yourDelta * speedMutator;

Does that improve the smoothness?

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.