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

BoundingSphere/BoundingBox Collision

Last post 11/12/2009 12:41 AM by Matt Dent. 5 replies.
  • 11/11/2009 4:05 PM

    BoundingSphere/BoundingBox Collision

    Hello,

    I have a "spaceship" that uses a BoundingSphere to detect collision. It works properly, and when I display it on the screen it is in the right place (updating with the ship's position). I also have a series of buildings that the ship can crash into, and they use BoundingBoxes. These also are in the right position when I draw them. The problem comes when the BoundingSphere of the ship collides with the BoundingBox. For some reason, it only detects collision for one face of the box, not the entire box. So, I was wondering, is there a way to fix that?

    This is the code I use for collision, pretty basic:
    public bool CollidesWith(BoundingBox box) 
        return (sphere.Intersects(box)); 

  • 11/11/2009 9:11 PM In reply to

    Re: BoundingSphere/BoundingBox Collision

    I see no issue with the code for checking the collisions.

    Maybe you are using the wrong points to create the bounding box?  If the points were on the same plane, it would create a bounding box that has only one surface.
  • 11/11/2009 9:26 PM In reply to

    Re: BoundingSphere/BoundingBox Collision

    The code I use to create the BoundingBox looks something like this.

    box = new BoundingBox(); 
     
    box.Min = position; 
    box.Max = box.Min + new Vector3(15049.687f * scale, height, -15049.687f * scale); 

    The buildings are really wide, hence the big numbers. Haha. From what I can see, the position of the building refers to the bottom left corner (on the bottom of the model). So, I don't think they are on the same plane, unless there is something that I'm not seeing.
  • 11/11/2009 11:36 PM In reply to

    Re: BoundingSphere/BoundingBox Collision

    That looks ok too as long as you're sure those values are right, although you may way to try the BoundingBox.CreateFromPoints method (if you have the corner vectors of your buildings).

    One thing to try is to move to a place in the game where you are within the box and then pause the debugger and compare the player's location with the min and max of some of the boxes to at least make sure everything looks ok.
  • 11/12/2009 12:25 AM In reply to

    Re: BoundingSphere/BoundingBox Collision

    Your Z addition should be positive.  The max values should always be greater than each of the min values.
  • 11/12/2009 12:41 AM In reply to

    Re: BoundingSphere/BoundingBox Collision

    Thanks a lot guys! I did what both of you said and they work perfectly now. Never thought to use that method.
Page 1 of 1 (6 items) Previous Next