Author Topic: Line-Sphere Intersection  (Read 12420 times)

0 Members and 1 Guest are viewing this topic.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Line-Sphere Intersection
« 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

Offline simplethinker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 695
  • Rating: +16/-5
  • snjwffl
    • View Profile
Re: Line-Sphere Intersection
« Reply #1 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)?
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Professor Robert Silensky



Chip's Challenge: ħ%

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Line-Sphere Intersection
« Reply #2 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

Offline simplethinker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 695
  • Rating: +16/-5
  • snjwffl
    • View Profile
Re: Line-Sphere Intersection
« Reply #3 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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Professor Robert Silensky



Chip's Challenge: ħ%

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Line-Sphere Intersection
« Reply #4 on: June 20, 2009, 08:37:27 pm »
YAY!  Thank you so much!  it works perfectly! :D