Sign in to follow this  
daniel_i_l

Line Sphere collision

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this