Search code examples
androidlibgdxcoordinatesprojectionorthographic

Get right coordinates on touchdown on android device


I'm programming a button in LIBGX. It works good on desktop, but when I launch it on android, I have to touch in a different place to trigger it (Im using a real Android device, not an AVD) Here's a pic describing it:

enter image description here

Below the TouchDown code:

Gdx.input.setInputProcessor(new InputAdapter () {
       public boolean touchDown (int x, int y, int pointer, int button) {
          // your touch down code here
         Vector3 coords = new Vector3(x, y, 0);
         camara.unproject(coords);

          if(coords.x >= 52 && coords.x<=129 && coords.y >= 158  && coords.y<=253){
              shoot(1);
          }
          return true; // return true to indicate the event was handled
       }
    });

I had the same problem without Vector3, I started using it because it was adviced, but didn't solve much. Here's the declaration of the camera:

camara = new OrthographicCamera();
camara.setToOrtho(false, 800, 480);

I have done some research, but can't find the right solution, and I find the cameras (ortographic, real world, etc) very confusing. I will keep digging, this has taken hours and had to make the question. I hope someone can point me in the right direction.


Solution

  • Please note that the origin (0,0) in Android screen is situated at top-left corner of the screen. Thus, when you add values to y axis the object go towards bottom and when subtract values the object goes upwards.

    In Android devices:

     Origin
     |
     V
     *-------------------------------
     | ----> X axis                  |
     | |                             |
     | |                             |
     | V Y-axis                      |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
     |                               |
      -------------------------------
    

    In desktops:

     --------------------------------------------------------------
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     |                                                             |
     | ^ Y axis                                                    |
     | |                                                           |
     | |                                                           |
     | ----> X axis                                                |
     *-------------------------------------------------------------
     ^
     | 
     Origin   
    

    Possible History:

    As screen space calculations started when television sets were used as screens. The raster gun of a TV is also starting at the top left corner, so this was accepted as the origin.

    For further reference you can refer here