简体   繁体   中英

Retrieving 3D cylinder parameters to create a Bounding Box

I'm implementing a Kinect application in XNA.

I'm pretty new on 3D programming and I'd like to know how to retrieve parameters such as radius or height from a cylinder model in order to create a bounding box around it for collision detection.

My problem is that my cylinders' position and angle are syncronized with the position of the forearm of the player in the field of the Kinect and so I don't know how to define the bounding box parameters (Center Min or Max values...).

Here is the code for my bounding box creation method:

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

Here is the code for my collision detection method

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

Each time you create a transformedPosition , add it to a list<Vector3> .

Then use that list to create a BoundingBox with the built in method BoundingBox.CreateFromPoints(myListOfTransformedPositions) .

That method will return the correct min and max.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM