redneon

I have a bounding box around a rotating object which is positioned at the origin. The object is only rotating about the Z-axis and I'm wanting the bounding box to rotate with it. To achieve this I am translating the bounding box's coordinates by the same rotation matrix as I'm using to rotate the object but it's not working correctly. Instead of rotating the bounding box it just appears to shrink and stretch it.

I have written a simple method which just draws lines between the bounding boxes coordinates to display the bounding box which is how I know that the bounding box isn't being rotated correctly. Interestingly enough, the bounding box is always drawn with vertical and horizontal lines, never diagonal lines. So it could be my drawing method which is incorrect but I can't see what I'm doing wrong.

This is the code for calculating the position and rotation of the bounding box:

Matrix rotMatrix = Matrix.CreateRotationZ(rotation.Z);

boundingBox.Min.X = position.X - 40;
boundingBox.Min.Y = position.Y - 70;

boundingBox.Max.X = position.X + 40;
boundingBox.Max.Y = position.Y + 70;

boundingBox.Min = Vector3.Transform(boundingBox.Min, rotMatrix);
boundingBox.Max = Vector3.Transform(boundingBox.Max, rotMatrix);

Matrix tempMatrix = worldMatrix * rotMatrix * Matrix.CreateTranslation(position);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = tempMatrix;
}
}

This is the method I am using to draw the bounding box:

public void DrawBoundingBox(BoundingBox boundingBox)
{
BasicEffect basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
basicEffect.EnableDefaultLighting();

basicEffect.DiffuseColor = new Vector3(1.0f, 0.0f, 0.0f);

Matrix viewMatrix = camera.View;
Matrix projectionMatrix = camera.Projection;
Matrix worldMatrix = Matrix.CreateRotationX(0);

basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;

VertexPositionColor[] vertices = new VertexPositionColor;

Vector3 topLeft = new Vector3(boundingBox.Min.X, boundingBox.Max.Y, 0.0f);
Vector3 topRight = new Vector3(boundingBox.Max.X, boundingBox.Max.Y, 0.0f);
Vector3 bottomLeft = new Vector3(boundingBox.Min.X, boundingBox.Min.Y, 0.0f);
Vector3 bottomRight = new Vector3(boundingBox.Max.X, boundingBox.Min.Y, 0.0f);

vertices[0] = new VertexPositionColor(bottomLeft, Color.White);
vertices[1] = new VertexPositionColor(topLeft, Color.White);

vertices[2] = new VertexPositionColor(topLeft, Color.White);
vertices[3] = new VertexPositionColor(topRight, Color.White);

vertices[4] = new VertexPositionColor(topRight, Color.White);
vertices[5] = new VertexPositionColor(bottomRight, Color.White);

vertices = new VertexPositionColor(bottomRight, Color.White);
vertices[7] = new VertexPositionColor(bottomLeft, Color.White);

VertexBuffer vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
VertexPositionColor.SizeInBytes * vertices.Length,
ResourceUsage.None,
ResourceManagementMode.Automatic
);

vertexBuffer.SetData<VertexPositionColor>(vertices);

VertexDeclaration basicEffectVertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);

graphics.GraphicsDevice.VertexDeclaration = basicEffectVertexDeclaration;

graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);

basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();

graphics.GraphicsDevice.DrawPrimitives(
PrimitiveType.LineList,
0,
4
);

pass.End();
}
basicEffect.End();
}

Can anyone spot what I'm doing wrong

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

redneon

I've just run my application again and it looks like it could be my drawing method that's displaying the bounding box incorrectly rather than the bounding box rotation not working. The reason I think this is because whenever the object is vertical, the bounding box is displayed vertical and whenever the object is horizontal, the bounding box is horizontal.

I can't see what I'm doing wrong in my drawing method though...

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

dczraptor

The XNA bounding box is axis aligned. That means it will never be rotated and so when you "rotate" it, you just get a larger box needed to encompass the entire model. Here's a blog entry about that: http://sharky.bluecog.co.nz/ p=108

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

redneon

dczraptor wrote:
 The XNA bounding box is axis aligned. That means it will never be rotated and so when you "rotate" it, you just get a larger box needed to encompass the entire model. Here's a blog entry about that: http://sharky.bluecog.co.nz/ p=108

Excellent, thanks. It looks like I'll have to use bounding spheres instead, then :)

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

redneon

Just another quick question I'm hoping someone can help me with =o) I'm trying to change my game to use bounding spheres instead of bounding boxes but it's a bit hard without knowing what the bounding sphere covers.

Does anyone know of a way to draw a primitive sphere in XNA that I could use to represent my bounding sphere

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

dczraptor

Check out sharky's source code. He draws circles like the one in this post: http://sharky.bluecog.co.nz/ p=121

Re: XNA Game Studio Express Bounding Box Not Rotating Correctly

redneon

dczraptor wrote:
 Check out sharky's source code. He draws circles like the one in this post: http://sharky.bluecog.co.nz/ p=121

Ah, of course :) The BoundingSphereRenderer class. I should have spotted it.

Thanks again.

Also, for reference. I found quite a nifty OrientedBoundingBox class here. From the example application it seems to work quite well but I couldn't get the rotation working when I tried to implement it so I'm just going to use bounding spheres for now. I'm in a rush to get this game finished before the first phase of the DBP competition finishes :)