Log in Page Discussion History Go to the site toolbox

XNA Tutorials/CollisionDetection

From BluWiki

The Second Sprite and Collision Detection

Adding A Second Sprite

Adding a Second Sprite is exactly like adding the first.

Declare the texture within the class.

 Texture2D myTexture2;

Next, declare the position of the second sprite.

 Vector2 spritePosition2 = new Vector2(600.0f, 750.0f);

In this case the vector created is located in the lower right side of the window. StartingPostionsPictures.png

Now load the content for each Sprite inside the LoadContent function.

protected override void LoadContent()
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myTexture = Content.Load<Texture2D>("linksprite1");
    myTexture2 = Content.Load<Texture2D>("linksprite1");

Add some information about the Sprite’s Motion with a class-level variable.

Vector2 spriteSpeed2 = new Vector2(-250.0f, -300.0f);

Be sure to update the Sprite within the UpdateSprite function.

SpritePosition2 += spriteSpeed2 * (float)gameTime.ElapsedGameTime.TotalSeconds;

Within the UpdateSprite function be sure to have your sprite check for the form boundaries. If your sprites are different sizes, be sure to include a new minimum and maximum X and Y.

             int MaxX2 = graphics.GraphicsDevice.Viewport.Width - myTexture2.Width;
             int MaxY2 = graphics.GraphicsDevice.Viewport.Height - myTexture2.Height;
            // Check sprite2 for window boundaries
           if (spritePosition2.X > MaxX2)
               spriteSpeed2.X *= -1;
               spritePosition2.X = MaxX2;
               myTexture2 = LinkNormal;
           else if (spritePosition2.X < MinX)
               spriteSpeed2.X *= -1;
               spritePosition2.X = MinX;
               myTexture2 = LinkNormalFlip;
           if (spritePosition2.Y > MaxY2)
               spriteSpeed2.Y *= -1;
               spritePosition2.Y = MaxY2;
           else if (spritePosition2.Y < MinY)
               spriteSpeed2.Y *= -1;
               spritePosition2.Y = MinY;
           else if (spritePosition2.Y < MinY)
               spriteSpeed2.Y *= -1;
               spritePosition2.Y = MinY;

Finally, we need to draw the second Sprite based on its own position. This is done within the Draw function.

protected override void Draw(GameTime gameTime)
    spriteBatch.Draw(myTexture, spritePosition, Color.White);
    spriteBatch.Draw(myTexture2, spritePosition2, Color.White);

Bounding Box

Now that the second sprite has been successfully added, it is time to detect collisions. In order to do this, a bounding box structure will be utilized. The Bounding Box Structure represents the space occupied by a box. Each face of the bounding box is perpendicular to the x-axis, the y-axis, or the z-axis. The Bounding Box has several helpful Public Members, but the one we will be using is the Intersects method.

First, declare the bounding box structure. This structure must be recreated every time the update function is called in order to continue to surround the moving sprite. Therefore, it is best to initialize the box within the update function itself. The BoundingBox constructor takes two Vector3's as parameters. These two Vector3's represent the three-dimensional coordinates of the opposite corners of a three-dimensional box.

BoundingBox b1 = new BoundingBox(new Vector3(x1,y1,z1), new Vector3(x2,y2,z2));

For 2D objects, set z1 and z2 to 0. To make the bounding box exactly cover your 2D sprite, set x1 and y1 like this:

 x1 = spritePosition.X;
 y1 = spritePosition.Y;

and set x2 and y2 like this:

 x2 = x1 + myTexture.Width;
 y2 = y1 + myTexture.Height;

where myTexture is the name of the Texture2D object for the sprite whose bounding box you are creating. If your second sprite is a different size, the x2 and y2 values will be different, using the second sprite's myTexture2 object.

Collision Detection

Once you have created your bounding boxes around your Sprites, a collision detection is very easy to implement using the Intersects method.

 protected bool Collision(BoundingBox b1 , BoundingBox b2)
       return b1.intersects(b2)

This function will return true if any part of bounding box b1 overlaps any part of bounding box b2. Once this function has been successfully implemented virtually anything can be added at the detection point.

Adding Some Extras

Now it is time to start adding some features upon collision detection. This is done inside the UpdateSprite Function, after you have checked for collisions with the walls.

           if (Collision(spritePosition, spritePosition2))
               spriteSpeed.X *= -1;
               spriteSpeed.Y *= -1;
               spriteSpeed2.X *= -1;
               spriteSpeed2.Y *= -1;
               mySpritePicture == myNewSpritePicture;

In this example, we have inverted the x and y speed of both sprites, played a sound effect, and changed one of the sprites pictures.


As you can see from the pictures, the Link Sprite's picture changes to a Link with a sword and the creature sprite's picture is inverted. Both sprites begin moving in opposite directions after the collision and a sword sound effect is played.

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 10 September 2009, at 19:26.
Disclaimers - About BluWiki