I'm fairly new to gamedev and have encountered a problem with the collision detection system.
The collision is essentially worked out like this:
for (int x = 0; x < playerprojectiles.Count; x++)
{
for (int i = 0; i < enemyships.Count; i++)
{
Vector2 projectilepos = new Vector2(playerprojectiles[x].position.X, playerprojectiles[x].position.Y);
Vector2 enemypos = new Vector2(enemyships[i].position.X, enemyships[i].position.Y);
Console.WriteLine("I=" + i + " , " + "x=" + x);
if (Vector2.Distance(enemypos, projectilepos) < 20)
{
playerprojectiles.RemoveAt(x);
x--;
break;
}
}
}
I can see the problem; it is having to iterate through too many items. However, I can't think of a way to overcome this.
Any help? To specify, can someone direct me to a method that would allow me to detect collision without impeding performance so severely.
Console.WriteLine()
is the easiest optimisation to make. It is quite expensive and could be called up to 3000 times with those numbers. \$\endgroup\$