mattyw87

Hi,

I'm pretty new to this and I need to create a bounding box or sphere (i'm not sure which to use) for a few 2D images (which are small circles) and then check to see whether they intersect. I have absoulty no idea how to do this and all the webpages I have viewed have given long winded and complicated explanations!

The images are called whiteBall and redBall and I just need to create a box or sphere around it. I believe I can use an intersect method to then see if they have collided.

Any help is greatly appriciated!

 



Re: XNA Game Studio Express Bounding Box/Sphere

Fluxtah

Well if it helps I can explain how to use XNA's bounding box

The BoundingBox class defines a 3D volume, that takes 2 vectors as parameters min and max, for example:

Vector3 min = new Vector3(-10,-10,-10);
Vector3 max = new Vector3(10,10,10);

BoundingBox bb = new BoundingBox(min, max);

This would create a bounding box, assuming the centre of the box is at 0,0,0  that would have a length, width and height of 20 that you can visualize as follows:-

    //      .--------------.
    //     /|       /|
    //     /       / |
    //    / |      / |
    //    .--------------.<-----Max
    //    |  |     |  |
    //    |       |  |
    //    |  |     |  |
    // Min---|-->. - - - - -| - .
    //    | /      | /
    //    |       | /
    //    |/       |/
    //    .--------------.

Min is the lower left far corner and Max is the upper near right corner of the box.

For 2D all you need to do is zero the min and max Y property.

you can perform a collision test against two bounding boxes as follows:

if(boundingBoxA.Intersects(boundingBoxB))
    // do collision stuff

You do not necessarily need to use the BoundingBox at all you can perform the test yourself (this assumes the origin of the sprite is the upper left corner, hence the adding of width and height):


    public bool Colliding(Sprite a, Sprite b)
    {
      if (a.Position.X + a.Width < b.Position.X) return false;
      if (a.Position.Y + a.Height < b.Position.Y) return false;
      if (a.Position.X > b.Position.X + b.Width) return false;
      if (a.Position.Y > b.Position.Y + b.Height) return false;

      return true;
    }

If your sprites origin is at the centre of the sprite you can define min and max extends like the BoundingBox class has and do something like this:

    public bool Colliding(Sprite a, Sprite b)
    {
      if (a.maxX < b.minX) return false;
      if (a.maxY < b.minY) return false;
      if (a.minX > b.maxX) return false;
      if (a.minY > b.maxY) return false;

      return true;
    }

 

Hope that helps!





Re: XNA Game Studio Express Bounding Box/Sphere

waruwaru

The easiest bounding box would have the same upper-left corner (x,y), width, and height as your sprite/image.  So you can either calculate it on the fly when you need the bounding box (base on the sprite location, width, height).  Or you can have the bounding box pre-calculated, stored with your object, and updated everytime you move your sprite or change size.

To create a bounding sphere (well, a circle in 2D), you need to find the center of your sprite.  Easiest center point calculation is (upper-left-x + (width/2), upper-left-y + (height/2).  When you want to see if 2 spheres intersect, calculate the distance between two circle's center point, and if it's less than the sum of the 2 sphere's radius, then you have collided.

You can tweak the box/sphere by hand if your sprite is oddly shaped.  Or pre-calculate them and store the data in your data structure somewhere.  Hope this helps.

Hah, Fluxtah is too fast for me (even with diagrams). ;)






Re: XNA Game Studio Express Bounding Box/Sphere

mattyw87

Thanks for your replys!

I know I probably sound stupid here but in the methods you have used you used sprite a and sprite b What do I replace sprite a and sprite b with for my program the variable names of the textures whiteBall and redBall

 Also, how do I get the bounding box to position around my sprite on the screen Especially if it is moving with each frame!

Thanks again!





Re: XNA Game Studio Express Bounding Box/Sphere

Fluxtah


I know I probably sound stupid here but in the methods you have used you used sprite a and sprite b What do I replace sprite a and sprite b with for my program the variable names of the textures whiteBall and redBall


Well its just one way of abstracting away from the details, in this case I used Sprite which would be an object that you could define:

public class Sprite
{
    public float MinX;
    public float MaxX;
    public float MinY;
    public float MaxY;
    public void Render();
}

Of course the implementation details are up to you, wether your sprite is a quad, uses SpriteBatch, wether you expose private fields as public properties, wether you encapsulate the min max x and y as Vector2's its entirely up to you ;)

here is an abstract class that I used when I was messing around with 2D, I whipped it up really quickly back then so its nothing special and could do with refactoring to encapsulate the collision logic.


EDIT: Just updated it to include the collision method
  public abstract class Sprite
  {
    private Texture2D _Texture;

    public Texture2D Texture
    {
      get { return _Texture; }
      set { _Texture = value; }
    }

    public Vector2 Velocity;

    public Vector2 Position;

    protected Game Game;

    private SpriteBlendMode _BlendMode;

    public SpriteBlendMode BlendMode
    {
      get { return _BlendMode; }
      set { _BlendMode = value; }
    }

    public float Width;
    public float Height;

    public float CenterX
    {
      get
      {
        return this.Position.X + this.Width / 2.0f;
      }
      set
      {
        this.Position.X = value - this.Width / 2.0f;
      }
    }

    public float CenterY
    {
      get
      {
        return this.Position.Y + this.Height / 2.0f;
      }
      set
      {
        this.Position.Y = value - this.Height / 2.0f;
      }
    }
    public Sprite(Game game)
    {
      Game = game;

    }

    public bool Colliding(Sprite sprite)
    {
      if (this.Position.X + this.Width < sprite.Position.X) return false;
      if (this.Position.Y + this.Height < sprite.Position.Y) return false;
      if (this.Position.X > sprite.Position.X + sprite.Width) return false;
      if (this.Position.Y > sprite.Position.Y + sprite.Height) return false;

      return true;
    }

    public virtual void Update(GameTime gameTime)
    {

    }

    public virtual void Draw(SpriteBatch spriteBatch)
    {
      spriteBatch.Begin(_BlendMode);
      spriteBatch.Draw(Texture, Position, Color.Red);
      spriteBatch.End();
    }
  }

 It probably looks really bad on me, I write business applications for a living and swear by properties, however since using XNA I avoid using properties in hopes to gain more performance, I know! its very bad of me! :(

EDIT: almost missed this one

Also, how do I get the bounding box to position around my sprite on the screen Especially if it is moving with each frame!


As you move the sprite, you would also have to move the bounding box, it may be best to avoid using bounding box and just use the other method where the sprites origin is the top left corner of the sprite.