XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Boundingbox troubles?

Last post 10/31/2009 1:10 AM by Matt Dent. 4 replies.
  • 10/29/2009 9:17 PM

    Boundingbox troubles?

    I have a 3D game with some rectangular buildings. When I try to create boundingboxes for these buildings, they are not anywhere near the actual building. I can collide with them even though there are not any buildings there. How do you create a bounding box that is located at the position of the building model?
  • 10/30/2009 12:06 AM In reply to

    Re: Boundingbox troubles?

    If you create your bounding box in object (local) space, then translate your building to some non-origin coordinate in world space, then you would have to translate your bounding box too.

    something like this:

    building.Position = someVector;  // translate building to some position in world space  
     
    building.BoundingBox.Min += someVector;  // need to also translate the bounding box  
    building.BoundingBox.Max += someVector;    
     

    Alternatively you can keep the original local space bounding boxes intact, then when doing collision testing, take a copy of the bounding box, translate it by the buildings world position and do the collision testing against the translated copy.

    something like this:

    void CollisionTest(object1, object2)  
    {  
        BoundingBox box1 = object1.BoundingBox;  
        BoundingBox box2 = object2.BoundingBox;  
     
        box1 += object1.Position;  
        box2 += object2.Position;  
     
        if (box1.Intercepts(box2))  
        {  
            // handle  
        }  
    }  
     
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 10/30/2009 7:17 PM In reply to

    Re: Boundingbox troubles?

    Ok, cool. That's along the lines of what I was originally thinking I would have to do. This is the code I use to generate a boundingbox for my buildings.

    public BoundingBox GetBoundingBoxFromModel(Model model)  
            {  
                BoundingBox boundingBox = new BoundingBox();     
         
                // Copy the Parent transforms.     
                Matrix[] transforms = new Matrix[model.Bones.Count];  
                model.CopyAbsoluteBoneTransformsTo(transforms);     
         
                Matrix transformation;  
     
                foreach (ModelMesh mesh in model.Meshes)     
                {     
                    VertexPositionNormalTexture[] vertices =     
                        new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes];     
                    mesh.VertexBuffer.GetData<VertexPositionNormalTexture>(vertices);     
         
                    Vector3[] vertexs = new Vector3[vertices.Length];     
         
                    transformation = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(rotation.Y)  
                            * Matrix.CreateTranslation(position);     
         
                    for (int index = 0; index < vertexs.Length; index++)     
                    {     
                        vertexs[index] = Vector3.Transform(vertices[index].Position, transformation); // Transform the vertex  
                    }     
         
                    boundingBox = BoundingBox.CreateMerged(new BoundingBox(),     
                      BoundingBox.CreateFromPoints(vertexs));     
                }  
     
                return boundingBox;     
            } 

    At the end of this method, do I need to add the buildings position to the boundingBox's min and max? If it helps, the game is also scaled by 1/2.
  • 10/30/2009 8:43 PM In reply to

    Re: Boundingbox troubles?

    I wouldn't add that line to the end of that method.  Definitely add it after you get the box back from the method (making it more factory-like).  Also, where are you scaling your models?  If the meshes you are using to create the bounding boxes with are already scaled, then you don't need to scale your box.  However, if they are scaled somewhere else, you should scale your bounding box (by 1/2) before translating it to it's correct position.
  • 10/31/2009 1:10 AM In reply to

    Re: Boundingbox troubles?

    My models are being scaled just before they get drawn; the meshes are not already scaled before then.

    So, essentially, it would follow these steps.

    1) Create my bounding box using the method (unscaled).
    2) Scale the min and max values of the bounding box by 1/2
    3) Add the position of the building to the min and max values after I get the box from the method.

    Did I get that right?


Page 1 of 1 (5 items) Previous Next