2017-02-20 91 views
0

一切都運行完美,但我找不到任何方法來實現碰撞。Java OpenGL 3D碰撞

但是,我已經制作了一個邊界框碰撞系統,我也需要一個完美的碰撞系統(基於每個頂點的碰撞)。

下面是一個使用擊中格網輪廓的真正目我在碰撞的嘗試:

String[] faceName = entity.getHitboxFaces(); //Gets all the faces from the mesh****** 
Vector3f[] verts = entity.getHitboxVertices(); //Gets all the vertices****** 

//Compiles each cube into a list****** 

for (int i = 0; i < faceName.length;) { 
    List <Vector3f> tmpVerts = new ArrayList <Vector3f>(); 

    for (int z = 0; z < 6; z++) { 
     String[] currentFace = faceName[i].split(","); 
     tmpVerts.add(verts[Integer.parseInt(currentFace[0]) - 1]); 
     tmpVerts.add(verts[Integer.parseInt(currentFace[1]) - 1]); 
     tmpVerts.add(verts[Integer.parseInt(currentFace[2]) - 1]); 
     tmpVerts.add(verts[Integer.parseInt(currentFace[3]) - 1]); 
     i++; 
    } 

    xmin = 0; 
    ymin = 0; 
    zmin = 0; 
    xmax = 0; 
    ymax = 0; 
    zmax = 0; 
    //Gets the bounding boxes of each cube****** 
    for (Vector3f v: tmpVerts) { 
     if (v.x < xmin) { 
      xmin = v.x; 
     } 
     if (v.y < ymin) { 
      ymin = v.y; 
     } 
     if (v.z < zmin) { 
      zmin = v.z; 
     } 
     if (v.x > xmax) { 
      xmax = v.x; 
     } 
     if (v.y > ymax) { 
      ymax = v.y; 
     } 
     if (v.z > zmax) { 
      zmax = v.z; 
     } 
    } 
    //Scales it to the objects scale****** 
    xmin *= scale; 
    ymin *= scale; 
    zmin *= scale; 
    xmax *= scale; 
    ymax *= scale; 
    zmax *= scale; 
    //Checks if the point is in the cube****** 
    if (px >= xmin + (entity.getPosition().x) && px <= xmax + (entity.getPosition().x) && py >= ymin + (entity.getPosition().y) && py <= ymax + (entity.getPosition().y) && pz >= zmin + (entity.getPosition().z) && pz <= zmax + (entity.getPosition().z)) { 
     hit = true; 
    } 

} 

我寧願沒有擊中格系統,這樣,如果有另一個(工作)的方式,請告訴我。

我的問題的例子。

enter image description here

回答

1

通常情況下,你要做的就是創建模型稱爲碰撞網格的低多邊形版本(但你的模型似乎是相當低的多,因此可能會奏效,因爲它是)。 然後,一種做法是使用ray casting。發送射線(或幾個)從你的角色,看看他們是否第一個

  1. 相交的另一個對象的邊界卷。這樣可以節省更多昂貴的計算。
  2. 如果存在,則會看到它是否與實際(碰撞)網格相交。

如果光線投射是不夠的,你可以做交叉實際網格支票或約束體積VS網。這實施起來更加昂貴和複雜。這裏似乎有一些資源:Object/Object Intersection