Friday, May 2, 2008

Of Intersection Tests and Floating Point Error

XNA's Ray-Sphere Intersection test returns true with a distance of zero if the Ray originates inside the sphere.

This is great for games. Very fast. Avoids all the lengthy distance calculations and just kicks out an indication of collision.

This is not good for ray-tracers. The transmitted ray specifically looks at what happens when a ray travels through a translucent object. The requires us to spawn a ray within a spherical boundary and find the intersection on the other side.

I've tried writing a new sphere intersection test. There are a couple that I've found that I really like, and are very slick. The problem? C#'s floating point numbers aren't accurate enough to support them. Whatever XNA's BoundingSphere is doing, it's accurate enough for these calculations, whereas I can't figure out how to make mine accurate enough.

So I'm going to try to fake it. I'm going to use the XNA sphere test for external intersections, and my own for internal calculations. Once I know that I'm inside from the other one, hopefully I'll be accurate enough on my own.

Using XNA's vector class may have been a mistake here. Probably should've rolled our own using double precision numbers.

No comments: