# Laggy empty project at 60FPS

@Override
public void create() {
batch = new SpriteBatch();
}

@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.

• 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 ? – dimitris93 Aug 5 '15 at 14:43
• It was a copy/paste mistake. Thanks. Do you have any idea ? – Redugsix Aug 5 '15 at 14:49
• 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. – dimitris93 Aug 5 '15 at 15:01
• 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 ? – Redugsix Aug 5 '15 at 18:25
• Obligatory go to gafferongames.com/game-physics/fix-your-timestep – Gustavo Maciel Oct 15 '16 at 0:32

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();
}

@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+'"');
}

• 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. – Redugsix Aug 5 '15 at 22:34
• 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. – RobStone Aug 5 '15 at 22:45

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?