代码之家  ›  专栏  ›  技术社区  ›  Martin

三维点三角形碰撞检测

  •  4
  • Martin  · 技术社区  · 17 年前

    如何在以下物理模拟中更正浮点错误:

    • 原点(x,y,z)
    • 施力后所需的点(x',y',z’)。
    • 两个三角形(A、B、C)和(B、C、D),共享边BC

    我使用此方法进行碰撞检测:

    For each Triangle
        If the original point is in front of the current triangle, and the desired point is behind the desired triangle:
            Calculate the intersection point of the ray (original-desired) and the plane (triangle's normal).
            If the intersection point is inside the triangle edges (!)
                Respond to the collision.
            End If
        End If
    Next Triangle
    

    我遇到的问题是,有时点落入浮点数学的灰色区域,在那里它非常接近线bc,以至于它不能与任何一个三角形碰撞,即使从技术上讲,它应该总是与一个或另一个三角形碰撞,因为它们共享一个边。当发生这种情况时,点正好在两个边共享三角形之间通过。我在代码的一行上 (!) 因为我相信这就是我应该改变的地方。

    在非常有限的情况下工作的一个想法是跳过边缘测试。有效地将三角形转化为平面。这只在我的网格是凸面外壳时有效,但我计划创建凸面形状。

    我专门使用点积和三角形法线来进行所有的前-后测试。

    5 回复  |  直到 17 年前
        2
  •  2
  •   Statement    17 年前

        3
  •  1
  •   shoosh    17 年前

        4
  •  0
  •   Martin    17 年前

        5
  •  0
  •   user4891    17 年前

    double Distance(double x0, double y0, double x1, double y1)
    {
      double a, b, dx, dy;
    
      dx = abs(x1 - x0);
      dy = abs(y1 - y0);
    
      a = max(dx, dy));
      if (a == 0)
        return 0;
      b = min(dx, dy);
    
      return a * sqrt( 1 + (b*b) / (a*a) );
    }
    

    推荐文章