Original Post
I want to test if a ray intersects a sphere. I found this code in this forum:
But it always returns me true value. I''m sure that values are passed to this function corectly as another similar function for testing tri - ray intersection works fine. So where is mistake? Or maybe you could post another code snippet.
bool IntersectRaySphere (D3DXVECTOR3& origin, D3DXVECTOR3& dir, D3DXVECTOR3& SphereCenter, float SphereRadius)
{
D3DXVECTOR3 q;
D3DXVec3Subtract(&q, &SphereCenter, &origin);
float c = D3DXVec3Length(&q);
float v = D3DXVec3Dot(&q, &dir);
float d = sqrt (SphereRadius) - (sqrt (c) - sqrt (v));
if (d < 0.0f)
return false;
return true;
}