2012-03-05 7 views
0

私は3次元テーブルテニスゲームを開発しようとしています。ボールをテーブルに衝突させるときに問題があります。私はボールの境界球とテーブルの境界ボックスを持っていますが、交差点は正確ではありません。ボールをラケットに衝突させるので境界ボックスが間違っていると推測しています。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

私はあなたがものをオーバーコンプリートしていると思います。ここに私の考えです:私は最後に私が 座標で何かを台無しにしているかもしれないので、XNAを使用するのでそれはしばらくしている

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; 
    } 
} 

が、これは私が何を意味したものです:

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

をねえ、私が見つかりました。私のバウンディングボックス機能は私に間違った答えを与えていた。実際には、私はupperleftcornerからlowerrightcornerまで単純なバウンディングボックスを作成し、その機能を削除し、ボールがそれに衝突するかどうかを単にチェックしました。回答ありがとうございます :) –