【问题标题】:Retrieving 3D cylinder parameters to create a Bounding Box检索 3D 圆柱体参数以创建边界框
【发布时间】:2013-11-20 13:23:40
【问题描述】:

我正在 XNA 中实现 Kinect 应用程序。

我是 3D 编程的新手,我想知道如何从圆柱体模型中检索半径或高度等参数,以便在其周围创建一个边界框以进行碰撞检测。

我的问题是我的圆柱体的位置和角度与 Kinect 场中玩家前臂的位置同步,所以我不知道如何定义边界框参数(中心最小值或最大值...)。

这是我的边界框创建方法的代码:

private BoundingBox CalculateBoundingBox(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
    return new BoundingBox(min, max);
}

这是我的碰撞检测方法的代码

private bool isCollisionDetected(Model m1, Model m2)
{
    bool detection;

    BoundingBox b1 = CalculateBoundingBox(m1);
    BoundingBox b2 = CalculateBoundingBox(m2);

    if (b1.Intersects(b2))
    {
        detection = true;
    }
    else
    {
        detection = false;
    }
    return detection;
}

【问题讨论】:

    标签: c# 3d xna collision-detection bounding-box


    【解决方案1】:

    每次创建transformedPosition 时,将其添加到list&lt;Vector3&gt;

    然后使用该列表通过内置方法 BoundingBox.CreateFromPoints(myListOfTransformedPositions) 创建一个 BoundingBox

    该方法将返回正确的最小值和最大值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多