Omnimaga

General Discussion => Other Discussions => Math and Science => Topic started by: Builderboy on June 20, 2009, 07:48:03 pm

Title: Line-Sphere Intersection
Post by: Builderboy on June 20, 2009, 07:48:03 pm
I am stuck.  really really stuck, and for some reason Google will not yield any useful results.  Does anybody know the formula for detecting a collision between a line and a sphere in 3D space?

Thankz
Title: Re: Line-Sphere Intersection
Post by: simplethinker on June 20, 2009, 07:51:32 pm
I am stuck.  really really stuck, and for some reason Google will not yield any useful results.  Does anybody know the formula for detecting a collision between a line and a sphere in 3D space?

Thankz
How are the coordinates represented (of both the sphere and line)?
Title: Re: Line-Sphere Intersection
Post by: Builderboy on June 20, 2009, 07:55:18 pm
The line is represented as a unit vector (a,b,c) starting at (0,0,0) and the Sphere is represented as having a center at (x,y,z) and a radius of r
Title: Re: Line-Sphere Intersection
Post by: simplethinker on June 20, 2009, 08:11:16 pm
A sphere of radius R, centered at (x0, y0, z0) can be represented by  (x - x0)^2 + (y - y0)^2 + (z - z0)^2 = R^2.  Also, any point on the line pointing in the direction of (a, b, c) can be represented by  x = ka, y = kb, z = kc.

Plug those in for x, y, z and solve for "k".  When I solved for "k", I got  k = (G +-  √(G² - F*H) )/F  where  F = a² + b² + c²,  G = a*x0 + b*y0 + c*z0,  H = x0² + y0² + z0² - R².  Plug those two values of "k" in the equations of the line and you'll get the two points (x, y, z) where the line intersects the sphere.
Title: Re: Line-Sphere Intersection
Post by: Builderboy on June 20, 2009, 08:37:27 pm
YAY!  Thank you so much!  it works perfectly! :D