I'm having an issue with my collision detection code. I'm making a top down shooter and when I shoot, the bullet will only destroy an enemy if it's the closest enemy. The bullet will go right through the 2nd closest enemy.
Here is my code:
// Iterate through bullets, if bullet ccollides with enemy, remove both
for (int i = 0; i < Bullet.Bullets.Count; i++)
{
if (Collisions.IsColliding(
(int)Bullet.Bullets[i].position.X, (int)Bullet.Bullets[i].position.Y,
Bullet.sprite.Width, Bullet.sprite.Height,
(int)Enemy.Enemies[i].position.X, (int)Enemy.Enemies[i].position.Y,
Enemy.sprite.Width, Enemy.sprite.Height))
{
Enemy.Remove(Enemy.Enemies[i]);
Bullet.Remove(i);
}
}
And here is my is colliding method:
public static bool IsColliding(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
return x1 < x2 + w2 &&
x2 < x1 + w1 &&
y1 < y2 + h2 &&
y2 < y1 + h1;
}
Can anyone please point me in the right direction? Thank you.
Bullet.Bullets
? What type of collection is it? \$\endgroup\$Bullets
\$\endgroup\$