Changing the coordinate system in LibGDX (Java)

LibGDX has a coordinate system where (0,0) is in the lower left corner. (for example, this image: http://i.stack.imgur.com/jVrJ0.png )

This I hit my head against the wall, mainly because I am porting a game that I have already done with the usual coordinate system (where 0,0 is in the upper left corner).

My question is: Is there an easy way to change this coordinate system?

+47
java 2d libgdx coordinate-systems sprite
Oct 10 2018-11-11T00:
source share
6 answers

If you use a camera (which you should), changing the coordinate system is pretty simple:

camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

If you use TextureRegions and / or TextureAtlas, all you need to do in addition to this is call region.flip (false, true).

The reasons we use y-up by default (which you can easily change as shown above) are as follows:

  • your simulation code will most likely use the standard Euclidean coordinate system with y-up
  • If you go in 3D, you have y-up
  • The default coordinate system is right-handed in OpenGL with y-up. Of course, you can easily change this with some magic.

The only two places in libgdx where we use y-down are:

  • Pixel coordinates (top left, y-down)
  • Touch the event coordinates indicated in the window coordinates (top left, y-down)

Again, you can easily change the coordinate system used to whatever you want using either the Camera or a tiny bit of math.

+106
Oct 13 '11 at 8:11
source share

Just to talk a bit about what I said above, if you use TextureAtlas (with TextureRegions), you need to flip them, as badlogic said, in addition to working with the camera. If you use TextureAtlas, you can use this code immediately after loading the atlas:

 String textureFile = "data/textures.txt"; atlas = new TextureAtlas(Gdx.files.internal(textureFile), Gdx.files.internal("data")); // Let flip all the regions. Required for y=0 is TOP Array<AtlasRegion> tr = atlas.getRegions(); for (int i = 0; i < tr.size; i++) { TextureRegion t = tr.get(i); t.flip(false, true); } 
+9
Aug 24 '13 at 3:25
source share

If you want to hide the transformation and not think about it after installing it once, you can create a class that inherits all the functions you need, but first converts the coordinates before passing it to the parent class function. Unfortunately, this will take a long time.

Alternatively, you can make a method that performs a simple transformation y' = height - y entire Coordinate object (or whatever you use) and call it once before each operation.

+6
Oct 10 '11 at 4:13
source share

An interesting graphics library, I would say. I found this rating from the link below:

Another problem was that different coordinate systems were used in different parts of Libgdx . Sometimes the origin of the axes occurred in the lower left corner with the y axis pointing up, and sometimes in the upper left corner of the sprite directed down. When drawing, Grids occurred even in the center of the screen. This caused quite a bit of confusion and extra work to get everything to the right place on the screen.

http://www.csc.kth.se/utbildning/kandidatexjobb/datateknik/2011/rapport/ahmed_rakiv_OCH_aule_jonas_K11072.pdf

+5
Oct 10 '11 at 4:20
source share

I just created a class that extends SpriteBatch , which overrides some methods by adding y = Gdx.graphics.getHeight() - y - height . Simple but effective.

+1
Aug 18 '16 at 19:33
source share

Not sure if this helps someone or not, but I was able to correctly process the textures and fonts using the proposed inverted coordinate system through OrthographicCamera. Here is what I did:

 private SpriteBatch batch; private BitmapFont font; private OrthographicCamera cam; private Texture tex; @Override public void create () { batch = new SpriteBatch(); font = new BitmapFont(true); font.setColor(Color.WHITE); cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); tex = new Texture("badlogic.jpg"); } @Override public void dispose() { batch.dispose(); font.dispose(); tex.dispose(); } @Override public void render () { cam.update(); batch.setProjectionMatrix(cam.combined); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); font.draw(batch, "Test", 50, 50); batch.draw(tex, 100, 100, tex.getWidth(), tex.getHeight(), 0, 0, tex.getWidth(), tex.getHeight(), false, true); batch.end(); } 

It is important to note:

  • BitmapFont constructor, boolean flips the font
  • For batch.draw (), you need to use all these parameters because you need the boolean flipY at the end to flip the texture (I can extend SpriteBatch or make a utility method so as not to skip so many parameters all the time.)
  • Check out the batch.setProjectionMatrix package (cam.combined); in render ()

Now we will see if I come back here later, will make changes to fix any other problems or discoveries by doing all this. :)

0
04 Oct '15 at 18:33
source share



All Articles