# Need help to find error in collision dection in XNA

I really need some help to find the reason why not any of my Asteroids sprites are visible on the screen after I added som code to dedect collision. My suspect is that it has to do in the collision method and that the sprites are removed from the list from the beginning!? The Spaceship is visible.

In my manager class I have this method for checking the collision:

public void CollisionControl(Spaceship spaceShip)
{
for (int i = 0; i < asteroidList.Count(); i++)
{
if (asteroidList.ElementAt(i).Bounds().Intersects(spaceShip.Bounds()))
{
asteroidList.RemoveAt(i);
i--;
}
}
}


And then from the Game1 class I call this manager from the Update Method like this:

 asteroidManager.CollisionControl(spaceship);


And finally in the SpaceShip class and the Asteroids Class I have this code:

// Bounds
public Rectangle Bounds()
{
//return new Rectangle((int)(position.X - orgin.X), (int)(position.Y - orgin.Y), texture.Width, texture.Height);
return new Rectangle(0,0,60,60);
}

// Draw method
public void Draw(SpriteBatch spriteBatch)
{
//spriteBatch.Draw(texture, position, new Rectangle(0,0, (int)texture.Width, (int)texture.Height) , Color.White, rotation, orgin, 1, SpriteEffects.None, 0);
spriteBatch.Draw(texture, position, null, Color.White, rotation, orgin, 1, SpriteEffects.None, 0);
}


Help is really preciated! I'm doing a hand in task that I need to complete soon! Thanks!

## 2 Answers

Do you ever move the bounds? If not, then all the rectangles are always at 0, 0, 60, 60 and always intersecting. You need to move the bounds to the location of your ship/asteroid:

public Rectangle Bounds()
{
int width = 60;
int height = 60;

int x = position.X - (width/2);
int y = position.Y - (height/2);

return new Rectangle(x, y, width, height);
}

• Thanks! You answered first, so I accept your answer! It works perfect! I'm using rotating asteroids and I'm just curious about the bound rectangle, is that also rotating all the time? I guess it's unnecessary to make it complicated, but collision dedection with rotation objects need perhaps another solution? This works, and a detailed collision isn't necessary!? Just need some advice! – 3D-kreativ Jul 13 '12 at 12:05
• If the asteroids rotate, you either need to increase/shrink the bounds with the asteroid or you need to rotate the bounds. Rotating the bounds changes this from an axis-aligned bounding box to an object-aligned bounding box - you can read up on these here. As to whether you need to do it or not - this really depends on the shape of your asteroid. If they're generally circular (like in asteroids) then I'd actually suggest a bounding circle, which won't even need rotating. – Matt Kemp Jul 16 '12 at 10:14

You are not updating your rectangle, however you do not want to be creating it every test so you could when you set up the class create the rectangle.

public Spaceship() {
rectangle = new Rectangle(position.X, position.Y, 60, 60);
}


then call bounds but update x and y before returning

public Rectangle Bounds()
{
rectangle.X = position.X - (width/2);
rectangle.Y = position.Y - (height/2);

return rectangle;
}


this will avoid multiple objects being created and destroyed in your update part of game loop.

• Since Rectangle is a struct (value type) creating Rectangles every update doesn't cause garbage issues. You end up creating a copy anyway when you return the Rectangle from your method. – dadoo Games Jul 13 '12 at 13:18
• Ahh ok I must have been thinking of the java equivalent :), could you provide source of statement? – AbstractChaos Jul 13 '12 at 13:25