盒子到球体的碰撞 [英] Box to Sphere Collision
问题描述
我需要检查一个盒子是否与一个球体相撞.我有一个用 x、y、z、宽度、高度、深度定义的 BoundingBox 类.我还有一个用 x、y、z、半径定义的 BoundingSphere 类.如何检查它们是否相交?
I need to check if a box is colliding with a sphere. I have a BoundingBox class defined with x, y, z, width, height, depth. I also have a BoundingSphere class defined with x, y, z, radius. How to I check to see if they intersect?
推荐答案
首先要检查的是 BoundingSphere 的 BoundingBox 是否相交.这样做的原因是,这是一种非常简单的方法,可以排除所涉及的更复杂的数学.
The first thing to check is if the BoundingBox for the BoundingSphere intersects. The reason for this is that it's a very simple way to rule out the more complicated math involved.
下一步是获取边界框的六个平面(或十二个三角形)中的每一个,并对它们进行点到多边形到球心的距离测试.如果其中之一小于球体的半径,那么您就命中了.
The next step would be to take each of the six planes (or twelve triangles) of the bounding box and do a distance from point to polygon test on them to the center of the sphere. If one of them is less than the radius of the sphere, then you have a hit.
多边形到点距离的 Matlab 代码:http://www.mathworks.com/matlabcentral/fileexchange/12744-distance-from-a-point-to-polygon
Matlab code for polygon-to-point-distance: http://www.mathworks.com/matlabcentral/fileexchange/12744-distance-from-a-point-to-polygon
这篇关于盒子到球体的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!