Share via


Colliding sprites in XNAExtras

The latest release of XNAExtras added support for sprite collisions, and allows the collision detection to be either bounding box (bbox) or pixel based. This is settable per sprite - some sprites can use bboxes and others can use pixels. The sprites will collide appropriately with each other even if they can't agree on how to do it.

Here's a simple tutorial for how to use the collision detection built into XNAExtras:

(1) Create an XSpriteManager.

  m_mgrSprites = new XSpriteManager();

(2) Create an XSprite.

  m_mgrSprites.AddTexture("tex", "texture.png", 64, 64);
XSprite s1 = m_mgrSprites.AddSprite("ball1", "tex");

(3) Create another XSprite so that you have something to collide with.

  XSprite s2 = m_mgrSprites.AddSprite("ball2", "tex");

(4) Enable collisions for these two sprites. For bounding box collisions, use:

  s1.SetCollideable(true);
s2.SetCollideable(true);

If you want pixel based collisions, use:

  s1.SetCollideable(true, XSpriteManager.CollisionFlags.Pixel, 0x40);
s2.SetCollideable(true, XSpriteManager.CollisionFlags.Pixel, 0x40);

where 0x40 is the alpha value of the sprite that defines the collideable part of the sprite - any alpha value >= this value will be added to the sprite's collision mask.

(5) In your Update method, after you've moved your sprites around a bit, you can check for collisions using CheckSpriteCollisions:

  List<XSpriteManager.SpriteCollision> aColl = m_mgrSprites.CheckSpriteCollisions();

This method returns a list of collisions that were detected. It returns null if there are no collisions.

(6) To handle collisions, simply walk the list of collisions and do whatever you feel is appropriate.

  if (aColl != null)
{
// handle the collisions
foreach (XSpriteManager.SpriteCollision c in aColl)
{
// if colliding with another sprite...
if (c.Type == XSpriteManager.CollisionType.Sprite)
{
// handle collision between c.Sprite and c.Sprite2
}
}
}

A working demo of collision detection is given in the CollisionDemo project included with the XNAExtras download.

Comments

  • Anonymous
    September 24, 2006
    Nice work!  Does it handle rotated sprites in this build? [Collision detection for rotated sprites is not yet implemented (unless your sprites are round ^_^). I hope to be adding that for a future release. UPDATE: I didn't notice this when I responded earlier, but it looks like Michael Morton has put a rotated sprite collision tutorial up on his site. Good stuff. If you can't wait for me to do it, you can follow his instuctions and get it working. -GaryKac]

  • Anonymous
    September 27, 2006
    PingBack from http://www.xnatutorial.com/?p=19

  • Anonymous
    October 07, 2006
    Wow. this is great. Thanks for making this!!

  • Anonymous
    October 14, 2006
    i would just like to say thank you for these tools to work with!  they are quite useful. would it be alright if i used them within my submission for the xbox360homebrew competition?  i plan on probably using just the textbox within the menu system i have currently got setup.  it appears to be exactly what i was going to already build, but with some pretty sweet extra features that i'll make use of (like the border).  though, the manager classes are pretty impressive also. thanks!