Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray - Sphere intersection test

Started by KriS-XK Mar 11, 2003 at 1:51 PM 4 replies 400+ views
Original Post
KriS-XK
KriS-XK
I want to test if a ray intersects a sphere. I found this code in this forum:
  
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;
} 
  
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.
--KriS
quant
quant
sqrt is fine..
-keyboard
quant
quant
all i know is sqrt is a valid function.. sqr isnt

maybe your right .. would have to know what hes trying to do
-keyboard
KriS-XK
KriS-XK
Thx. I just thought that sqr is sqrt .
--KriS

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.