# ellipsoid intersections

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

## Recommended Posts

Hey people
so i was browsing the web to find a "tested" ray-ellipsoid intersection test, and i did, but i really didn't get the math behind it.
here it is:
 bool IT_RayEllipsoid(D3DXVECTOR3 RayPos, D3DXVECTOR3 RayDir, D3DXVECTOR3 EllPos, D3DXVECTOR3 EllRadius, D3DXVECTOR3* DistToEll) { D3DXVECTOR3 m = RayPos - EllPos; D3DXVec3Normalize(&RayDir, &RayDir); float a = ((RayDir.x * RayDir.x) / (EllRadius.x * EllRadius.x)) + ((RayDir.y * RayDir.y) / (EllRadius.y * EllRadius.y)) + ((RayDir.z * RayDir.z) / (EllRadius.z * EllRadius.z)); float b = ((2.0f * m.x * RayDir.x) / (EllRadius.x * EllRadius.x)) + ((2.0f * m.y * RayDir.y) / (EllRadius.y * EllRadius.y)) + ((2.0f * m.z * RayDir.z) / (EllRadius.z * EllRadius.z)); float c = ((m.x * m.x) / (EllRadius.x * EllRadius.x)) + ((m.y * m.y) / (EllRadius.y * EllRadius.y)) + ((m.z * m.z) / (EllRadius.z * EllRadius.z)) - 1.0f; float d = ((b * b) - (4.0f * a * c)); if (d < 0) { return false; } else { d = sqrt(d); } float hit = (-b + d) / (2.0f * a); float hitsecond = (-b - d) / (2.0f * a); float t; if (hit < hitsecond) t = hit; else t = hitsecond; D3DXVECTOR3 Q = RayPos + t*RayDir; *DistToEll = Q - RayPos; return true; 

i get the part about solving the quadratic equation, i just couldn't figure out how did a,b,c come in to place. What is the quadratic equation behind it??
last i checked, the ray-sphere equation is:
(p+td-C)(p+td-C) = r2
p = ray's position
d = normalized direction vector for the ray
C = sphere's center
and then you just expand it, and solve for the two solutions(or no solution if it didn't intersect)

P.S: i tested it and it is working perfectly, the question is how???

##### Share on other sites
If you have a ray defined by , and the ellipse has an equation of , then by substituting the 3 line equations into the ellipse equation you should get a quadratic equation in t that has the same coefficients as in the code sample you posted.

##### Share on other sites
damn, that was easy.. didn't even think about that
thanks

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 25