article

Calculations for a ray–sphere intersection using algebra and the quadratic equation. One application of this is the calculation of ray intersections in ray tracing.

Equation for a sphere

(x-s_x)^2+(y-s_y)^2+(z-s_z)^2=s_r^2
  • s_? - center point and radius

Equation for a line starting at (0,0,0)

x=d*l_x
y=d*l_y
z=d*l_z
  • d - distance along line from starting point
  • l_? - direction of line

Solving for d:

  1. Equations combined: (d*l_x-s_x)^2+(d*l_y-s_y)^2+(d*l_z-s_z)^2=s_r^2
  2. Expanded: d^2l_x^2-2dl_xs_x+s_x^2+d^2l_y^2-2dl_ys_y+s_y^2+d^2l_z^2-2dl_zs_z+s_z^2=s_r^2
  3. Factored: d^2(l_x^2+l_y^2+l_z^2)+d(-2l_xs_x-2l_ys_y-2l_zs_z)+s_x^2+s_y^2+s_z^2-s_r^2=0
  4. Put into quadratic formula and simplified: d=\frac{l_xs_x+l_ys_y+l_zs_z \pm \sqrt {(l_xs_x+l_ys_y+l_zs_z)^2-(l_x^2+l_y^2+l_z^2)(s_x^2+s_y^2+s_z^2-s_r^2)}}{l_x^2+l_y^2+l_z^2}

If only the fact of if the ray and sphere intersect is needed:

(l_xs_x+l_ys_y+l_zs_z)^2 \ge (l_x^2+l_y^2+l_z^2)(s_x^2+s_y^2+s_z^2-s_r^2)

The above is true if the line and sphere intersect.

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Line–sphere intersection".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld