2014-08-28 48 views
1

我正在編程LIBGX中的一個按鈕。它適用於臺式機好,但是當我啓動它在Android上,我有一個不同的地方接觸(使用真正的Android設備,而不是一個AVD林)來觸發它下面是描述它一峯:在Android設備上着落時獲得正確的座標

enter image description here

下面的TouchDown代碼:

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 
     } 
    }); 

我有同樣的問題沒有的Vector3,我開始使用它,因爲它是勸,但沒有太多解決。下面是攝像頭的聲明:

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

我做了一些調查,但無法找到合適的解決方案,我發現相機(ortographic,現實世界中,等)十分混亂。我會繼續挖掘,這需要數小時,並且必須提出問題。我希望有人能指出我正確的方向。

+1

不知道我是否可以直接幫你,但我可以告訴你,x和y在Android座標相比,計算機有所反轉。 x和y座標從左上角開始,而不是在其他操作系統上的左下角開始。請參閱:http://stackoverflow.com/questions/11483345/how-android-screen-coordinates-works更多解釋(抱歉,找不到任何直接從文檔中找到的東西)。 – Darwind 2014-08-28 19:26:47

+0

@Darwind謝謝非常,我會看看,謝謝你的評論 – dasjkdj 2014-08-28 19:27:26

回答

1

請注意,Android屏幕中的原產地(0,0)位於左上角屏幕的一角。因此,當您將值添加到y axis時,對象將會向底部移動,當減去值時對象會向上移動。

在Android設備:

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

在臺式機:

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

可能的歷史:

當電視機被用作顯示屏的屏幕空間計算開始。電視的光柵槍也開始於左上角角落,所以這被認爲是原點。

有關進一步的參考,你可以參考here

相關問題