Thursday, 15 July 2010

math - clicking on a sphere -



math - clicking on a sphere -

i have unit sphere (radius 1) drawn centred in orthogonal projection.

the sphere may rotate freely.

how can determine point on sphere user clicks on?

given:

the height , width of monitor the radius of projected circle, in pixels the coordinates of point user clicked on

and assuming top-left corner (0,0), x value increases travel right, , y value increases travel down.

translate user's click point coordinate space of globe.

userpoint.x -= monitor.width/2 userpoint.y -= monitor.height/2 userpoint.x /= circleradius userpoint.y /= circleradius

find z coordinate of point of intersection.

//solve z //x^2 + y^2 + z^2 = 1 //we know x , y, 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 homecoming -1; } //the negative sqrt closer screen positive one, prefer that. z = -sqrt(1 - x^2 - y^2);

now know (x,y,z) point of intersection, can find lattitude , longitude.

assuming center of globe facing user 0e 0n,

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

if sphere rotated 0e meridian not straight facing viewer, subtract angle of rotation longitude.

math graphics geometry

No comments:

Post a Comment