LibGDX Camera and Coordinates

539 views Asked by At

I'm programming a game but I've run into a problem regarding cameras I've made the camera follow an object, but it messes up the coordinates. It seems that no matter where I click, it's as if I'm always interacting with the same coordinate plane. For example, if I were to click at the point (500,500) (from the screen's perspective), the world would react correctly. However, if I were to move the player (+200,+200), the camera would shift accordingly, but if I were to then click at (500,500) (from the screen's perspective), instead of reacting at (700,700), the game would still react at (500,500), or from the screen's perspective, (300,300). Some photos are below. Green circle is the object's position where the camera and game's coordinates align. Red circle is where I click. Blue circle is where the click registers in the game ![normal]https://i.stack.imgur.com/tJlhv.png Here I've moved the object so that the camera's coordinates matches up to the game's coordinates. Clicking a point correctly registers in the game. ![player moved upright]https://i.stack.imgur.com/5pUkw.png Here I've moved the object right of the alignment position. Clicking below the object registers the click in the game below the original position. The camera's perspective is wonky; how to fix that?

1

There are 1 answers

2
chein On

If you are using Gdx.input.get to poll for input it will always give you back coordinates from the screen (with origin at the top left corner). If you want world coordinates you could use Camera.unproject() which will convert the given screen coordinates to world space. Something like this:

Vector3 touchPoint = new Vector3(Gdx.input.getx(), Gdx.input.gety(), 0);
camera.unproject(touchPoint);