Original Post
ok people. I have been searching about this thing for a while and i found many good tests but most of them i didn't understand.
From what i heard, a Ray-Ellipsoid Intersection Test in exactly like a Ray-Sphere Intersection Test but with converting the ray to ellipsoid space, meaning that you multiply it by a 3x3 matrix that represents the radii of the 3 axises of the ellipsoid.
I just want to know if there is something i missed
here is the quick code
The code is originally a Ray-Sphere intersection test taken from "Real-Time Collisions" by Christer Erikson. I just added the matrix thing to change it to Ellipsoid space.
From what i heard, a Ray-Ellipsoid Intersection Test in exactly like a Ray-Sphere Intersection Test but with converting the ray to ellipsoid space, meaning that you multiply it by a 3x3 matrix that represents the radii of the 3 axises of the ellipsoid.
I just want to know if there is something i missed
here is the quick code
float IT_RayEllipsoid(D3DXVECTOR3 RayOrigin, D3DXVECTOR3 RayVec,
D3DXVECTOR3 EllRadius, D3DXVECTOR3 EllAxis[])
{
D3DXMATRIX ToEllipsoid;
D3DXVECTOR3 xAxis = EllAxis[0] * EllRadius.x;
D3DXVECTOR3 yAxis = EllAxis[1] * EllRadius.y;
D3DXVECTOR3 zAxis = EllAxis[2] * EllRadius.z;
ToEllipsoid(0,0) = xAxis.x;
ToEllipsoid(1,0) = xAxis.y;
ToEllipsoid(2,0) = xAxis.z;
ToEllipsoid(0,1) = yAxis.x;
ToEllipsoid(1,1) = yAxis.y;
ToEllipsoid(2,1) = yAxis.z;
ToEllipsoid(0,2) = zAxis.x;
ToEllipsoid(1,2) = zAxis.y;
ToEllipsoid(2,2) = zAxis.z;
D3DXVec3TransformCoord(&RayOrigin, &RayOrigin, &ToEllipsoid);
D3DXVec3TransformNormal(&RayVec, &RayVec, &ToEllipsoid);
//Since it is in ellipsoid space, the center = 1.0f and the radius is 0,0,0
float Radius = 1.0f;
D3DXVECTOR3 Center = ZEROVEC;
D3DXVECTOR3 m = RayOrigin - Center;
float b = D3DXVec3Dot(&m, &RayVec);
float c = D3DXVec3Dot(&m, &m) - Radius * Radius;
// Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0)
if (c > 0.0f && b > 0.0f) return -1.0f;
float discr = b*b - c;
// A negative discriminant corresponds to ray missing sphere
if (discr < 0.0f) return -1.0f;
// Ray now found to intersect sphere, compute smallest t value of intersection
float t = -b - sqrt(discr);
// If t is negative, ray started inside sphere so clamp t to zero
if (t < 0.0f) t = 0.0f;
return RayOrigin + t*RayVec;
}
The code is originally a Ray-Sphere intersection test taken from "Real-Time Collisions" by Christer Erikson. I just added the matrix thing to change it to Ellipsoid space.