3d边界框边界xna

时间:2012-03-05 14:36:56

标签: 3d xna collision bounding-box

所以,我正在尝试开发一款3D乒乓球比赛,而且我在将球与桌子碰撞方面遇到了问题。我有一个用于桌子的球和边界框的边界球体,但是交叉点并不准确,我猜测边界框是不正确的,因为球与球拍碰撞是好的。

我试图显示边界框,以便更容易看到错误的位置,但我似乎无法实现我在代码中找到的教程,或者只是不要&#39不知道怎么做。而且,由于我的桌子不动,我是否正确创建了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;
}

1 个答案:

答案 0 :(得分: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
相关问题