# LibGDX - Best way of scrolling background

I'm new to LibGDX and i'm trying to implement a screen where i have a guy running and the ground under him scrolling to give impression he is really running in a sidewalk for example.

The ground should be interactive meaning it will not only change his colors or Texture but there will also be holes in it and so on. As you can see from the picture below each brick is a Block Object and i'd like to keep it this way.

Do you think in this case the best thing is to move the camera so i can show the screen ahead or try to scroll the background elements editing their position?

Update

So to be more clear, each block is separated from the others so it is impossible to "scroll the background" since there is no a real background, also each block will have different properties, it means it can be a block or a hole or whatever, basically i see the possibility of the scrolling camera as only solution.

• An appropriate solution is dependent on some of the other systems of your game, like how your player moves, and how your levels are implemented. Can you give more details about your existing system or plans? Without those details, there is no answer that's better than any other. – Seth Battin Mar 7 '15 at 16:52
• @SethBattin i just edited my answer, i tried to be more clear as possible but actually the question wants to be very generic. – TheRedFox Mar 11 '15 at 17:04
• Don't try to make a question generic; again, that actively harms its ability to get good answers. It may seem counter-intuitive, but the best way to provide help to others (I assume that's why you value general applicability), is to ask a very specific question so that someone in a similar specific situation can search and find a helpful, specific answer. By making questions vague and general, there's no reason anyone would find it amid all the other vague, unspecific advice that makes the internet noisy. – Seth Battin Mar 12 '15 at 2:56
• Regarding your edits, you haven't really clarified your problem. It's ok that your tiles aren't technically background; you already stated that. How do they move? How do your store their data? How does the player move? How does the player interact with the tiles? Do you only scroll, or are there other transitions? Do you need the camera to move in different ways? You commented below that your current solution has poor performance, so can you elaborate about why? Specific, specific, specific, specific. – Seth Battin Mar 12 '15 at 3:00

You could move the background or make a new orthographic camera. Then do some thing like

camera.setPosition(player.x, player.y);

• That's what i'm doing now, it seems to perform well. Don't forget to call camera.update() otherwise the position will not change. – TheRedFox Mar 11 '15 at 17:11

Moving background has slightly better performance than moving camera. On the other hand, moving camera is more intuitive and that's what we see in real life. It's all up to you.

• How do you figure that out? The entire viewport/camera system is designed around the camera moving. – RichieHH Aug 23 '15 at 12:01
• When you move background, you change only its x and y. When you move camera, a bit more work is performed. – nikoliazekter Aug 23 '15 at 12:05

I had made a game similar to yours with almost similar logic ,I had moved camera,instead of moving each element individually as moving camera is easier and saves lot of logic's. I had a class cameraHelper that gets and sets all necessary details about camera

import com.badlogic.gdx.graphics.OrthographicCamera;

public class CameraHelper {

Vector3 position;

OrthographicCamera camera;

public Vector3 getPosition()
{
return position;
}
public void setPosition()
{
camera.position.set(position);
}
public void update(float deltaTime,int velocity)
{
position.y=position.y+deltaTime*velocity*60;
setPosition();

}
}


The below is the render loop to scroll the background and draws the background based on the position of the camera

@Override
public void render(SpriteBatch batch)//draw background on screen
{
if(cameraHelp.getPosition().y-height/2>=j*height)
{

k=j;
j=j+2;
}
for ( i = k; i < j+1; i++)
batch.draw(backgroundTexture, 0, i * height, width, height);

}