Question

I have the Coordinate System for Rendering set to:

camera.setToOrtho(false, 288, 512);

But the touch-coordinate system is the other one (true, not false)

How can I flip this to read inputs at special positions?

Was it helpful?

Solution

Simply flipping isn't the only thing you should do with touch inputs. Inputs are given in pixel coordinates, while your camera uses a 288x512 units system regardless of the display size. Whenever you get touch events, they are given in the current display's pixel coordinates, which is different for different phones and even changes when you resize the screen on desktop. This is the code you need:

Vector3 v3 = new Vector3(screenX, screenY, 0);
camera.unproject(v3);

screenX and screenY are the touch coordinates given to InputProcessor functions or returned by Input.getX() and Input.getY(). The 0 is needed because Camera.unproject() takes a Vector3, it's ignored for an OrthographicCamera.

This will set v3.x and v3.y to the touch coordinates in the camera's coordinate system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top