点击一个球体

3
我有一个半径为1的单位球,以正交投影为中心绘制。
球体可以自由旋转。
如何确定用户单击的球面上的点? enter image description here
2个回答

3

假设有以下信息:

  • 显示器的高度和宽度
  • 投影圆的半径(以像素表示)
  • 用户点击点的坐标

并且假设左上角的坐标为 (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个三角形。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接