Sign in to follow this  
ChristianFrantz

Best way to create bounding boxes

Recommended Posts

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? 

Share this post


Link to post
Share on other sites
Pink Horror    2459

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
smr    2468

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this