Best way to create bounding boxes

Started by
5 comments, last by ChristianFrantz 10 years, 9 months ago

I have a CubeChunk class which creates indices and vertices for each "Cube" in my chunk. What I want to do is create a bounding box for each of these cubes, so what I'm wondering is what is the best way to do this? Should I create a separate list for each chunk and store the bounding boxes in that? Or should I call a CreateBoundingBox method each time the cube is created or drawn?

For Example


for (int x = 0; x < 50; x++)
            {
                for (int z = 0; z < 50; z++)
                {
                    for (int y = 0; y <= map[x, z]; y++)
                    {

                        if (y >= 1)
                        {
                            SetUpIndicesAndVertices(x, map[x, z] - y, z, vertices, indices);
                            cubes.Add(new Cube.Stone(device, new Vector3(x, map[x, z] - y, z), stone));
                              CreateBoundingBox(some kind of vector3 values here);
                        }

                        else
                        {
                            SetUpIndicesAndVertices(x, map[x, z] - y, z, vertices, indices);
                            cubes.Add(new Cube.Grass(device, new Vector3(x, map[x, z] - y, z), grass));
                              CreateBoundingBox(some kind of vector3 values here);
                        }
                    }
                }

Or I could replace the line with cubeBB.Add(new BoundingBox(some vector3 crap here). Which one would make more sense to use?

If you see a post from me, you can safely assume its C# and XNA :)

Advertisement

Just curious why you need a separate bounding box for each cube? Wouldn't each cube already be its own bounding box?

To test collision between mouse and cube, or player and cube. I didn't know I could just use the cube as its own bounding box though. How would I do that?

If you see a post from me, you can safely assume its C# and XNA :)

If I understand correctly, you want to take your "Cube", which is your own custom box, and make an official XNA BoundingBox out of it. I think you should make a Cube Intersect method, and locally construct the BoundingBox inside of that method for the intersection test. It's better than having it hang around in memory. A local struct shouldn't cost much to create.

So what you're saying is to create the BoundingBox of the cube at the point of intersection with the mouse or player? I guess that could work. It would save a lot of unnecessary bounding boxes in the memory. Although I didn't think c# had an issue with things hanging around in memory but who knows :P

If you see a post from me, you can safely assume its C# and XNA :)

So what you're saying is to create the BoundingBox of the cube at the point of intersection with the mouse or player? I guess that could work. It would save a lot of unnecessary bounding boxes in the memory. Although I didn't think c# had an issue with things hanging around in memory but who knows tongue.png

An optimization over this would be instead of creating a new cube to test collision, set aside a number of already created bounding boxes in a circular queue and reuse. Update the appropriate fields and properties of the BB to match the cube and send to the collision tester. This will save you many allocations per collision frame. If you plan to target a mobile device this will help to reduce garbage collections which could affect your framerate.

What I was thinking is that I could have something like this:

x, x, x, x, x,

x, x, x, x, x,

x, x, o, x, x,

x, x, x, x, x,

x, x, x, x, x,

Where the x's are equal to 2 cubes and 0 is the player. The 5x5 space would be like a bounding box for the player which moves with the player and allows the player to only mine or build within that 5x5 box. I would definitely decrease the amount of bounding boxes needed, but I wouldn't even know how to start coding it.

If you see a post from me, you can safely assume its C# and XNA :)

This topic is closed to new replies.

Advertisement