# Creating A Bounding Box around each item in a list XNA 4.0

I have asked a similar question before except i am more experienced and have more understanding of XNA. I have bullets that shoot whenever you press RightTrigger. I am having trouble though creating bounding boxes around each bullet in the list though. I want to have this so you can shoot the man and when he is hit he will die, except for some reason no bounding boxes are being drawn. I have updated this now because im trying the same method in a platformer after i gave up on the shooter game because i gave up on it because i couldnt get the collision detection right. Now im trying this method again and am having trouble. The thing is, it does work but only on the last ground piece added to the list. Here is the code for the collision detection in the Game1 class:

OLD CODE (DISREGARD)

public bool hitBullet()
{

foreach (Bullets bullet in bullets.bulletAmm)
{
bulletRect = new Rectangle(
(int)bullet.position.X,
(int)bullet.position.Y,
bullet.size.X,
bullet.size.Y);

manRectangle = new Rectangle(
(int)man.position.X,
(int)man.position.Y,
man.pistolTexture.Width,
man.pistolTexture.Height);
}

return bulletRect.Intersects(manRectangle);
}


NEW CODE (PAY ATTENTION)

 public bool HitGround()
{
foreach (Ground newGround in ground.groundAmmount)
{
groundRectangle = new Rectangle(
(int)newGround.position.X,
(int)newGround.position.Y,
newGround.shortTexture.Width,
newGround.shortTexture.Height);

iceBoyRectangel = new Rectangle(
(int)iceBoy.position.X,
(int)iceBoy.position.Y,
iceBoy.texture.Width,
iceBoy.texture.Height);
}

return groundRectangle.Intersects(iceBoyRectangel);
}


now the same method of

    foreach (Bullets bullet in bullets.bulletAmm)


or foreach(Ground newGround in ground.groundAmmount);

works for updating and drawing each bullet which works. but not for the bounding boxes. Any suggestions?

EDIT 1:

i noticed now that this method works sometimes, but only for about 1 in every 20 bullets which makes this even stranger.

EDIT 2:

i thought that i had solved this issue but it turns out i havent. I have updated the code to what it is now.

• Is your code transcription accurate? You recreate bulletRect and manRectangle each iteration yet do nothing with them until after the loop finishes. – Chewy Gumball Feb 21 '13 at 0:47
• Take a look at your loop again. You only check for intersection of the last item in the list. – scable Feb 25 '13 at 0:51
• @daddz yes that was it, thanks. i had to create a seperate bool variable and declare it as true if the intersected inside the loop than return the value of that variable. – Battle_Pasture Feb 25 '13 at 1:02