java - 三角形-三角形相交检测

标签 java 3d computational-geometry

我正在使用 this 研究三角形检测算法文章。我编写了这段代码,但不幸的是,当三角形之间存在交集时,该方法返回 false。

private boolean checkTriangleCollision(Triangle triangle1, Triangle triangle2) {
    Vector3f n1 = getN(triangle1);
    Vector3f n2 = getN(triangle2);
    Vector3f v = null;
    v = Vector3f.cross(n1, n2, v);

    Vector3f p = new Vector3f();

    float pxy = n1.z * triangle1.vertex1.position.z * n1.x * n1.y;
    float px = n1.z * triangle1.vertex1.position.z * n1.x * n1.y * triangle1.vertex1.position.y;
    float py = n1.z * triangle1.vertex1.position.z * n1.x * triangle1.vertex1.position.x * n1.y;
    float p0 = n1.z * triangle1.vertex1.position.z * n1.x * triangle1.vertex1.position.x * n1.y * triangle1.vertex1.position.y;

    p.x = (p0/(px/pxy))/pxy;
    p.y = (p0/(py/pxy))/pxy;

    Vector3f x = null;
    x = Vector3f.add(p, v, x);

    Vector3f xq1 = null;
    xq1 = Vector3f.sub(x, triangle1.vertex1.position, xq1);
    float i1 = Vector3f.dot(xq1, n1);

    Vector3f xq2 = null;
    xq2 = Vector3f.sub(x, triangle2.vertex1.position, xq2);
    float i2 = Vector3f.dot(xq2, n2);

    if (i1 == 0 && i2 == 0) {
        return true;
    }
    return false;
}

private Vector3f getN(Triangle triangle) {
    Vector3f vn1 = null;
    vn1 = Vector3f.sub(triangle.vertex2.position, triangle.vertex1.position, vn1);
    Vector3f vn2 = null;
    vn2 = Vector3f.sub(triangle.vertex3.position, triangle.vertex1.position, vn2);
    Vector3f n = null;
    n = Vector3f.cross(vn1, vn2, n);
    return n;
}

我不完全知道为什么,但我想问题出在计算 p 上。如果是,那么我如何通过代码求解这个方程式?

N1·(P0-Q1) = 0 
and
N2·(P0-R1) = 0 

最佳答案

在我看来,您做错了点积。

试试这个:

    Vector3f n1 = getN(triangle1);
    Vector3f n2 = getN(triangle2);
    Vector3f v = null;
    v = Vector3f.cross(n1, n2, v);

    Vector3f p = new Vector3f();

//P0 calcuiation

    float C1 = n1.x * triangle1.vertex1.position.x + n1.y * triangle1.vertex1.position.y + n1.z * triangle1.vertex1.position.z;

    float C2 = n2.x * triangle2.vertex1.position.x + n2.y * triangle2.vertex1.position.y + n2.z * triangle2.vertex1.position.z;

    float Kn = n2.x / n1.x;

    p.y = (C2 - C1 * Kn) / (n2.y - n1.y * Kn);

    p.x = (C1 - n1.y * p.y) / n2.x;

p.z = 0;

//P0 calcuiation ends

    Vector3f x = null;
    x = Vector3f.add(p, v, x);

这是我的计算: http://i.stack.imgur.com/3USpZ.png

关于java - 三角形-三角形相交检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14329305/

相关文章:

javascript - 3D CSS 立方体 - 奇怪的旋转不一致(已解决 : Gimbal Lock)

在 2n+3 个点中找到一个圆的算法,它包含内部 n 个点、外部 n 个点和自身 3 个点

algorithm - 在 Delaunay 三角剖分中重新定位点

java - 在 Java 8 中使用列表值 Map<Key, List<Value>> 将 Map 反转为 Map <Value, Key>

java - 为什么 Java Web Start 不适用于 64 位 Java 环境?

javascript - 如何在 p5.js 中导入模型?

c++ - WGS84中2个 “linearly”运动对象之间的碰撞检测

java - 在 Android 中使用图像检测裁剪人脸

java - 如何将 Project Explorer View 添加到 Eclipse RCP 应用程序

computational-geometry - 多边形三角剖分反射顶点