Page 1 of 1

Transform (scale, rotate, translate) bounding box

Posted: Fri Feb 10, 2012 5:11 pm
by rmnbp3
Hello,

I just started using the Bepu Physics Engine and noticed that bounding boxes retain being axis-aligned, but also resize to fit the containing object as the object scales, rotates, and translates. I'm trying to do the same thing in a sample that I wrote, not using Bepu Physics, but the bounding box doesn't refit as the object transforms. Here's some sample code below. If the cube continously rotates around the Y axis, the bounding box continues to expand and translate up the Y axis and not resize and stay in place. I want the bounding box to shrink and grow as the cube rotates exactly how the bounding boxes do in the Bepu demos. I can't seem to find any code samples online on how to achieve this. Any help would be much appreciated.

F.Y.I. The cube below is a simple primitive, using VertexBuffer and IndexBuffer, and not a model.

// Create axis-aligned bounding box based on the cube's vertices.
BoundingBox cubeBoundingBox = BoundingBox.CreateFromPoints(cubeVertices);

// Rotate the cube around the Y axis.
cube.World = Matrix.CreateRotationY(MathHelper.ToRadians(cubeRotationVelocity * (float)gameTime.ElapsedTime.TotalSeconds));

// Transform the cube's bounding box into an oriented bounding box.
Matrix cubeWorld = cube.World;
Vector3[] corners = cubeBoundingBox.GetCorners();
Vector3.Transform(corners, ref world, corners);

// Convert the oriented bounding box into an axis-aligned one.
cubeBoundingBox = BoundingBox.CreateFromPoints(corners);

Re: Transform (scale, rotate, translate) bounding box

Posted: Fri Feb 10, 2012 10:48 pm
by Norbo
It sounds like there's some feedback going on.

Right now the 'world' matrix is being set to an incremental rotation. This implies that the cubeVertices data already includes some transforms. The world matrix should be the full current state.

If:
-the cubeVertices input is in local space,
-the world transform is the correct full world transform, and
-the computed world space corners are only used to compute the current bounding box, not fed back into the next frame's calculations
it should work.

The important thing is to walk through the applied transforms and at each stage make sure it matches your expectations.

Re: Transform (scale, rotate, translate) bounding box

Posted: Fri Feb 10, 2012 10:53 pm
by Norbo
The code for computing a bounding box of a box can also be found in the BoxShape class.

Re: Transform (scale, rotate, translate) bounding box

Posted: Fri Feb 10, 2012 11:54 pm
by rmnbp3
Thanks for the reply Norbo! I'll step through my code again this evening and will let you know.

Re: Transform (scale, rotate, translate) bounding box

Posted: Sat Feb 11, 2012 12:30 am
by rmnbp3
After stepping through my code once more, I saw that the cube's bounding box was being fed into the next frame, thus causing the current bounding box to continuously expand. Thanks for the last suggestion in your if check above Norbo. This has been driving me crazy for the last week.