# BoundingBox created from mesh to origin, making it bigger

I'm working on a level-based survival game and I want to design my scenes in Maya and export them as a single model (with multiple meshes) into XNA.

My problem is that when I try to create Bounding Boxes(for Collision purposes) for each of the meshes, the are calculated from origin to the far-end of the current mesh, so to speak.

I'm thinking that it might have something to do with the position each mesh brings from Maya and that it's interpreted wrongly... or something.

Here's the code for when I create the boxes:

private static BoundingBox CreateBoundingBox(Model model, ModelMesh mesh)
{

Matrix[] boneTransforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(boneTransforms);

BoundingBox result = new BoundingBox();

foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
BoundingBox? meshPartBoundingBox = GetBoundingBox(meshPart, boneTransforms[mesh.ParentBone.Index]);
if (meshPartBoundingBox != null)
result = BoundingBox.CreateMerged(result, meshPartBoundingBox.Value);
}
result = new BoundingBox(result.Min, result.Max);
return result;
}
private static BoundingBox? GetBoundingBox(ModelMeshPart meshPart, Matrix transform)
{
if (meshPart.VertexBuffer == null)
return null;

Vector3[] positions = VertexElementExtractor.GetVertexElement(meshPart, VertexElementUsage.Position);
if (positions == null)
return null;

Vector3[] transformedPositions = new Vector3[positions.Length];
Vector3.Transform(positions, ref transform, transformedPositions);

for (int i = 0; i < transformedPositions.Length; i++)
{
Console.WriteLine(" " + transformedPositions[i]);
}
return BoundingBox.CreateFromPoints(transformedPositions);
}

public static class VertexElementExtractor
{
public static Vector3[] GetVertexElement(ModelMeshPart meshPart, VertexElementUsage usage)
{
VertexDeclaration vd = meshPart.VertexBuffer.VertexDeclaration;
VertexElement[] elements = vd.GetVertexElements();

Func<VertexElement, bool> elementPredicate = ve => ve.VertexElementUsage == usage && ve.VertexElementFormat == VertexElementFormat.Vector3;
if (!elements.Any(elementPredicate))
return null;

VertexElement element = elements.First(elementPredicate);

Vector3[] vertexData = new Vector3[meshPart.NumVertices];
meshPart.VertexBuffer.GetData((meshPart.VertexOffset * vd.VertexStride) + element.Offset,
vertexData, 0, vertexData.Length, vd.VertexStride);

return vertexData;
}
}


Here's a link to the picture of the mesh(The model holds six meshes, but I'm only rendering one and it's bounding box to make it clearer: http://www.gsodergren.se/portfolio/wp-content/uploads/2011/10/Screen-shot-2011-10-24-at-1.16.37-AM.png The mesh that I'm refering to is the Cubelike one. The cylinder is a completely different model and not part of any bounding box calculation.

I've double- (and tripple-)-checked that this mesh corresponds to this bounding box.

Any thoughts on what I'm doing wrong?

I have found the solution to this problem of yours.

Instead of doing just the following in CreateBoundingBox:

BoundingBox result = new BoundingBox();


do this:

BoundingBox result = new BoundingBox();
result.Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
result.Max = new Vector3(float.MinValue, float.MinValue, float.MinValue);


When you create a new BoundingBox, the Min value is set to 0,0,0. Then later where you merge the boundingbox with result, one of the values (x,y,z) stays 0, depending on the mesh's position. This causes the box to stretch to 0,0,0 when you draw the bounding box.

So setting the Max to the minimum possible, and Min to the maximum possible, the situation is avoided.

In my tests it did fix the issue.

• +1 to finding that error, but also +1 to Jonathon Hobbs and the hint "In fact, it's drawing from the exact centre of that cylinder. This very unlikely to be a coincidence". What is the bet that cylinder is drawn at 0,0,0. :) – deceleratedcaviar Mar 1 '12 at 4:43