# Libgdx: movement of scrolling background is not smooth even at 60fps

I'm developing a game and I want to make a sliding background. The background is not scrolling smoothly. I logged the fps, but it was ok. This is my code, I don't know what I'm doing wrong, any ideas?

public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
Texture back;
float offset =  0f;

FPSLogger logger = new FPSLogger();
@Override
public void create() {
camera = new OrthographicCamera(320, 480);
camera.position.set(320 / 2, 480 / 2, 0);
camera.update();
batch = new SpriteBatch();
batch.setProjectionMatrix(camera.combined);
back= new Texture(Gdx.files.internal("back.png"));
}

@Override
public void dispose() {
batch.dispose();
}

@Override
public void render() {
logger.log();
offset +=  (400f * Gdx.graphics.getDeltaTime());
offset = offset % 480f;
batch.begin();
batch.draw(back, 0, offset);
batch.draw(back, 0, offset - 480);
batch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}


I uploaded the code to bring more information, I looked for on internet and a lot of people have the same problem, but I didn't find solution. I have almost finished the game, change the library will be a pity, any can help me? Thanks! This is the code: https://www.dropbox.com/sh/t26gt33wcex5izn/AAD8-yBSOztZfHe4wzDPRjjQa

• Is offset an integer? If so, using a float might improve the smoothness. Just a wild guess though. May 9 '14 at 2:42
• Declare offset as a float instead of int or DWORD or whatever integral type you are using. Also, why are you drawing twice instead of once? And try to put parantheses like that: offset += ( 400 * Gdx.graphics.getDeltaTime() ); May 9 '14 at 4:43
• Thank you! Yes it was integer, and I fixed it, but it's still not smooth. May 10 '14 at 13:27

Without any further information (and you don't provide enough for us to give a positive answer for sure), the most likely solution for your problem is that you're introducing some granularity you don't realise. For example the line:

offset += 400 * Gdx.graphics.getDeltaTime();

Gdx.graphics.getDeltaTime() will return a float, and it will likely be a very small float. At 60fps, the result of that equation will be somewhere near 6.6666666... But if offset is an integer, then it will either increase by 7 units each update. That .6666666 will get thrown away, and your background will move 0.3333333 further than it should have done. Over time that error will accumulate. If the frame rate was perfectly constant, you might not notice, because it would always be 6, or always 7, and would appear perfectly constant as well, even though it's actually moving slower or faster than it actually should be.

If your framerate varies up to ~62 fps, then offset will suddenly start advancing by 6 every update instead of 7, a decrease in speed of almost ~15% even though the frame-rate has only increased by ~3%. This will manifest as a kind of jitter, moving slower or faster as the frame-rate varies.

The obvious solution there is to make offset a float, so that any movement less than 1px is preserved, and when accumulated will give the correct rate of motion. That may not be the only extra granularity in your system though, but you've not given us enough information to diagnose any better than that.

• Thank you for the answer. I apologize for my bad english. You was righh, and I fixed it, but it's still not smooth. I edited the code, to give you more information, but the code it´s really simple. Thank you! May 10 '14 at 13:25