# creating bounding box list

I'm trying to create a list of bounding boxes for each cube drawn, so I can use the boxes to intersect with a ray that my mouse position is casting, but I have no idea how. I've created a list that stores the boxes, but how am I getting the values from each box?

    for (int x = 0; x < mapHeight; x++)
{
for (int z = 0; z < mapWidth; z++)
{
cubes.Add(new Vector3(x, map[x, z], z), Matrix.Identity, grass);
}
}

public Cube(GraphicsDevice graphicsDevice)
{
device = graphicsDevice;

var vertices = new List<VertexPositionTexture>();

BuildFace(vertices, new Vector3(0, 0, 0), new Vector3(0, 1, 1));
BuildFace(vertices, new Vector3(0, 0, 1), new Vector3(1, 1, 1));
BuildFace(vertices, new Vector3(1, 0, 1), new Vector3(1, 1, 0));
BuildFace(vertices, new Vector3(1, 0, 0), new Vector3(0, 1, 0));

BuildFaceHorizontal(vertices, new Vector3(0, 1, 0), new Vector3(1, 1, 1));
BuildFaceHorizontal(vertices, new Vector3(0, 0, 1), new Vector3(1, 0, 0));

cubeVertexBuffer = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly);

cubeVertexBuffer.SetData<VertexPositionTexture>(vertices.ToArray());

}


There aren't any clearly defined variables for the bounds of each cube created, so where do I create the bounding box from?

Generate the corner vertices of your cube. Apply the cube's rotation matrix. Find the min/max of the transformed vertices. Construct an AABB from those. Apply the cube's translation. Done.

min_vertex = (+float_max, +float_max, +float_max)
max_vertex = (-float_max, -float_max, -float_max)

foreach vertex in vertices:
vertex = apply(rotation, vertex)

min_vertex = (min(min_vertex.x, x), min(min_vertex.y, y), min(min_vertex.z, z))
max_vertex = (max(max_vertex.x, x), max(max_vertex.y, y), max(max_vertex.z, z))

return aabb(min_vertex, max_vertex) + translation


If all you have is a transformation matrix, this is even easier. Just multiple that matrix by each vertex and forget about separately handling rotation and translation. You can even handle scaling transforms that way.

• Perfect. And this can be easily implemented into my cube class too. Thanks! – Christian Frantz Jun 23 '13 at 22:13
• You could. You might consider building the full version into the AABB class, actually. Use the core loop to generate an AABB for any object in model-space. Then apply the transformation to the AABB using the given code to get an AABB in world-space. Your cube can just spit out an AABB with no transformation, a Sphere can likewise easily spit out an AABB (the utility would deal with translation and scaling and rotation in the face of scaling), and you can precalculate AABBs for meshes and other complex geometry and then pass those and instances' world transforms into the utility. – Sean Middleditch Jun 23 '13 at 22:17
• I think it'd be easier to just use AABBs for my cubes because they only have a Vector3 position and a texture. For my models I'll be using bounding spheres, as long as for any projectile the player can shoot. Although it would be easier to just use this for each object created in the game, I'm not sure how it would do performance-wise – Christian Frantz Jun 23 '13 at 22:24
• It would do just fine if you pre-cache the OBB when the mesh is loaded. Remember that once you have an OBB, you can use the above snippet to generate an AABB. For many games you won't even need to cache it - don't underestimate modern CPUs and overestimate the performance sensitivity of your project. Of course, if curious, test and profile and find out for sure. – Sean Middleditch Jun 24 '13 at 1:34

If by bounding box you meant AABB, then you can write a general AABB computer by finding the min and max coordinate on each axis by looping over the world space points of your object. Usually each object will generate its AABB every frame, unless that object hasn't moved at all.

• In my case, the cubes will not be moving at all unless one is placed or removed. Each cube has a position stored as a Vector3, but I'm not sure how to get the dimensions of the cube – Christian Frantz Jun 23 '13 at 21:41
• You get the dimensions from the vertices. – RandyGaul Jun 23 '13 at 21:58