1

as I understood, libGDX coordinate system set by default, so the (0,0) is located at the bottom left corner, like in the following picture:

img
(source: gyazo.com)

Is it possible to make it work like JFrame, where the default position is at the top left corner?

Anyhow, back to my actual question, android (OR at least my phone) touch coordinate system is working by full screen (when phone is laying on it's side) so the default (0, 0) position is located at the top right corner, like in the following picture:

img2
(source: gyazo.com)

So what happens when I simply do this:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    System.out.println("X: " + screenX  + " Y: " + screenY);
    this.instance.player.setLocation(screenX, screenY);
    return false;
}

I simply get the X, Y of touched screen (based on android's fullscreen coordinate system) and then use it on libgdx coordinate system which is totally different,

so if I touched at the top right corner, my blue rectangle will appear at bottom left corner.

Is there a way to make everything work by the top left corner like on PC?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Artemkller545
  • 979
  • 3
  • 21
  • 55

6 Answers6

4

You'll need to use a camera to do this. If you're not using a camera, I'd highly recommend adding one in, as it makes things a lot easier. Simply call camera.setToOrtho(true), which will switch LibGDX's coordinate system to the one you want, (0,0) in the top left.

To do this with a certain viewport width and height other than what you get from Gdx.graphics.getWidth/Height(), you can call camera.setToOrtho(true, viewportWidth, viewportHeight).

kabb
  • 2,474
  • 2
  • 17
  • 24
  • Are you drawing everything using the camera as well? Make sure you set your SpriteBatch's projection matrix to camera.combined. – kabb Mar 04 '14 at 22:58
0

I don't know how to set it at top left corner but one thing you could is to convert input coordinate to libgdx coordinate. I hope that could help you or help somebody else :

int Screen_height = Gdx.graphics.getHeight(); // Your screen height    

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
   System.out.println("X: " + screenX  + " Y: " + (Screen_height - screenY));
   this.instance.player.setLocation(screenX, (Screen_height - screenY));
return false;
}
LeSam
  • 1,235
  • 4
  • 18
  • 38
0

Yes, and it's very easy. You just have to subtract from the Y value the height of the screen. So: The finger position Y = Heigh of the screen - the Y value you have found.

0

There's an easier way: Just make the subtraction bellow

Gdx.graphics.getHeight()-screenY

Only this line of code is necessary, as you can see.

  • I'm sorry people, I wrote the same thing a lot of times because of an ERROR in the Stackoverflow app that wasn't showing the answers I had posted... sorry – BeginnerDev Dec 22 '17 at 19:23
0

Create the variable fingerY and use the subtraction:

fingerY= Gdx.graphics.getHeight()-screenY;

So, if screenY equals 10 from the left bottom corner and the height of the screen equals 240 pixels:

fingerY=240-10

Then fingerY=230 pixels from the bottom left corner, while it is 10 pixels from the top left corner.

-1

You need to use the method project(Vector3 worldCoords) in class com.badlogic.gdx.graphics.Camera.

private Camera camera;
............

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

Create an instance of the vector and initialize it with the coordinates of the input event handler.

    Vector3 worldCoors = new Vector3(screenX, screenY, 0);

Projects the worldCoors given in world space to screen coordinates.

    camera.project(worldCoors);

Use projected coordinates.

    world.hitPoint((int) worldCoors.x, (int) worldCoors.y);

    OnTouch();

    return true;
}