射线投射碰撞检测缺失
我正在编写自己的 Ray Cast 碰撞检测器,时不时地,就像百万分之一(最终变得非常频繁)。由于舍入点错误,我得到一条光线,它只是设法穿过多边形两侧之间的间隙。这真的很烦人,我根本不知道如何解决这个问题。不仅如此, Box2D 的创建者似乎也有同样的问题:
注意
由于舍入误差,光线投射可能会潜入您的多边形之间的小裂缝中。 静态环境。如果这在您的申请中不可接受,请扩大您的范围 稍微多边形。
那么,对于那些以前实际这样做过的人来说,如何确保我永远不会遇到这种类型的浮点值舍入点错误?您使用了哪些解决方法,以使该问题永远不会影响您的程序。
PS我已经阅读了大量有关浮点数的内容,但我仍然不知道如何解决百万分之一的问题。一定有什么东西我错过了。
I am writing my own Ray Cast collision detector and every now and then, like one in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于浮点数的近似性质。在这种情况下,许多程序员所做的就是尝试将计算结果与所需的测试进行比较(或者在您的情况下,评估投射光线和多边形之间的距离),然后确定该差异是否小于某个 epsilon 值。这是错误的做法。
我强烈鼓励您阅读 Bruce Dawson 的文章 比较浮点数。这就是您正在寻找的解决方案。
顺便说一句,“稍微放大你的多边形”相当于使用 epsilon 值,如果你不能保证你的世界中所有多边形的大小和位置的域,那么它就不起作用。
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.