• Create Account

## Best way to create bounding boxes

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

443
Like
0Likes
Like

Posted 16 July 2013 - 09:38 AM

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

### #2smr  Members

2387
Like
0Likes
Like

Posted 16 July 2013 - 10:05 AM

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

443
Like
0Likes
Like

Posted 16 July 2013 - 10:12 AM

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

### #4Pink Horror  Members

2459
Like
0Likes
Like

Posted 16 July 2013 - 10:44 AM

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.

443
Like
0Likes
Like

Posted 16 July 2013 - 12:21 PM

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

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

### #6smr  Members

2387
Like
0Likes
Like

Posted 16 July 2013 - 01:08 PM

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

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.

443
Like
0Likes
Like

Posted 16 July 2013 - 01:26 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.