5
\$\begingroup\$

I'm writing a game in Java for Android using LibGDX, and I connected my phone so I can test it on it.

I know that LibGDX's render system works different than regular Java systems since the 0,0 coordinate is in the lower-left corner, while in java it usually is in the upper-left corner.

But now I use the touchDown method out of the GestureListener class, it's like the regular Java rendering system: when I obviously tap in the left down corner of my screen, I debug-messaged the coordinates and it says it's Y-coordinate is 475, while I render the object at 0,0.

enter image description here

Is there a way to fix this easily?

\$\endgroup\$
  • 2
    \$\begingroup\$ screenHeight - y maybe? That's a quick-and-dirty fix... \$\endgroup\$ – Alexandre Desbiens Jun 29 '15 at 16:25
  • \$\begingroup\$ @AlexandreDesbiens Tested it, it works, thought that I needed to do a lot more to do it that way because it could be -400 or so as well, doesn't seem like it does that, thank you. \$\endgroup\$ – MrGussio Jun 29 '15 at 16:32
4
\$\begingroup\$

The problem you are facing is conversion between two different coordinates systems: the graphical one and the input one.

Graphics coordinates

Like you said, libGDX uses a 1 to 1 ratio between space coordinates and pixels, and starts in the bottom left corner. But it can be anything, really. That is just the default behavior of libGDX. You could change the origin of the coordinates system, flip the axis and even scale them.

Under the hood there is a graphics library called OpenGL. This graphics library will use a transformation matrix in order to draw shapes on the screen. Its default matrix will create the following coordinates system:

enter image description here

So as you can see, we are far away from a pixel-perfect camera.

Input coordinates

Each hardware can have a different implementation of this, but the most popular by far is to use the top left corner for the origin and use a 1 unit to 1 pixel ratio. So when you poll a mouse (or touch) position, (0,0) will be top-left, and the position will match screen size in pixels.

Conversion

What you need to do now is convert the input coordinate to fit in the graphics space. In your example, it is quite simple: you only need to take the height of the screen and subtract the Y position of the touch. This will "flip" the Y position, thus giving you the value you wanted:

// This will flip the Y value of the touch
graphicsY = screenHeight - touchY;

Please note that this will only work if both the graphical and input spaces are the same height. If not, you will need to multiply this value with the ratio of the two spaces:

// Here we add the ratio
graphicsY = (screenHeight - touchY) * (graphicsHeight / screenHeight);
\$\endgroup\$
  • 2
    \$\begingroup\$ Tip: in case you don't use the default camera and is passing the viewport size of your own, then check the method Camera.unproject(Vector3) \$\endgroup\$ – Gustavo Maciel Jun 29 '15 at 21:36
1
\$\begingroup\$

had a similar problem... figured out that when u draw, pixels start from bottom left, but when u need to obtain position of something, coordinates that you obtain start from top left(in my case). This means that whats for you is the lowest Y, for libgdx its the highest. u need to do something like Gdx.graphics.getHeight()-screenY. Anaways you just need to understand how coordinates work

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.