XNA collision detection between Lists

I have 2 lists, one is for meteors and second is for bullets. I have problem with collision detection between them, this is my code for meteor spawning:

      public void LoadMeteors()
{

int randY = random.Next(100, 400);
if (spawn >= 1)
{
spawn = 0;
if (meteors.Count() < 10)

}

for (i = 0; i < meteors.Count; i++)
{
//here i check the collision between bullets and meteors.
if (meteors[i].metRectang.Intersects(bullets[k].pulkaRectangle))
{
meteors[i].isVisible = false;
}
if (!meteors[i].isVisible)
{
meteors.RemoveAt(i);
i--;
}

}
}


and that is my bullet spawn:

    public void Start()
{
//interval timing

for (k = 0; k < meteors.Count; k++)
{

if (!bullets[k].isVisible)
{
bullets.RemoveAt(k);
i--;

}
}
}
public void SpawnBullet()
{
bullets.Add(new Bullet(Content.Load<Texture2D>("Backgrounds/pula"), new Vector2(rocket.rectangle.X+20, rocket.rectangle.Y + 20), new Rectangle(0, 0, 10, 5)));
soundEffect.Play();
}


and somwhere there is spawnBullet(); (there was only timer for interval nothing special) So, when i start game in few seconds i get this error: ....and thanks for any help!

• K should run from 0 to bullets.count, not meteors.count – Peethor Dec 29 '15 at 8:30

You are removing elements while iterating over a collection

Even though you move an element backwards after the removal, the size of your list shrinks after you removed a meteor. The for loop is still set to iterate for the initial Count returned by the list. Therefore, it will exceed the boundaries of the list once a meteor has been removed.

I'd generally not recommend deleting elements from a collection in the first place, but you can instead iterate backwards. I.e.:

 for (i = meteors.Count; i >= 0; i--)
{
//here i check the collision between bullets and meteors.
if (meteors[i].metRectang.Intersects(bullets[k].pulkaRectangle))
{
meteors[i].isVisible = false;
}
if (!meteors[i].isVisible)
{
meteors.RemoveAt(i);
}
}


This solves the issue of the value of count changing. The same goes to what you are doing in your Start method with the bullets.

However, I don't think the code does what you desire. You are checking collision between meteor i and bullet k, but the value of k never changes (in this loop). That means you are only checking one bullet against all the meteors.

And in addition to that, I doubt LoadMeteors is being called every frame. Since that method (from a logical standpoint) is only executed once, collision would only occur if a bullet and a meteor spawn on the same position.

EDIT: One other thing: as mentioned in the comment, in your other Start method, where you are iterating over the bullets, you run until meteors.Count rather than bullets.Count

• hey, yes i call Loadmeteors() every frame in update method, also i changed my code as you said but, i get same error – Nick Dec 29 '15 at 11:58
• and, could you please tell me, how can i check all bullets against all meteors? – Nick Dec 29 '15 at 12:01
• You'd want a nested for loop (a for loop inside another). The first iterates over all the bullets, the inner for loop loops over all the meteors (or vice versa). Within that, you check for the collisions – Athos vk Dec 29 '15 at 12:40
• public void check() { for (int j = 0; j < bullets.Count; j++) { for (int l = 0; l < meteors.Count; l++) { if (meteors[l].metRectang.Intersects(bullets[j].pulkaRectangle)) { meteors[l].isVisible = false; } } } } – Nick Dec 29 '15 at 13:00
• that one is not working, i call check(); in update method – Nick Dec 29 '15 at 13:00