Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Simple Ray/Sphere Collision

This topic is 5263 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

Hi there, I''ve been searching around this forum and while finding a lot of information on the general subject, I haven''t really found exactly what I''m looking for (or, at least, haven''t been able to extract it from what I''ve read ) Okay, here''s what I''m trying to do: I have a large sphere and two vectors (one player location and remote location). What I''m looking to do is test between the two locations to see if the line intersects the sphere. I don''t care where it intersects (if it does), I just want to know true or false if it did. Oh, one other little detail (don''t know if this matters), the remote point is really just needed for direction, I don''t need to know if it specifically intersects BETWEEN the two vectors, just if a line in the direction of the remote vector intersects the sphere. If someone could provide me with just a plain language list of what operations are needed for this, I''d be greatly appreciative. Thanks!

Share this post

Link to post
Share on other sites
in that case, you just want to know if an intersection occurs, between a segment and a sphere. In that case, it''s quite simple.

1) Find the closest point in the segment to the sphere centre.
2) check if that point is inside the sphere.

step one, you project the sphere centre on the infinite line going through the two vertices. That gives you a point. if the point lies before or after the segment defined by the vertices, clamp the point to one of the extremities of the segment.

2) it''s a simple point-in-sphere test.

to project the point in on the line, it involves two dot products. If you don;t know what is a cross product, look up on the net, or head towards the article and resource section and look for vector maths primer.

anyway, the pseudo code would look like this

// Segment (A, B)

// Point P

// return : Point Q, the cloesest point on [AB] from P

Vector ClosestPoint(Vector A, Vector B, Vector P)
Vector AB = B - A;
float ab2 = AB.Dot(AB);
Vector AP = P - A;
float ap_dot_ab = AP.Dot(AB);
float t = ap_dot_ab / ab2; // the projection parameter on the line

// clamp parameter to segment extremities

if (t < 0.0f) t = 0.0f; else if (t > 1.0f) t = 1.0f;

// calculate the closest point

Vector Q = A + AB * t;

return Q;

then you check if Q is in the sphere (P, r)

bool PointInSphere(Vector P, float r, Vector Q)
Vector PQ = Q - P;
float pq2 = PQ.Dot(PQ);
float r2 = r * r;

if(pq2 > r2) return false; else return true;

if Q is in the sphere, there is an intersection. else the sphere do not intersect the segment [AB].

Share this post

Link to post
Share on other sites

  • 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!