title: LibGdx How to Scroll using OrthographicCamera?
body: <p><img src="https://i.stack.imgur.com/Jiafh.png" alt="enter image description here" /></p>
  <p>I have been looking for 10 hours (literally) and I'm done, I need to ask. Thing is I'm learning How use LibGdx to program Java games. I'm doing a Horizontal Space Ship Game. So, my worst problem here is that I do not know how do scroll (I think draw will explain better). I want to draw a huge background (Space) and make my OrthographicCamera move right like with my SpaceShip, so it will create a Scroll effect with the Space Background. No enemies and nothing but the ship on the screen.</p>
  <p>I'm trying this:</p>
  <pre><code> public void moveCamera(float x,float y){
      cam.position.set(x, y, 0);
   }
  </code></pre>
  <p>Then I use that method in my WorldRender render() method:</p>
  <pre><code>public void render(float delta){ 
      ship.Move(delta);
      moveCamera(ship.getPosition().x,ship.getPosition().y);
      cam.update();
      System.out.println(&quot;&quot;+cam.position);
      spriteBatch.begin();
          drawBackground();
          drawShip();
      spriteBatch.end();
  }
  </code></pre>
  <p>I actually move the camera position (I can see that thanks to the println), but It isn't moving in the game, so SpaceShip just disappears by the edge of the window.</p>
  <p>I also tried this before spriteBatch.end()</p>
  <pre><code>spriteBatch.setProjectionMatrix(camera.combined);
  </code></pre>
  <p>but when I do that windows only shows a black screen, no ship, no nothing.
  As I said, I'm desperate, I see lot of examples (scroll with mouse, paralexscrolling etc) but all are to advanced or just nothing to do with my code.</p>
  <p>This is how I draw stuff. Background and ship are textures inside WorldRender. I draw background image very wide, so my intention is do some scrolling over as I said. That's the code</p>
  <pre><code>private void loadTextures(){
      shipTexture=new Texture(Gdx.files.internal(&quot;nave.png&quot;));
      background=new Texture(Gdx.files.internal(&quot;fondo.jpg&quot;));
  }
  
  public void drawShip(){
      spriteBatch.draw(shipTexture,ship.getPosition().x*ppuX,ship.getPosition().y*ppuY, ship.WIDTH*ppuX,ship.HEIGHT*ppuY);
  }
  
  public void drawBackground(){
      spriteBatch.draw(background, -10*ppuX,0*ppuY, Gdx.graphics.getWidth()*10,Gdx.graphics.getHeight());     
  }
  </code></pre>
  <p>Here you can download the code if someone want to help in hardcore mode</p>
  <p><a href="http://ubuntuone.com/3IdlECzw1zdJnqfj7hKDd0" rel="nofollow noreferrer">My code (not working)</a></p>
  <p>I FINALLY SOLVED IT!</p>
  <p>That's the code I used in a class name WorldRenderer, which have methods that are called within GameScreen for render, resize etc</p>
  <pre><code> public WorldRenderer(World world) {
      // TODO Auto-generated constructor stub
      
      this.world=world;
      this.ship=world.getShip();
      this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
      
      this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
      this.cam.position.set(ship.getPosition().x,CAMERA_HEIGHT/2,0);
      this.cam.update();//actualizamos la camara
      spriteBatch=new SpriteBatch();
      loadTextures();
  }
  
  private void loadTextures(){
      shipTexture=new Texture(Gdx.files.internal(&quot;nave.png&quot;));
      background=new Texture(Gdx.files.internal(&quot;fondo.jpg&quot;));
  }
  
    public void drawShip(){
            spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y,10,10);
  }
  
  public void drawBackground(){
      spriteBatch.draw(background, 0,0, 500,50);
  } 
  
   public void render(float delta){ 
      ship.Move(delta);
      moveCamera(ship.getPosition().x);
      spriteBatch.setProjectionMatrix(cam.combined); 
      spriteBatch.begin();
          drawBackground();
          drawShip();
      spriteBatch.end();
  }
  
   public void moveCemara(float x){
      cam.position.set(x+20,cam.position.y, 0);
      cam.update();
  }
  </code></pre>
  <p>Inside the Ship I have this method which I call within render in WorldRenderer to move It</p>
  <pre><code>public void Move(float delta){
      if(Gdx.input.isKeyPressed(Keys.LEFT)) this.position.x -=velocity *delta;
      if(Gdx.input.isKeyPressed(Keys.RIGHT)) this.position.x +=velocity *delta;
      if(Gdx.input.isKeyPressed(Keys.UP)) this.position.y +=velocity *delta;
      if(Gdx.input.isKeyPressed(Keys.DOWN)) this.position.y -=velocity *delta;
  }
  </code></pre>
  <p>Also I want to thanks very much to the people who helped me. I'm marking first answer as the good one, but, mix both was what gave me the real solution.</p>
  <p>I leave here some tutorials I followed which are pretty good for noobs</p>
  <p>That's a good everything-from-scratching-tutorial
  <a href="http://bitowl.de/de/adventskalender/" rel="nofollow noreferrer">LiGdxForNoobs</a></p>
  <p>A simple platform game
  <a href="http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/" rel="nofollow noreferrer">platformGame</a></p>
  <p>A very simple game
  <a href="http://code.google.com/p/libgdx/wiki/SimpleApp" rel="nofollow noreferrer">bucketGame</a></p>