cylinder ray intersection test?

Started by
4 comments, last by Eelco 19 years, 6 months ago
Anyone know how to do a cylinder ray intersection test? i can do the end cap on the two planes formed at the end of the cylinder (simple plane intersection test then distance <= radius of cylinder easy peasy) the problem is finding if the ray intersects the cylinder. I need to be able to check if the ray formed from points A and B intersect the cylinder formed from C and D with a radius of r. worse case i need a way to check if the ray intersects the infinite cylinder formed along the lne formed by C and D with a radius of r then i could figure out how to do the check to see if it is along the segment would preffer to have one that solves without the check along segment sort of a faster set up i hope but not possitive hehe.
Advertisement
Just test for intersection with the vertical cylinder where
x^2 + z^2 = 1.

If you want a different cylinder, you can then use a transformation matrix on the ray. This matrix would be the inverse of the cylinder's transformation matrix.

Then, once you've found the intersection point, multiply this vector by the transformation matrix of the cylinder to get the real co-ordinate.

It's probably not the fastest method, but it definately works (unless you choose a transformation for the cylinder with no inverse matrix!)
David Eberly's excellent Magic Software site has lots of interesting stuff for this kind of thing (see his books for more stuff):
http://www.magic-software.com/Intersection.html

Another excellent resource is the site from Möller & Haines' "Realtime rendering" book, of particular interest will be their 3D object intersections page: http://www.realtimerendering.com/int/


And yep, I agree with MrEvil, transforming the ray into the space of the cylinder so the cylinder becomes axis aligned can be a good idea (depending on what you know about how your application uses the rays and cylinders).

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

I have a much much more simpler method - which strangley enough I just worked out a minute ago as I need it. It requires zero transformation and little matrix knowledge.

Suppose the ray(bullet/rocket) is at point Pr=(4,8,2)
The cylinder starts at point Pc=(5,9,-4) and points in the direction (or is travelling in the direction) Vc=(5,3,-4).
Let Vcu be the unit length vector of Vc = (x, y, z)=
=(5,3,-4)/Length (where Length=sqrt(25+9+16)).
assume the cylinder is of length 20 feet and has radius 3 feet.

A point anywhere on the centre line of the cylinder is:
P = (5,9,-4) +kVcu = (5+k*x, 9+k*y, -4+k*z)
k can be any value from 0 up to Length 20 feet - the smaller the increments the more accurate the test.

The distance from the ray to the cylinder is:
P-Pr = (1+k*x, 1+k*y, -6+k*z)

To test if the ray intersects with the cylinder:

Simply loop from k =0 to k=length of the cylidner (the smaller the icrements the more accurate it is)

CRASH=FALSE;
for (int k=0; k<LENGTH; k++) //LENGTH=20
{
VectorPPr=(1+k*x, 1+k*y, -6+k*z);
if (VectorPPr.Length() <= RADIUS) //RADIUS =3
{
//CRASH !!!
CRASH=TRUE;
BREAK;
}
}




For more accuracy incremnt k by say 0.5 or smaller each time.

cheers

Ade
Quote:=(5,3,-4)/Length (where Length=sqrt(25+9+16)).


That would be (0, 6, 0)/Length (where Length = sqrt(0+6*6+0)

And although this method is simpler, it requires infinately more processing (because of the iteration), whereas the inversematrix*ray -> calculate intersection -> matrix*intersection) is constant time.
yeah transforming the ray is definitly the simplest way. it also automaticly gives you eliptical cylinders (elinders? :P)

its also slighty faster than doing it otherwise, but only a little.

This topic is closed to new replies.

Advertisement