LibGDX: How does unprojecting world coordinates work?

I'm having issues on a conceptual level understanding this, as most articles/questions don't go in depth.

I understand that the camera focuses on a part of the game world, like a real camera would.

So world coordinates are the coordinates/measurements of the world, correct? and Screen coordinates are the coordinates of the screen (whether it's a phone or a Desktop Launcher), right? Do viewports handle both the world size/coordinates and also the intended screen size/coordinates?

Whenever I use the unproject method, whether by calling viewport.unproject or camera.unproject (with Gdx.input.getX() and Gdx.input.getY() as parameters for the Vector3 argument for the unproject method), nothing else changes.

My intended game width and heigh are 136 and 204 (aspect ratio). I have methods such as "if x >32 ...." but obviously thsee create issues because when I run it on different devices (or even my desktop launcher), the coordinate at 32 is very different. How do I make it the same? Is this what unproject method solves?

How is it supposed to work? Is it designed so that wherever I touch on the screen, the touch will be mapped to what ever is on the world where I touched?

Usually I will call this:

Gdx.app.log("x", Gdx.input.getX() + "");
Gdx.app.log("y", Gdx.input.getY() + "");

But when I call the unproject methods, the coordinates stay the same. Am I not interpreting it right?

Is it better to just use box2d actors instead? Or will I get the same issues?

1 answer

  • answered 2018-01-17 04:16 Lestat

    you must use it like this:

    Vector3 touch;
    
    cam.unproject(touch.set(Gdx.input.getX(), Gdx.input.getY(), 0));
    

    then touch.x and touch.y are your unprojected coordinates.