点击一个球体 [英] clicking on a sphere
问题描述
球体可以自由旋转。
如何确定用户点击的球体上的点?
鉴于:
- 显示器的高度和宽度
- 投影圆的半径,以像素为单位
- 用户点击的坐标
假设左上角是(0,0),随着您向右移动x值会增加,并且随着您向下移动,y值会增加。
将用户的点击点转换为地球坐标空间。 / p>
userPoint.x - = monitor.width / 2
userPoint.y - = monitor.height / 2
userPoint.x / = circleRadius
userPoint.y / = circleRadius
找到交点的z坐标。
//解析z
// x ^ 2 + y ^ 2 + z ^ 2 = 1
//我们知道x和y,来自userPoint
如果(x ^ 2 + y ^ 2> 2)满足下面的等式, 1){
//用户在球体外点击。翻转
返回-1;
}
//负的sqrt比正的更接近屏幕,所以我们更喜欢这一点。
z = -sqrt(1 - x ^ 2 - y ^ 2);
现在您知道了(x,y,z)交点,和经度。
假设全球面向用户的中心是0E 0N,
经度= 90 + toDegrees(atan2(z,x));
lattitude = toDegrees(atan2(y,sqrt(x ^ 2 + z ^ 2)))
如果球体被旋转以便0E子午线不直接面对观察者,从经度减去旋转角度。
I have a unit sphere (radius 1) that is drawn centred in orthogonal projection.
The sphere may rotate freely.
How can I determine the point on the sphere that the user clicks on?
Given:
- the height and width of the monitor
- the radius of the projected circle, in pixels
- the coordinates of the point the user clicked on
And assuming that the top-left corner is (0,0), the x value increases as you travel to the right, and the y value increases as you travel down.
Translate the user's click point into the coordinate space of the globe.
userPoint.x -= monitor.width/2
userPoint.y -= monitor.height/2
userPoint.x /= circleRadius
userPoint.y /= circleRadius
Find the z coordinate of the point of intersection.
//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);
Now that you know the (x,y,z) point of intersection, you can find the lattitude and longitude.
Assuming that the center of the globe facing the user is 0E 0N,
longitude = 90 + toDegrees(atan2(z, x));
lattitude = toDegrees(atan2(y, sqrt(x^2 + z^2)))
If the sphere is rotated so that the 0E meridian is not directly facing the viewer, subtract the angle of rotation from the longitude.
这篇关于点击一个球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!