2013-02-19 81 views
3

我有一個以正交投影爲中心繪製的單位球體(半徑1)。點擊球體

球體可以自由旋轉。

如何確定用戶點擊的球體上的點?

enter image description here

回答

2

鑑於:

  • 的監視器
  • 投影圓的半徑的高度和寬度,以像素爲單位
  • 用戶點擊

的點的座標和假設左上角爲(0,0),則隨着您向右移動x值會增加,並且隨着您向下移動y值會增加。

將用戶的點擊點轉換爲地球的座標空間。

userPoint.x -= monitor.width/2 
userPoint.y -= monitor.height/2 
userPoint.x /= circleRadius 
userPoint.y /= circleRadius 

找到交點的z座標。

//solve for z 
//x^2 + y^2 + z^2 = 1 
//we know x and y, from userPoint 
//z^2 = 1 - x^2 - y^2 
x = userPoint.x 
y = userPoint.y 

if (x^2 + y^2 > 1){ 
    //user clicked outside of sphere. flip out 
    return -1; 
} 

//The negative sqrt is closer to the screen than the positive one, so we prefer that. 
z = -sqrt(1 - x^2 - y^2); 

現在你已經知道了(x,y,z)交點,你可以找到經度和緯度。

假設面向使用者的眼球的中心是0E 0N,

longitude = 90 + toDegrees(atan2(z, x)); 
lattitude = toDegrees(atan2(y, sqrt(x^2 + z^2))) 

如果球被旋轉,使得0E子午線不直接面對觀察者,從經度減去旋轉角度。

1

一種可能的方法是生成從三角形的球體,由行和列組成。它們也可以看不見。然後用鼠標拾取光線對這些三角形進行打擊測試。

Latitude/longitude grid

看到這張照片的經/緯度格,但其應用更加密集。對於每個網格單元格,您需要2個三角形。