Archived

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

Ray to Cylinder collision algorithm -- will this work?

This topic is 5733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I found an algorithm online that looked pretty easy to understand so I coded it up and got decent results but not exactly what I was expecting. I am trying to make a "railgun" like weapon that shoots a ray out from the camera''s position along the camera''s direction to infinity. The ray''s starting point is the camera''s position and the ending point is the camera''s direction vector scaled out a sufficiently far distance (infinity). The algorithm is as follows: const bool Util::RaySphereTest( D3DXVECTOR3* vPofI, const D3DXVECTOR3* vSpherePos, const float fRadius, const D3DXVECTOR3* vRayStartPos, const D3DXVECTOR3* vRayEndPos ) { // Ray''s direction D3DXVECTOR3 vRayNormal = *vRayEndPos - *vRayStartPos; Normalize( &vRayNormal, &vRayNormal ); // Get distance of a vector from ray origin to sphere''s center pos projected on the ray D3DXVECTOR3 vRaySphere = *vSpherePos - *vRayStartPos; float dist = DotProd( &vRaySphere, &vRayNormal ); // Find distance from point of intersection to vector perpendicular to ray and goes through sphere''s // center (that''s the idea anyway) float disc = (fRadius * fRadius) - (DotProd( &vRaySphere, &vRaySphere ) - (dist * dist) ); if( disc < 0.0f ) return false; // No intersection // They intersect if( vPofI ) { // Find point of intersection float d = (float)sqrt( disc ); *vPofI = *vRayStartPos + ((dist - d) * vRayNormal); } return true; } Did I make an error in the algorithm somewhere? If not, then I am probably messing something up somewhere.

Share on other sites
What results are you obtaining that suggest that you have made an error?

As far as I can tell from your code the ray-sphere intersection is being performed correctly. However, I suspect that your attempt to find the first point of intersection is wrong. The correct distance to subtract from the length of the ray is in fact
sqrt((fRadius-d)2+fRadius2)

I hope this helps.

Cheers,

Timkin

Share on other sites
Do you mean the following?

*vPofI = *vRayStartPos + ((dist - d) * vRayNormal);

The problem I am having is that I aim at an object and the collision doesn''t happen as I would expect it to. It''s as if I''m using an incorrect ray. This is my first time doing a FPS style game so I''m not sure if the camera''s position is the correct starting point for my ray. Do I need to offset it a little?

Share on other sites
I think my problem might be fixed. I forgot to add the camera''s position to the scaled direction for the ray''s endpoint. It seems to work pretty well now but I''m still wondering about the point of intersection. I didn''t understand what you said about the distance.

Share on other sites
quote:
Original post by beoch
I didn''t understand what you said about the distance.

My apologies. I misread your code (I got some brackets mixed up). Your code is correct (barring the omission of the camera position).

Cheers,

Timkin

Ah, right on.

Thanks.

• 10
• 40
• 15
• 10
• 23