【发布时间】:2014-08-29 06:27:15
【问题描述】:
是否有用于计算球体和线是否相交的优化方程,我知道最近的点解,但我想知道是否还有另一个。还有二次方程,但它需要大量计算,而且我不确定所有可能的早期输出。我知道两个(我认为)...
Vec3 d = lineEnd - lineStart; // the ray
Vec3 f = lineStart - sphereCenter; // center -> lineStart
float c = dot(f, f) - radius * radius;
if(c <= 0)
{
// first out
// sphere is touching the vertex
// hit !
}
float b = dot(f, d);
if(b >= 0)
{
// second out
// line ray and center -> start, are going same direction
// if the start point didn't intersect above
// then there's no way for the segment or other point to
// miss
}
float a = dot(d, d);
// ... any other optimizations
if(b*b - a*c < 0)
{
// miss
}
【问题讨论】:
-
注意,
b实际上应该是dot(f, d) * 2。 (或者,如果你真的想把 2 因素排除在外,判别检查中的2*b*b应该是4*b*b。) -
@Wyzard 很好,我现在可以删除这两个 4,但我不能吗?
标签: c++ algorithm math optimization