Bounding box

Topics: Developer Forum
Dec 23, 2006 at 3:12 PM
I tried adding a bounding box calculation based on this post

Then I tried drawing the bounding box in my code around the model (bear_walking.x). The box came out the right size but while the box's local center was smack at (0,0,0), the bear's center seems to be offset. When debugging I saw that that's actually true. The question is:
A) is there a global translation applied to the entire bear mesh (one that I need to take into consideration when calculation the box).
b) The post above includes this line:
trans = Matrix.CreateRotationX((float)(Math.PI / 2));
The it follows by multiplying each vertex by that matrix. Why does it need to do that in the first place?

Follows the exact code I added:

To AnimatedModelProcessor class

double minX = double.MaxValue;
double minY = double.MaxValue;
double minZ = double.MaxValue;
double maxX = double.MinValue;
double maxY = double.MinValue;
double maxZ = double.MinValue;
Matrix trans = Matrix.CreateRotationX((float)(Math.PI / 2));
List<BoundingBox> boxes = new List<BoundingBox>();

To Process():
trans = Matrix.CreateRotationX((float)(Math.PI / 2));
//MeshHelper.TransformScene(input, trans);
dict"bboxes" = boxes;

To AnimatedModelProcessor class

private void FindBoundingBox(NodeContent content)
foreach (NodeContent o in content.Children)
if (o is MeshContent)
BoundingBox bb = new BoundingBox();
minX = double.MaxValue;
minY = double.MaxValue;
minZ = double.MaxValue;
maxX = double.MinValue;
maxY = double.MinValue;
maxZ = double.MinValue;
MeshContent mesh = (MeshContent)o;
foreach (Vector3 basev in mesh.Positions)
Vector3 v = Vector3.Transform(basev, trans);
//Vector3 v = basev;
if (v.X < minX)
minX = v.X;
if (v.Y < minY)
minY = v.Y;
if (v.Z < minZ)
minZ = v.Z;
if (v.X > maxX)
maxX = v.X;
if (v.Y > maxY)
maxY = v.Y;
if (v.Z > maxZ)
maxZ = v.Z;

double lenX = maxX - minX;
double lenZ = maxZ - minZ;
double lenY = maxY - minY;
trans *= Matrix.CreateTranslation((float)(-minX - lenX / 2.0), (float)(-minY - lenY / 2.0), (float)(-minZ - lenZ / 2.0));
bb.Min = new Vector3((float)minX, (float)minY, (float)minZ);
bb.Max = new Vector3((float)maxX, (float)maxY, (float)maxZ);

else FindBoundingBox(o);


Dec 28, 2006 at 7:35 PM
The root bone, or a bone near the root, translates the bear up to its current position. The bounding box is drawn using only the vertices, and does not take into account bones (pretty sure of this).