【问题标题】:3d bounding Box border xna3d 边界框边框 xna
【发布时间】:2012-03-23 00:42:55
【问题描述】:

所以,我正在尝试开发 3d 乒乓球游戏,但在将球与桌子碰撞时遇到了问题。我有一个球的边界球和桌子的边界框,但交点不是那么准确,我猜测边界框是不正确的,因为球与球拍的碰撞很好。

我正在尝试显示边界框,以便更容易查看错误所在,但我似乎无法使用我的代码实现我找到的教程,或者只是不知道如何实现。而且,由于我的桌子不动,我创建 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;
}

【问题讨论】:

    标签: 3d xna collision bounding-box


    【解决方案1】:

    我认为你把事情复杂化了。这是我的想法:

    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
    

    【讨论】:

    • 嘿,我发现了问题,我的边界框功能给了我错误的答案。是的,我明白你的意思,实际上我创建了一个从左上角到右下角的简单边界框并删除了该功能并简单地检查了球是否与它发生碰撞。感谢您的回答:)
    猜你喜欢
    • 2013-04-01
    • 2013-08-30
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多