Advertisement

Ray to cylinder collsion test

Started by January 12, 2003 11:24 AM
1 comment, last by Lord Maz 22 years, 1 month ago
I''ve got big problems with my cyl to ray test, I found this text that described how to do it:
quote:
** Cylinder vs ray/line segment ** Graphics Gems IV had an algorithm for this. Basically you find a vector that is perpendicular to both the axis and the ray. Then you take a vector from the endpoint of the cylinder to a point on the ray. You then project this vector onto your perpendicular vector, and compare that length against the radius. ...
So I have a line (vLine), a startpoint for the cylinder (vCylBase) and a endpoint (vCylTop). To get a perpendicular vector, I took the crossproduct (think that''s the way to do it)
  
cVector vVector1 = vLine[1] - vLine[0];
cVector vVector2 = vCylTop - vCylBase;

cVector vCrossNormal = Normalize(Cross(vVector1, vVector2));
  
Then I get a vector from a point in the cylinder to a point on the ray, and project it to the perpendicular vector (using dot product, as with the crossproduct earlier I''m not sure this is right)
  
cVector vV1 = vLine[1]- vCylBase;
float t = Dot(vCrossNormal, vV1);

cVector vV2 = vCrossNormal * t;
  
If I had done this correctly, i could have tested the distance from vV2 and the cylinder.. but I get crazy results only from this.. Could someone help me correct my code? -Lord Maz-
-Lord Maz-
the


float t = Dot(vCrossNormal, vV1);


should already be the distance
Visit our homepage: www.rarebyte.de.stGA
Advertisement
Oh. Hehe, I''m making it harder than it is as usual

Well, now (after adding some more tests) it seems to be working perfectly.

Thanks for the help!

-Lord Maz-
-Lord Maz-

This topic is closed to new replies.

Advertisement