Jump to content
  • Advertisement
Sign in to follow this  

[solved](XNA) Creating Bounding Boxes from Model, need a little help

This topic is 3389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The goal here is to create a AABB(BoundingBox) around each "mesh" / individual piece of the model. Alright here's my code:
public List <BoundingBox> GetBoundingBoxesFromModel(){
            List <BoundingBox> retList = new List<BoundingBox>();
            BoundingBox boundingBox = new BoundingBox();
            for(int meshNumber = 0; meshNumber < model.Meshes.Count; meshNumber++){
                  ModelMesh mesh = model.Meshes[meshNumber];
                  VertexPositionNormalTexture[] vertices =
                        new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes];


                  Vector3[] vertexs = new Vector3[vertices.Length];

                  for (int index = 0; index < vertexs.Length; index++) {
                        vertexs[index] = vertices[index].Position;
                  //BoundingBox Transforms
                  boundingBox = BoundingBox.CreateMerged(boundingBox, BoundingBox.CreateFromPoints(vertexs));
                  boundingBox.Min = Vector3.Transform(boundingBox.Min,
                      modelTransformations[model.Meshes[meshNumber].ParentBone.Index] * modelWorldMatrix);
                  boundingBox.Max = Vector3.Transform(boundingBox.Max,
                      modelTransformations[model.Meshes[meshNumber].ParentBone.Index] * modelWorldMatrix);

                  boundingBox = fixBoundingBoxData(boundingBox);

                  //System.Diagnostics.Debug.Write(boundingBox.ToString() + "\n");
                  //System.Diagnostics.Debug.Write("Mesh(" + meshNumber + "): " + boundingBox.ToString() + "\n");
                  boundingBox = new BoundingBox();

            return retList;

        private BoundingBox fixBoundingBoxData(BoundingBox AABB){
            Vector3 min = AABB.Min, max = AABB.Max;
            float tempData = 0f;
            if (min.X > max.X) { tempData = min.X; min.X = max.X; max.X = tempData; }
            if (min.Y > max.Y) { tempData = min.Y; min.Y = max.Y; max.Y = tempData; }
            if (min.Z > max.Z) { tempData = min.Z; min.Z = max.Z; max.Z = tempData; }
            AABB.Min = min;
            AABB.Max = max;
            return AABB;
It works 99% fine. But for some reason it is either MISREADING a value for one of my objects, or something else is happening. It's the third Mesh's either X or Z. I'm beggining to blame the model data. However if the model data where incorrect, then wouldn't it be displaying incorrectly(I'd assume so)? Could the problem be how I do my bounding box transforms? Any Ideas? I noticed if I use different Vertex buffer structures(VertexPositionColor, VertexPositionNormalTexture, etc) my data is a little different. The problemematic variable seems to be recieving the most change from playing with those. I know my model is a .x, and has no texture on it the only Material is a white color. Should I be getting data from ModelMeshPart verses ModelMesh? [Edited by - Cakey on June 11, 2009 10:55:05 AM]

Share this post

Link to post
Share on other sites
Thanks for that link. However, I've already seen it. My problem with it is it has to be done at compilation(Custom Content Processor). Does this idea only work at compilation time, or is it feasible to accomplish this just during run time?

Could it be because of the ".CreateMerged(" statement? when I remove it the vertex data looks accurate but it doesn't seem to work at all...hmm

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!