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?
相关问题
- Is GLFW designed to use without LWJGL (in java)?
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
- Check if a number is a perfect power of another nu
- How does gl_ClipVertex work relative to gl_ClipDis
相关文章
- ceil conterpart for Math.floorDiv in Java?
- why 48 bit seed in util Random class?
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Keep constant number of visible circles in 3D anim
- Need help generating discrete random numbers from
- How do you create a formula that has diminishing r
- Math.Max vs Enumerable.Max
There's a chapter in Graphics Gems by Jim Arvo.
I guess the stale link above used to point to his code as there's "arvo" in the URL. This link works - at least right now.
If you want to keep the test at the level you described, you can place a bounding box around the sphere where width, height, and depth = 2r. Of course, this admits the risk of false positives for collisions at "non-polar" or "non-equatorial" points on the sphere. To solve that, you might consider building a series of hierarchical bounding boxes to increase the granularity of hit tests in these problems regions.
You might also approach the problem from a rendering level. Since you cannot render a sphere, some sort of polygonal mesh is commonly used. Hit tests between 2D (or 3D) polygons is a straightforward exercise.
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 code for polygon-to-point-distance: http://www.mathworks.com/matlabcentral/fileexchange/12744-distance-from-a-point-to-polygon
You just have to check all the corners of the bounding box against the distance from the center of the sphere. Here's some pseudocode: