2
\$\begingroup\$

Is it possible to find the distance of a pixel to a rendered sphere, in screen space? All my naive solutions for just using the 2D screen distances are failing because of the warping that occurs during projection.

\$\endgroup\$
1
  • \$\begingroup\$ See maybe Closest point on an ellipsoid. But I'm not sure that's really what you are looking for. Could you maybe explain what is the purpose of knowing that distance? \$\endgroup\$ May 26, 2012 at 14:24

2 Answers 2

2
\$\begingroup\$

It's definitely possible, though it doesn't have a clean explicit formula. The projection of a sphere is an ellipse, and given the projection matrix you should be able to find an explicit formula for the ellipse, something of the form ax^2+bxy+cy^2+dx+ey+f=0 (the canonical formula for a conic section); from this you can find a rational parametrization of the ellipse (of the form x=f(t), y=g(t) with f and g rational functions). The problem then comes down to minimizing (f(t)-x_0)^2+(g(t)-y_0)^2 as a function of t, and it turns out that rootfinding methods work pretty well for this - there are exact solutions but they require solving a degree-4 equation, which can involve hairy complex arithmetic and tends to be less numerically stable (and thus less accurate) than the approximate methods anyway.

\$\endgroup\$
0
\$\begingroup\$

Your screen is a plan. Your sphere is an elipse. When the user clicks somewhere on the screen it's easy to find the coordinates on the screen plan. Then you just have to apply basic elipsis equation in order to know if the selected point is inside or outside the elipsis and how far it is.

\$\endgroup\$
1
  • \$\begingroup\$ This works if the question is 'is my click point inside the ellipse or not?' but not if the question is 'how far is my click point from the ellipse?' - there the calculation of, say, (x/2)^2+(y/3)^2 doesn't result in a distance value. \$\endgroup\$ May 26, 2012 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .