小编典典

3D线段和平面相交

algorithm

我正在尝试实现线段和平面相交测试,该测试将根据其是否与平面相交而返回true或false。它还会返回直线相交的平面上的接触点,如果直线不相交,如果直线是射线,函数仍应返回相交点。我使用了克里斯特·埃里克森(Christer
Ericson)的实时碰撞检测中的信息和代码,但我认为无法正确实现它。

im使用的平面是从三角形的法线和顶点导出的。我想要在平面上找到相交的位置,而不管它是否位于我用来得出平面的三角形上。

在此处输入图片说明

该函数的参数如下:

contact = the contact point on the plane, this is what i want calculated
ray = B - A, simply the line from A to B
rayOrigin = A, the origin of the line segement
normal = normal of the plane (normal of a triangle)
coord = a point on the plane (vertice of a triangle)

这是即时通讯使用的代码:

bool linePlaneIntersection(Vector& contact, Vector ray, Vector rayOrigin, Vector normal, Vector coord) {

    // calculate plane
    float d = Dot(normal, coord);

    if (Dot(normal, ray)) {
        return false; // avoid divide by zero
    }

    // Compute the t value for the directed line ray intersecting the plane
    float t = (d - Dot(normal, rayOrigin)) / Dot(normal, ray);

    // scale the ray by t
    Vector newRay = ray * t;

    // calc contact point
    contact = rayOrigin + newRay;

    if (t >= 0.0f && t <= 1.0f) {
        return true; // line intersects plane
    }
    return false; // line does not
}

在我的测试中,它永远不会返回true …任何想法?


阅读 533

收藏
2020-07-28

共1个答案

小编典典

我对此可能是错的,但是代码中有些地方似乎非常可疑。首先,请考虑以下这一行:

// calculate plane
float d = Dot(normal, coord);

在这里,您的值d对应于平面法线(向量)与空间中的点(平面上的点)之间的点积。这似乎是错误的。特别是,如果您有任何平面穿过原点,并将原点用作坐标点,则最终将进行计算

d = Dot(normal, (0, 0, 0)) = 0

并立即返回假。我不确定您打算在这里做什么,但是我很确定这不是您的意思。

该行中似乎可疑的另一个地方是这一行:

// Compute the t value for the directed line ray intersecting the plane
float t = (d - Dot(normal, rayOrigin)) / Dot(normal, ray);

请注意,您正在计算平面法线向量(向量)和射线的原点(空间中的点)之间的点积。这似乎很奇怪,因为这意味着根据射线在空间中的起源,用于射线的缩放比例会发生变化。我建议再看一次这段代码,看看这是否真的是您的意思。

希望这可以帮助!

2020-07-28