Line Sphere collision

Started by
4 comments, last by xissburg 18 years ago
Whats a fast way (for the computer) to find the closest distance between a sphere and a line that continues infinatly in direction V from point P?
"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
Advertisement
http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm

This is also a comprehensive general x vs. y intersection resource:
http://www.realtimerendering.com/int/
Quote:Original post by Antheus
http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm

This is also a comprehensive general x vs. y intersection resource:
http://www.realtimerendering.com/int/
These actually solve a different problem than what the OP asked about. I'll try to sketch out the correct algorithm here (but may or may not get it right):
float t = dot(sphere.center - line.origin, line.direction) / (dot(line.direction, line.direction);vector3 closest = line.origin + t * line.direction;float dist_squared = length_squared(sphere.center - closest);if (dist_squared <= sphere.radius * sphere.radius) {    // Line intersects sphere} else {    float distance = sqrt(dist_squared) - sphere.radius;}
If you want to test a ray or segment rather than a line, just clamp t as appropriate.
If line.direction is normalized can I just do:
float t = dot(sphere.center - line.origin, line.direction); ?
Thanks.
"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
Quote:Original post by daniel_i_l
If line.direction is normalized can I just do:
float t = dot(sphere.center - line.origin, line.direction); ?
Absolutely :-)
another option: About Lines and
Distance of a Point to a Line (2D & 3D)
.

This topic is closed to new replies.

Advertisement