Welcome to our first article of "How To Use ...?" Series.I will be writing more than 300 articles for this series.So keep following XNA Section in our community.So lets talk about Bounding Box and How To Use it!
BoundingBox is a structure which defines an axis-aligned box-shaped 3D volume.
BoundingBox is a structure which defines an axis-aligned box-shaped 3D volume.
The BoundingBox Structure represents the space occupied by a box. The bounding box class is axis aligned. Each face of the bounding box is perpendicular to the x-axis, the y-axis, or the z-axis.
There are several benefits of using the bounding box for collision detection.
- The bounding box class fits rectangular shapes aligned with the axis very well. Compared to the bounding sphere class, the bounding box class provides a much tighter fit for non-rotated rectangular objects.
- Because the bounding box class is axis aligned, you can make certain assumptions that result in collision checks between bounding boxes being quicker than a bounding box that can be rotated.
There are a few drawbacks of using the bounding box for collision detection.
- Rotating a bounding box causes it to no longer be axis aligned. Because of this, if you rotate a model being bounded, you will need to recreate the bounding box. Doing so can be slow, since all the points in an object are iterated through to get the bounding box. If the model has not changed orientation, you can translate the bounding box instead of recreating it.
- If the model being bounded is not aligned to the axis, the bounding box will have some empty space. The amount of empty space will be greatest when the object is rotated 45 degrees from an axis.
- Empty space in the bounding box can result in false positives when checking for collision.

A sphere-like shape is surrounded by a BoundingBox.BoundingBox is used for Collision Detection processes so in our example i will show you how you can use BoundingBox.
Here is a general Collision Detection code:
void CheckForCollision()
{
BoundingBox bb1 = new
BoundingBox(new
Vector3(spritePosition1.X - (sprite1Width /
2), spritePosition1.Y - (sprite1Height / 2), 0), new
Vector3(spritePosition1.X + (sprite1Width /
2), spritePosition1.Y + (sprite1Height / 2), 0));
BoundingBox bb2 = new BoundingBox(new Vector3(spritePosition2.X - (sprite2Width / 2), spritePosition2.Y - (sprite2Height / 2), 0), newVector3(spritePosition2.X + (sprite2Width / 2), spritePosition2.Y + (sprite2Height / 2), 0));
if (bb1.Intersects(bb2))
{
//Do Something if they crash
}
}
Ok
We will have 2 ships in our sample game.
As you can see it is about Running-Away from a Pirate ship.
Ok here are the codes.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace BoundingSample
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture1;
Texture2D texture2;
Vector2 spritePosition1;
Vector2 spritePosition2;
Vector2 spriteSpeed1 = new
Vector2(50.0f, 50.0f);
Vector2 spriteSpeed2 = new
Vector2(100.0f, 100.0f);
int sprite1Height;
int sprite1Width;
int sprite2Height;
int sprite2Width;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteBatch = new SpriteBatch(GraphicsDevice);


Join the conversation! Your thoughts help the community grow.