Ray Cast Collision Detection Missing
I am writing my own Ray Cast collision detector and every now and then, like one i开发者_如何转开发n a million times (which ends up being quite frequent). I get a ray that just manages to slip through the gaps between two sides of a polygon due to rounding point errors. It is really annoying and I do not know how to solve this problem at all. Not only that but it seems that the creators of Box2D have the same problem:
Caution
Due to round-off errors, ray casts can sneak through small cracks between polygons in your static environment. If this is not acceptable in your application, please enlarge your polygons slightly.
So, to those that have actually done this before, how do I make sure that I never suffer this type of rounding point error with floating point values? What workarounds have you used so that this problem does not affect your program ever.
P.S. I have read a large amount about floating point numbers and I still cannot figure out how to make the one in a million problem go away. There must be something I am missing.
This is due to the approximate nature of floating-point numbers. What many programmers do in this situation is to try to compare their computed result against their desired test (or in your case, evaluating the distance between the cast ray and the polygon), and then determinig if this difference is less than some epsilon value. THIS IS THE WRONG APPROACH.
I strongly encourage you to read Bruce Dawson's article on Comparing Floating Point Numbers. This is the solution you're looking for.
By the way, "enlarging your polygons slightly" is equivalent to using an epsilon value, and won't work if you can't guarantee the domain of the sizes and positions of all polygons in your world.
精彩评论