General Discussion > Math and Science

Line-Sphere Intersection

(1/1)

Builderboy:
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

simplethinker:

--- Quote from: 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

--- End quote ---
How are the coordinates represented (of both the sphere and line)?

Builderboy:
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

simplethinker:
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.

Builderboy:
YAY!  Thank you so much!  it works perfectly! :D

Navigation

[0] Message Index

Go to full version