Sign in to follow this  

Ray-Capsule Intersection Test

This topic is 4676 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, I am currently trying to implement a dynamic line-segment sphere intersection test. I am looking for an explanation of ray-capsule intersection test, so far I have only been able to find oliii's very generous source code, I'd rather have an explanation and try to implement it myself though, does anyone know of any links? Thanks

Share this post


Link to post
Share on other sites
Not sure about links but I'll take a stab at explaining how I implemented it:

First of all, I'm assuming you have a line/ray defined by:
A + tB
where A is the origin of the ray, and B is the endpoint.

I intersect the line with both spheres of the caps, and the cylinder. After this, you should have 0 to 6 't' values, that is, the value of the parameter at each possible intersection.

To find the intersection of the line with a sphere, you plug the ray equation into x^2 + y^2 + z^2 = r^2. For the cylinder, take only the x and y components of the ray, and solve it for x^2 + y^2 = r^2.

For the sphere:

(A.x + t*B.x)^2 + (A.y + t*B.y)^2 + (A.z + t*B.z)^2 = r^2

(A.x^2 + 2*t*A.x*B.x + t^2*B.x^2) + (A.y^2 + 2*t*A.y*B.y + t^2*B.y^2) + (A.z^2 + 2*t*A.z*B.z + t^2*B.z^2) = r^2

A.x^2 + A.y^2 + A.z^2 + 2*t*(A.x*B.x + A.y*B.y + A.z*B.z) + t^2*(B.x^2 + B.y^2 + B.z^2) = r^2

as you can see, this is just a quadratic, so you just plug this into the quadratic equation to get 4 of the 't' values (one for each possible intersection with the sphere). Btw, here I am assuming you translate the ray such that the sphere is at the origin. This makes it quite a bit less messy to show.

For the cylinder:

(A.x + t*B.x)^2 + (A.y + t*B.y)^2 = r^2

...

A.x^2 + A.y^2 + 2*t*(A.x*B.x + A.y*B.y) + t^2*(B.x^2 + B.y^2) = r^2

Once again, this is a quadratic. This gives you your 2 other t values. However, for these t's, you also need to plug them back into the z component of the ray, and verify that it is between the caps of the cylinder. I am also assuming you translate the ray such that the cylinder is at the origin.

edit: You should check to see if all the values of t are between 0 and 1.

If all you need is to check to see if there is an intersection, then you are done, there is an intersection if you have any real value for t. If not, find the t with the smallest value (to get the one closest to the origin of the ray), and plug it back into the ray equation to find the point of intersection.

If you need it to be an oriented capsule (that is, the cylinder is not axis aligned), transforming the ray endpoints by the inverse of the transformation of the capsule would be a good way to do this.


I realize I'm not that good at explaining myself, I will try to answer any questions on this method.

Share this post


Link to post
Share on other sites
Fantastic, thanks a lot I think I follow it now, I will have to wait until I get home to try and implement it, they're a bit touchy about me programming at work! But thanks alot

Share this post


Link to post
Share on other sites
OK I do actually have a question :P,

If the cylinder is represented as as a centre point, a length, a radius and an axis, is there a fast way of finding the transformation? Or do i need to decompose the axial direction into rotations about the x and y axes to get the inverse rotation?

Share this post


Link to post
Share on other sites
I think you could find each axis of the capsule, one vector going through the two spheres 'vertically', and two other orthogonal vectors 'horizontally' through the center point, one of these should be a vector that lies in the horizontal plane. The other is just the cross product of this vector and the vertical one. You should normalize these 3 vectors. Now you have an orthonormal basis for the cylinder's coordinate system, so you can build the proper matrix from this.

Once you have that matrix, just invert it.

However, you know that the 3 vectors are orthogonal, so you can build the matrix inverted directly (by transposing the rotation 3x3, and negating the center point).

Personally I have my capsules represented as a height and radius only, and then the transformation matrix gets applied to that, so I have this part easy with having the transformation handy ;)

Share this post


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