A detailed description of each step; hope this makes things crystal clear. The equation for a three-dimensional sphere:
(xa)^2 + (yb)^2 + (zc)^2 = r^2
with <a, b, c> being the center of the sphere, and r its radius. The point <x, y, z> is on the sphere if it satisfies this equation.
Parametric equations for the beam:
X = xo + xd*tY = yo + yd*tZ = zo + zd*t
where <xo, yo, zo> is the beam source, and <xd,yd,yd> is the direction of the camera beam.
To find the intersection, we want to see which points on the ray coincide with the points on the sphere. Therefore, we substitute the equation of the ray in the equation of the sphere:
(xo + xd*t - a)^2 + (yo + yd*t - b)^2 + (zo + zd*t - c)^2 = r^2
which expands to:
(xd^2 + yd^2 + zd^2) * t^2 + [2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]] * t + [(xo - a)^2 + (yo - b)^2 + (zo - c^)2 - r^2] * 1 = 0
Note that this is a quadratic equation in the form At^2 + Bt + C = 0 , where:
A = (xd^2 + yd^2 + zd^2)B = [2[xd * (xo - a) + yd * (yo - b) + zd *(zo - c)]]C = [(xo - a)^2 + (yo - b)^2 + (zo - c^)2 - r^2]
We can apply the general quadratic formula for an unknown variable:
t = [-B +- sqrt(B^2 - 4AC)] / 2A
Part B^2 - 4AC is called the "discriminant." Depending on the value of the discriminant, we obtain zero, one, or two solutions to this equation:
If it is less than zero, the solution is an imaginary number, and the ray and sphere do not intersect in the real plane.
If it is zero, then the ray intersects the sphere at exactly 1 point (it exactly touches the sphere).
If it is greater than zero, the ray crosses the sphere at exactly 2 points.
If the discriminant indicates that there is no solution, then everything is ready! The ray does not cross the sphere. If the discriminant indicates at least one solution, you can decide for t determine the intersection point. These two solutions:
t_1 = [-B + sqrt(B^2 - 4AC)] / 2A t_2 = [-B - sqrt(B^2 - 4AC)] / 2A
A smaller solution is the point at which the ray first enters the sphere.