title: Libgdx's World Units
body: <p>I've been trying to learn libgdx a little but now i'm kinda confused about world units there that i don't even know what exact question i should be asking.</p>
  
  <p>Anyway, i've been reading this example game code <a href="https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java" rel="nofollow noreferrer">here</a>
  and from what i understand, when there's no camera, <code>SpriteBatch</code> renders everything in relation to device resolution, so pixels, but when we make a  camera, set it's "size", position and then use <code>batch.setProjectionMatrix(camera.combined)</code>, batch translates pixels to units and then it knows where to render , but in this game there's collision detection between <code>Rectangle</code> objects, and when you're setting position of this <code>Rectangle</code> with <code>rect.set(x, y, 1, 1);</code> where <code>x</code> and <code>y</code> are world units and not pixels, how does rectangle know if it should use those <code>x</code> and <code>y</code> as units and not as pixels if there's nothing used like <code>setProjectionMatrix</code> to let it know that now we're working in units and not in pixels, and then there's this <code>1, 1);</code> at the end, is it in units too? if so, then how does Rectangle know how big those units shoud be (Scale in game is 1 / 16 for example) <code>renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);.</code> </p>
  
  <p>I don't even know if this question really makes sense, but that's where i'm at  and i'm really lost here</p>
  
  <p>EDIT: Ok, i understand how units work now, but collision still confuses me a bit, here's example, i created this </p>
  
  <pre><code>// onCreate
      mapSprite = new Sprite(new Texture(Gdx.files.internal("space.png")));
              planetTexture = new Texture(Gdx.files.internal("planet.png"));
              shapeRenderer = new ShapeRenderer();
  
  
             //Planet(X,Y,Radius)
          planet = new Planet(20, 30, 7);
          planet2 = new Planet(70, 50, 8);
          circle = new Circle(planet.getPosition().x + planet.radius, planet.getPosition().y + planet.radius, planet.radius);
          circle2 = new Circle(planet2.getPosition().x + planet2.radius, planet2.getPosition().y + planet2.radius, planet2.radius);
  
  
          mapSprite.setPosition(0,0);
          mapSprite.setSize(133, 100);
  
          stateTime = 0;
  
          camera = new OrthographicCamera();
          camera.setToOrtho(false, 133, 100);
  
          camera.position.set(133 /2, 100 /2, 0);
          camera.update();
  
          batch = new SpriteBatch();
  ...
  // Render Method
  
  batch.setProjectionMatrix(camera.combined);
          batch.begin();
          mapSprite.draw(batch);
          batch.draw(planetTexture, planet.getPosition().x, planet.getPosition().y, planet.radius * 2, planet.radius * 2);
          batch.draw(planetTexture, planet2.getPosition().x, planet2.getPosition().y, planet2.radius * 2, planet2.radius * 2);
  
          batch.end();
  
          shapeRenderer.setProjectionMatrix(camera.combined);
          shapeRenderer.setColor(Color.RED);
          shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
  
          shapeRenderer.circle(circle.x, circle.y, circle.radius + 1);
          shapeRenderer.end();
  </code></pre>
  
  <p>And everything renders fine, the <code>ShapeRenderer</code> circle is where it's supposed to be after setting ShapeRenderer's projection matrix <code>shapeRenderer.setProjectionMatrix(camera.combined)</code> : <a href="https://i.stack.imgur.com/FQ0XR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FQ0XR.png" alt="enter image description here"></a></p>
  
  <p>But, i still don't understand how to check collision on this circle, when i do this in render method and press on the circle nothing is happening, like i don't get that log and i assume that's because the circle doesn't know the world scale (no nothing like <code>setprojectionmatrix</code> ) and render coordinates and where the actual circle "thinks" it is don't match. How do i check collision on that circle?</p>
  
  <pre><code>if(Gdx.input.justTouched()) {
  
      if(circle.contains(Gdx.input.getX(), Gdx.input.getY())) {
  
                      Gdx.app.log("SCREEN", "TOUCHED AND CONTAINS");
      }
  }
  </code></pre>
  
  <p>EDIT2: I get it now, everything works, i just wasn't using <code>camera.unproject</code> on touch coordinates</p>