2012-03-05 43 views
0

所以,我正在嘗試開發一款3d乒乓球遊戲,而我在碰撞球與桌子時遇到了問題。我對球的邊界框和邊界框有一個界限,但是交叉點並不準確,我猜測邊界框是不正確的,因爲碰撞球和球拍是很好的。三維邊界盒邊框xna

我試圖顯示邊界框,以便更容易看到錯誤在哪裏,但我似乎無法實現我用我的代碼找到的教程,或者只是不知道如何。而且,我正確地創建了AABB,因爲我的桌子沒有移動?如果任何人都可以提供幫助,我們將不勝感激,或者如果有人能夠提出更好的方法來將球與盒子碰撞(如果有的話),謝謝。我粘貼了邊界框檢測,以及球體和框之間的碰撞。如果需要更多的代碼,我會發布它。謝謝

private bool Ball_Table(Model model1, Matrix world1) 
{  
    for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++) 
    { 
     BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere; 
     sphere1 = sphere1.Transform(world1); 

     if(table_box.Intersects(sphere1)) 
      return true; 
    } 
    return false; 
} 

protected BoundingBox UpdateBoundingBox(Model model, Matrix worldTransform) 
{ 
    // Initialize minimum and maximum corners of the bounding box to max and min values 
    Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); 
    Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); 

    // For each mesh of the model 
    foreach (ModelMesh mesh in model.Meshes) 
    { 
     foreach (ModelMeshPart meshPart in mesh.MeshParts) 
     { 
      // Vertex buffer parameters 
      int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride; 
      int vertexBufferSize = meshPart.NumVertices * vertexStride; 

      // Get vertex data as float 
      float[] vertexData = new float[vertexBufferSize/sizeof(float)]; 
      meshPart.VertexBuffer.GetData<float>(vertexData); 

      // Iterate through vertices (possibly) growing bounding box, all calculations are done in world space 
      for (int i = 0; i < vertexBufferSize/sizeof(float); i += vertexStride/sizeof(float)) 
      { 
       Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), worldTransform); 

       min = Vector3.Min(min, transformedPosition); 
       max = Vector3.Max(max, transformedPosition); 
      } 
     } 
    } 

    // Create and return bounding box 
    table_box = new BoundingBox(min, max); 
    return table_box; 
} 

回答

0

我覺得你太過於複雜了。這是我的想法:

class Ball 
{ 
    public Vector3 Position; 
    public float Radius; 

    // ... 
} 

class Table 
{ 
    public Vector3 Position; 
    public SizeF Size; 

    // ... 

    public bool CheckForCollision(Ball ball) 
    { 
     Vector3 upperLeftCorner = new Vector3(
       this.Position.X - this.Size.Width, 
       this.Position.Y, 
       this.Position.Z - this.Size.Heigth), 
      lowerRightCorner = new Vector3(
       this.Position.X + this.Size.Width, 
       this.Position.Y, 
       this.Position.Z + this.Size.Height); 

     if(ball.Position.X < upperLeftCorner.X || 
      ball.Position.X > lowerRightCorner.X) 
     { 
      return false; 
     } 

     if(ball.Position.Z < upperLeftCorner.Z || 
      ball.Position.Z > lowerRightCorner.Z) 
     { 
      return false; 
     } 

     if(ball.Position.Y - ball.Radius > this.Position.Y) 
     { 
      return false; 
     } 

     return true; 
    } 
} 

這已經有一段時間,因爲我最後一次使用XNA,所以我可能會搞砸了一些與 座標,但是這是我的意思做的事:

origin 
    +----> X, Width 
    | 
    | upperLeftCorner 
    |  +----------------+ 
    v  |    | 
      |    | 
    y, |    | 
    height |    | 
      |    | 
      |  +  | 
      | table.position | 
      |    | 
      |    | 
      |    | 
      |    | 
      +----------------+ 
         lowerRightCorner 
+0

嘿,我發現出了概率,我的邊界框函數給了我錯誤的答案。我得到了你的說法,實際上我創建了一個從upperleftcorner到lowerrightcorner的簡單邊界框,並移除了該功能,並簡單地檢查球是否與其碰撞。謝謝回答 :) – 2012-03-05 22:02:42