# How do i create bounding boxes with XNA 4.0?

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

• Can you post some sample code that no longer works, or at least name the parameters that are now obsolete? – tenpn Aug 9 '10 at 9:31
• 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 – markblue777 Aug 9 '10 at 10:31
• It might be better to update your question rather than post a comment. Just click the "edit" link underneath the question text. – ChrisF Aug 9 '10 at 11:10
• 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. – Jesse Dorsey Aug 9 '10 at 13:08

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).

• 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. – LynchDev Jul 3 '13 at 18:20
• 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; – Ronen Ness Jan 10 '17 at 15:21