6
\$\begingroup\$

I am having some trouble making bounding boxes for my models I am using within XNA 4.0. The way it was done in XNA 3.1 seems to be obsolete as you can no longer access parameters that were used before in XNA 3.1. Has anyone got any decent links I have been looking around the net for a while now and all I am finding is things for XNA 3.1 or below.

Help much apprechiated. Regards Mark

EDIT:

Hey, the problem i am having is getting the min and max points for my model i load through the content pipeline. as articles i have seen on how to do it tell you to do such things as VertexPositionNormalTexture[] vertices= new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / mesh.MeshParts[0].VertexStride; but i can no longer call size in bytes and vertex strides within XNA 4.0

\$\endgroup\$
4
  • \$\begingroup\$ Can you post some sample code that no longer works, or at least name the parameters that are now obsolete? \$\endgroup\$
    – tenpn
    Aug 9, 2010 at 9:31
  • \$\begingroup\$ Hey, the problem i am having is getting the min and max points for my model i load through the content pipeline. as articles i have seen on how to do it tell you to do such things as VertexPositionNormalTexture[] vertices= new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / mesh.MeshParts[0].VertexStride; but i can no longer call size in bytes and vertex strides within XNA 4.0 \$\endgroup\$ Aug 9, 2010 at 10:31
  • \$\begingroup\$ It might be better to update your question rather than post a comment. Just click the "edit" link underneath the question text. \$\endgroup\$
    – ChrisF
    Aug 9, 2010 at 11:10
  • \$\begingroup\$ It's really nitpicking I know, but could you please put your title in the form of a question. It may help you get answers. \$\endgroup\$ Aug 9, 2010 at 13:08

1 Answer 1

13
\$\begingroup\$

I have encountered the same problem yesterday. My solution is this one, it is vertex type independent and works quite good.

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

All calculations are done in world space, this gives you world space bounding boxes (which is quite common for AABBs). Since it is quite expensive (but still performs good in real-time) just update it on dynamic objects and only when a rotation is performed (you can just translate AABBs with objects if that is the only transform).

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Performance note: You are transforming every vertex which is unnecessary; just transform the min and max at the end, and you'll get the same result. \$\endgroup\$
    – LynchDev
    Jul 3, 2013 at 18:20
  • 1
    \$\begingroup\$ Great answer but you might want to multiply with the bone transformations as well. If its a model without animations you can just do something like ``` worldTransform = worldTransform * model.Bones[0].Transform;``` \$\endgroup\$
    – Ronen Ness
    Jan 10, 2017 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .