立方体球面相交测试? [英] Cube sphere intersection test?

查看:350
本文介绍了立方体球面相交测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最简单的方法是什么?我在数学上失败了,我发现在互联网上的复杂的公式...我希望如果这些更简单一些?

What's the easiest way of doing this? I fail at math, and i found pretty complicate formulaes over the internet... im hoping if theres some simpler one?

我只需要知道一个球是否重叠一个立方体,我不关心它的哪个点等等。

I just need to know if a sphere is overlapping a cube, i dont care about which point it does that etc.

我也希望它会利用这两个形状是对称的事实。

I'm also hoping it would take advantage of the fact that both shapes are symmetric.

编辑:立方体在x,y,z轴上直线对齐

the cube is aligned straight in the x,y,z axises

推荐答案

看半空间是不够的,你还必须考虑最接近的方法:

Looking at half-spaces is not enough, you have to consider also the point of closest approach:

借用亚当的符号:

假设轴对齐的立方体,让C1和C2是对角,S是球体的中心,R是球体的半径,并且两个对象都是固定的:

Assuming an axis-aligned cube and letting C1 and C2 be opposing corners, S the center of the sphere, and R the radius of the sphere, and that both objects are solid:

inline float squared(float v) { return v * v; }
bool doesCubeIntersectSphere(vec3 C1, vec3 C2, vec3 S, float R)
{
    float dist_squared = R * R;
    /* assume C1 and C2 are element-wise sorted, if not, do that now */
    if (S.X < C1.X) dist_squared -= squared(S.X - C1.X);
    else if (S.X > C2.X) dist_squared -= squared(S.X - C2.X);
    if (S.Y < C1.Y) dist_squared -= squared(S.Y - C1.Y);
    else if (S.Y > C2.Y) dist_squared -= squared(S.Y - C2.Y);
    if (S.Z < C1.Z) dist_squared -= squared(S.Z - C1.Z);
    else if (S.Z > C2.Z) dist_squared -= squared(S.Z - C2.Z);
    return dist_squared > 0;
}

这篇关于立方体球面相交测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆