Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!