XNA 4.0 - Bounding Boxes

So I'm kind of new to programming and I'm trying to check collision using bounding boxes. I'm currently stuck as to how to call my bounding box within my bounding box function as shown below

public BoundingBox BuildBoundingBox()
{
// Create initial variables to hold min and max xyz values for the mesh
Vector3 meshMax = new Vector3(float.MinValue);
Vector3 meshMin = new Vector3(float.MaxValue);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
// The stride is how big, in bytes, one vertex is in the vertex buffer
// We have to use this as we do not know the make up of the vertex
int stride = part.VertexBuffer.VertexDeclaration.VertexStride;

VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);

// Find minimum and maximum xyz values for this mesh part
Vector3 vertPosition = new Vector3();

for (int i = 0; i < vertexData.Length; i++)
{
vertPosition = vertexData[i].Position;

// update our values from this vertex
meshMin = Vector3.Min(meshMin, vertPosition);
meshMax = Vector3.Max(meshMax, vertPosition);
}
}

// transform by mesh bone matrix
meshMin = Vector3.Transform(meshMin, meshTransform);
meshMax = Vector3.Transform(meshMax, meshTransform);
}
// Create the bounding box
BoundingBox box1 = new BoundingBox(meshMin, meshMax);
return box1;

}

I'm trying to call box1 (my bounding box) in my update method to check collision however I can't seem to call box1 as it's not in the context of my class.

• There is a fundamental problem with your algorithm. The box's min/max vertices are not simply the min/max vertPositions like you are working towards. To create the actual Box.min, the X component may be from one vertPosition while the Y component may be from a different vertPosition and the Z component from yet another. Nov 12 '15 at 3:29

box1 only resides in that method, but it get's returned so you just have to assign a new BoundingBox object the return value. Then you can access that object in your update function. I'm guessing you have something like this:

class MyGame{
BoundingBox theBox = null;

public MyGame(){
theBox = BuildBoundingBox();
}

public bool Update(){
//Do stuff with theBox here:
theBox.intersects(someOtherBox);
}
}