Android Question Reversing the Y Coordinates in libGDX

wonder

Expert
Licensed User
Longtime User
Hey guys!!

Reading the tutorials, to my understanding so far, a 2D openGL surface has its origin at the bottom-left corner.

Since I'm porting a game that performs all the calculations considering that any 2D surface should have its origin in the top-left corner, I wanted to find a way to reverse the Y coordinate system used by libGDX.

Searching the internet, I found a guy with the same problem as me and a given solution:
http://stackoverflow.com/questions/7708379/changing-the-coordinate-system-in-libgdx-java

I copy-pasted the camera code into B4A but it didn't work, I'm guessing it's because it's in Java.

Can anyone help me with this? Perhaps @Informatix :)


EDIT: SOLVED!!

B4X:
Sub LG_Resize(Width As Int, Height As Int)

    'Sets the camera viewport
    Camera.Initialize   
    Camera.SetToOrtho2(True, lGdx.graphics.Width, lGdx.Graphics.Height)

End Sub

...and after setting the dimensions of the region to be rendered:

B4X:
actor(active_actor).Flip(False, True)


Sorry for bothering... :D
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Yes, you could reverse the Y direction this way but you will face a few problems. The best thing to do is to adapt all your Y positions. The computation is pretty easy: lGdx.Graphics.Height - Y (in your game) - Sprite.Height. Thus for a sprite located at Y=500 with a height of 100 in a screen 1280x764, the new Y will be 764-500-100=164.
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
lGdx.Graphics.Height - Y (in your game) - Sprite.Height
This will work if the origin of the sprite is in the lower left corner. If it has been moved, then that must be taken into account. Might be best to move the origin to where the original game's engine expects it to be. Most likely you would need either mySprite.SetOriginCenter (for the center) or mySPrite.SetOrigin(0,mySprite.Height) (for upper left corner. Then the calculation would just be lGdx.Graphics.Height - y
 
Upvote 0
Top