2012-03-05 60 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