views:

716

answers:

4

I am trying to determine whether a line segment (i.e. between two points) intersects a sphere. I am not interested in the position of the intersection, just whether or not the segment intersects the sphere surface. Does anyone have any suggestions as to what the most efficient algorithm for this would be? (I'm wondering if there are any algorithms that are simpler than the usual ray-sphere intersection algorithms, since I'm not interested in the intersection position)

A: 

you sorta have to work that the position anyway if you want accuracy. The only way to improve speed algorithmically is to switch from ray-sphere intersection to ray-bounding-box intersection.

Or you could go deeper and try and improve sqrt and other inner function calls

http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection

CVertex
+2  A: 

I don't know what the standard way of doing it is, but if you only want to know IF it intersects, here is what I would do.

General rule ... avoid doing sqrt() or other costly operations. When possible, deal with the square of the radius.

  1. Determine if the starting point is inside the radius of the sphere. If you know that this is never the case, then skip this step. If you are inside, your ray will intersect the sphere.

From here on, your starting point is outside the sphere.

  1. Now, imagine the small box that will fit sphere. If you are outside that box, check the x-direction, y-direction and z-direction of the ray to see if it will intersect the side of the box that your ray starts at. This should be a simple sign check, or comparison against zero. If you are outside the and moving away from it, you will never intersect it.

From here on, you are in the more complicated phase. Your starting point is between the imaginary box and the sphere. You can get a simplified expression using calculus and geometry.

The gist of what you want to do is determine if the shortest distance between your ray and the sphere is less than radius of the sphere.

Let your ray be represented by (x0 + i*t, y0 + j*t, z0 + k*t), and the centre of your sphere be at (xS, yS, zS). So, we want to find t such that it would give the shortest of (xS - x0 - i*t, yS - y0 - j*t, zS - z0 - k*t).

Let x = xS - x0, y = yX - y0, z = zS - z0, D = magnitude of the vector squared

D = x^2 -2*x*i*t + (i*t)^2 + y^2 - 2*y*j*t + (j*t)^2 + z^2 - 2*z*k*t + (k*t)^2

D = (i^2 + j^2 + k^2)*t^2 - (x*i + y*j + z*k)*2*t + (x^2 + y^2 + z^2)

dD/dt = 0 = 2*t*(i^2 + j^2 + k^2) - 2*(x*i + y*j + z*k)

t = (x*i + y*j + z*k) / (i^2 + j^2 + k^2)

Plug t back into the equation for D = .... If the result is less than or equal the square of the sphere's radius, you have an intersection. If it is greater, then there is no intersection.

Sparky
+4  A: 

This page has an exact solution for this problem. Essentially, you are substituting the equation for the line into the equation for the sphere, then computes the discriminant of the resulting quadratic. The values of the discriminant indicate intersection.

plinth
Thanks, I had found this equation somewhere else without much or any explanation of what it did, nice to have a little bit explanation.
David
A: 

If you are only interested if knowing if it intersects or not then your basic algorithm will look like this...

Consider you have the vector of your ray line, A -> B.

You know that the shortest distance between this vector and the centre of the sphere occurs at the intersection of your ray vector and a vector which is at 90 degrees to this which passes through the centre of the sphere.

You hence have two vectors, the equations of which fully completely defined. You can work out the intersection point of the vectors using linear algebra, and hence the length of the line (or more efficiently the square of the length of the line) and test if this is less than the radius (or the square of the radius) of your sphere.

Cruachan