2012-06-21 40 views
0

我用我的碰撞检测代码的代码是这样的:“是三角触摸”式的代码不给正确的结果

(注:Vector3f是LWJGL库的一部分)

(注2:三是一类由三个LWJGL的Vector3fs的。V1,V2和V3。)

public Vector<Tri> getTrisTouching(Vector3f pos, float radius){ 
     Vector<Tri> tempVec = new Vector<Tri>(); 
     for(int i = 0; i < tris.size(); i++){ 
      Tri t = tris.get(i); 

      Vector3f one_to_point = new Vector3f(0,0,0); 
      Vector3f.sub(pos,t.v1,one_to_point);   //Storing vector A->P 

      Vector3f one_to_two = new Vector3f(0,0,0); 
      Vector3f.sub(t.v2,t.v1, one_to_two);   //Storing vector A->B 

      Vector3f one_to_three = new Vector3f(0,0,0); 
      Vector3f.sub(t.v3, t.v1, one_to_three);   //Storing vector A->C 

      float q1 = Vector3f.dot(one_to_point, one_to_two)/one_to_two.lengthSquared();   // The normalized "distance" from a to 
      float q2 = Vector3f.dot(one_to_point, one_to_three)/one_to_three.lengthSquared();   // The normalized "distance" from a to 



      if (q1 > 0 && q2 > 0 && q1 + q2 < 1){ 
       tempVec.add(t); 
      } 
     } 

     return tempVec; 
    } 

我的问题是我怎么正确地看到,如果在空间中的点被触碰我的一个三角形?

回答

1

要测试您的点是否在三角形内,请在测试点创建一条射线并将其扩展到无穷远。一个简单的例子是一条水平线(例如y常数,x增加到无穷大),然后计算它与一个多边形边相交的次数。零或偶数个交点意味着你在三角形之外。它的好处不仅适用于三角形,而且适用于任何多边形。

http://erich.realtimerendering.com/ptinpoly/

0

我可以帮助你的唯一方法就是向你提供这个链接。 不幸的是,我不是很擅长LWJGL Geometry所以你现在就是。 - Vector3f

希望它有帮助!如果确实如此,请勾选答案接受。