# Line Sphere collision

This topic is 4467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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/

##### Share on other sites
Quote:
 Original post by Antheushttp://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htmThis 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.

##### Share on other sites
If line.direction is normalized can I just do:
float t = dot(sphere.center - line.origin, line.direction); ?
Thanks.

##### Share on other sites
Quote:
 Original post by daniel_i_lIf line.direction is normalized can I just do:float t = dot(sphere.center - line.origin, line.direction); ?
Absolutely :-)

1. 1
Rutin
19
2. 2
JoeJ
15
3. 3
4. 4
5. 5

• 23
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631699
• Total Posts
3001775
×