2

I have a sphere centered on the origin $O$. I have a point in space $Cam$ and a vector $Dir$.

How do I find a point $X$ and a Vector Hor where:

  1. The line starting at Cam and of direction Hor, passing through X, is tangent to the sphere in X
  2. Hor, Dir and O-Cam are on the same plane

Illustration

And if possible, with the shortest calculation possible. Technically I will be implementing this in a shader, for every given direction I need to find the horizon line on the planet. I tried to abstract the problem as much as possible.

2 Answers2

0

You stated that the center of the sphere is the origin of our 3-dimensional space, so suppose we have $O = (0, 0, 0)$, $Cam=(c_x, c_y, c_z)$ and $\overrightarrow{Dir}=\langle d_x, d_y, d_z \rangle$, and that the radius of the sphere is $R$.

Let $X = (r_x, r_y, r_z)$ be what we are solving for.

First, we need to find the plane containing both $Dir$ and $Cam$. Taking the cross product of $\overrightarrow{Dir}$ and $\overrightarrow{CamO}$, combined with the fact that the plane should contain $O = (0, 0, 0)$, the equation of our plane $P$ is $(c_zd_y - c_yd_z)x + (c_xd_z - c_zd_x)y + (c_yd_x - c_xd_y)z = 0$. We will later use this to ensure our point $X$ is on this plane.

Next, consider the conditions for $X$ to be the point at which $\overrightarrow{Hor}$ tangentially contacts the sphere. We need the point $X$ to be on the surface of the sphere, and we need the vectors $\overrightarrow{XO}$ and $\overrightarrow{XCam}$ to be perpendicular (in order for $\overrightarrow{XCam}$ to be tangent to the sphere).

For the first condition, we want $r_x^2 + r_y^2 + r_z^2 = R^2$, simply by substituting the coordinates of $X$ into the equation of our sphere.

For the second condition, we want $\overrightarrow{XO} \cdot \overrightarrow{XCam} = 0$. Since $\overrightarrow{XO} = \langle -r_x, -r_y, -r_z \rangle$ and $\overrightarrow{XCam} = \langle c_x - r_x, c_y - r_y, c_z - r_z \rangle$, we have $\overrightarrow{XO} \cdot \overrightarrow{XCam} = r_x^2 - c_xr_x + r_y^2 - c_yr_y + r_z^2 - c_zr_z = 0$. Note that we can simplify this equation by first substituting $r_x^2 + r_y^2 + r_z^2 = R^2$, and we obtain $c_xr_x + c_yr_y + c_zr_z = R^2$.

Thus, we now have a system of 3 equations and 3 unknowns:

  • $(c_zd_y - c_yd_z)r_x + (c_xd_z - c_zd_x)r_y + (c_yd_x - c_xd_y)r_z = 0$
  • $r_x^2 + r_y^2 + r_z^2 = R^2$
  • $c_xr_x + c_yr_y + c_zr_z = R^2$

Now you can just pick your favorite way to solve it. This will give you the coordinates of $X$, and you will in turn have $\overrightarrow{Hor} = \overrightarrow{CamX}$. I haven't done the actual substitution yet as it's a huge mess, but I think this answers your question. I'm not exactly sure what you mean by the shortest calculation possible, but I think this method is straightforward and gives you a general expression for the coordinates of $X$ in terms of the parameters you provided.

Hope this helped.

  • Thanks a lot for your response. I tried to solve that system but after a bit of frustration I gave up on it and reformulated the problem as follows:
    1. The problem could be assimilated to an intersection of two circles, since we're working in a plane. Once circle centered on the sphere center of radius r, and a second circle centered on Cam of radius (Cam-X)
    2. The length of (Cam-X) can be determined from (Cam-O) and the radius of the sphere, taking advantage of the fact that (O-X) and (Cam-X) are perpendicular since X is a tangent.

    The resulting problem was much easier for me to solve.

    – blackrack Feb 17 '16 at 03:13
  • @blackrack No problem. Indeed, that sounds like a nice way to do it. Was it really easier to solve? I'm envisioning solving for the intersection of two spheres and a plane (also a system of 3 equations), which to me feels like would still take a lot of work to grind through. How did you go about solving it? – Brian Yao Feb 17 '16 at 04:48
  • I actually solved for the intersection of 2 circles and used existing circle intersection formulas instead of intersection of 2 spheres and a plane, I'll describe and illustrate my idea shortly. – blackrack Feb 17 '16 at 14:33
0

Create a local coordinate system on the plane with the center of the circle as origin, and local x direction along $\vec{OC_{am}}$. The 3×3 rotation matrix is $$ \begin{align} E = & \begin{vmatrix} \hat{i} & \hat{j} & \hat{k} \end{vmatrix} \\ \hat{i} &= [ \vec{O C_{am}} ] \\ \hat{k} & = [\hat{i} \times \vec{Dir}] \\ \hat{j} &= \hat{k} \times \hat{i} \end{align} $$

where the $[\vec{v}] = \frac{\vec{v}}{\| \vec{v} \|}$ notation is for unit vectors, and $\times$ is the vector cross product.

Now you take the distance between the center of the circle and point $O_{am}$ along the $\hat{i}$ direction as $\ell = \| \vec{O C_{am} } \|$

The $(x,y)$ coordinate of the tangent point X on the plane are

$$ (x,y) = \left( \frac{r^2}{\ell}, \frac{r \sqrt{\ell^2-r^2}}{\ell} \right) $$ where $r$ is the radius of the circle.

The 3D coordinates of X are

$$ \vec{X} = \frac{r^2}{\ell} \hat{i} + \frac{r \sqrt{\ell^2-r^2}}{\ell} \hat{j} $$

The direction of X is $$ \vec{H_{or}} = [\vec{O_{am} X}] $$

NOTE: The equation of the tangent line on the plane is $\left(\frac{r}{\ell}\right)x + \left( \frac{\sqrt{\ell^2-r^2}}{\ell} \right)y = r$.

Example

I have verified the calculation with a GeoGebra model:

geo

John Alexiou
  • 13,816