BoundingBox Intersection Problems
- by Deukalion
When I try to render two cubes, same sizes, one beside the other. With the same proportions (XYZ).
My problem is, why do a Box1.BoundingBox.Contains(Box2.BoundingBox) == ContaintmentType.Intersects - when it clearly doesn't?
I'm trying to place objects with BoundingBoxes as "intersection" checking, but this simple example clearly shows that this doesn't work. Why is that?
I also try checking height of the next object to be placed, by checking intersection, adding each boxes height += (Max.Y - Min.Y) to a Height value, so when I add a new Box it has a height value. This works, but sometimes due to strange behavior it adds extra values when there isn't anything there.
This is an example of what I mean:
BoundingBox box1 = GetBoundaries(new Vector3(0, 0, 0), new Vector3(128, 64, 128));
BoundingBox box2 = GetBoundaries(new Vector3(128, 0, 0), new Vector3(128, 64, 128));
if (box1.Contains(box2) == ContainmentType.Intersects)
{
// This will be executed
System.Windows.Forms.MessageBox.Show("Intersects = True");
}
if (box1.Contains(box2) == ContainmentType.Disjoint)
{
System.Windows.Forms.MessageBox.Show("Disjoint = True");
}
if (box1.Contains(box2) == ContainmentType.Contains)
{
System.Windows.Forms.MessageBox.Show("Contains = True");
}
Test Method:
public BoundingBox GetBoundaries(Vector3 position, Vector3 size)
{
Vector3[] vertices = new Vector3[8];
vertices[0] = position + new Vector3(-0.5f, 0.5f, -0.5f) * size;
vertices[1] = position + new Vector3(-0.5f, 0.5f, 0.5f) * size;
vertices[2] = position + new Vector3(0.5f, 0.5f, -0.5f) * size;
vertices[3] = position + new Vector3(0.5f, 0.5f, 0.5f) * size;
vertices[4] = position + new Vector3(-0.5f, -0.5f, -0.5f) * size;
vertices[5] = position + new Vector3(-0.5f, -0.5f, 0.5f) * size;
vertices[6] = position + new Vector3(0.5f, -0.5f, -0.5f) * size;
vertices[7] = position + new Vector3(0.5f, -0.5f, 0.5f) * size;
return BoundingBox.CreateFromPoints(vertices);
}
Box 1 should start at x -64, Box 2 should start at x 64 which means they never overlap. If I add Box 2 to 129 instead it creates a small gap between the cubes which is not pretty.
So, the question is how can I place two cubes beside eachother and make them understand that they do not overlap or actually intersect?
Because this way I can never automatically check for intersections or place cube beside eachother.