Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Coordinate System in LibGDX (Java)

LibGDX has a coordinate system where (0,0) is at the bottom-left. (like this image: http://i.stack.imgur.com/jVrJ0.png)

This has me beating my head against a wall, mainly because I'm porting a game I had already made with the usual coordinate system (where 0,0 is in the Top Left Corner).

My question: Is there any simple way of changing this coordinate system?

like image 431
Sosavpm Avatar asked Oct 10 '11 03:10

Sosavpm


People also ask

How is Java coordinate system organized?

Java's Coordinate System By default, the upper-left corner of a GUI component has the coordinates (0, 0). A coordinate pair is composed of an x-coordinate (the horizontal coordinate) and a y-coordinate (the vertical coordinate). The x-coordinate is the horizontal location moving from left to right.

How do you flip a texture in Libgdx?

1 Answer. Show activity on this post. Texture tex = new Texture("path"); Sprite sprite = new Sprite(tex); sprite. flip(true, false);

How do you use coordinate in Java?

By default, Java 2D uses the same coordinate system as AWT. The origin is in the upper-left corner of the drawing surface. X coordinate values increase to the right, and Y coordinate values increase as they go down. When drawing to a screen or an off-screen image, X and Y coordinates are measured in pixels.


1 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 a TextureAtlas, all you need to do in addition to that is call region.flip(false, true).

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

  • your simulation code will most likely use a standard euclidian coordinate system with y-up
  • if you go 3D you have y-up
  • The default coordinate system is a right handed one in OpenGL, with y-up. You can of course easily change that with some matrix magic.

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

  • Pixmap coordinates (top upper left origin, y-down)
  • Touch event coordinates which are given in window coordinates (top upper left origin, y-down)

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

like image 174
badlogic Avatar answered Oct 15 '22 15:10

badlogic