I've been looking for articles or tutorials on this and I can't seem to find any, so I thought I'd ask here. I've created a bullet class in my game that is added and removed through a list. If I'm going to make these bullets work the way I want them to I need to be able to do two things:
One, I need to check if the bullets are colliding with each other and then remove the two bullets that are colliding. I've tried doing it by updating the bullet list with a for statement and then checking for collision with another for statement, but that occasionally crashes the game.
Two, I need to be able to check how far away every other bullet in the list is from the current one.
So how can I go about doing this?
Answer
The crash you are getting is probably because you are removing objects from the list while iterating through it. This will modify the Count
of the list and the iterator will try to access elements that no longer exist.
The solution is to use a second list to which you add the objects that must be deleted.
for(int i = 0; i < bulletList.Count - 1; i++)
{
for(int j = i + 1; j < bulletList.Count; j++)
{
if(bulletList[i].CollidesWith(bulletList[j])
{
bulletsColliding.Add(bulletList[i]);
bulletsColliding.Add(bulletList[j]);
}
}
}
for(int i = 0; i < bulletsColliding.Count; i++)
{
bulletList.Remove(bulletsColliding[i]);
}
bulletsColliding.Clear();
Unfortunately, the problem with this is that you can get duplicates of bullets in your bullets colliding list if a bullet is colliding with more than one bullet at a time. Using C# lists the Remove
function will only return false though.
Using flags to set the bullets to Alive
or not can be a good idea to stop you from having to instanciate a Bullet if dead ones aleady exist. I would however just 'move' the bullets into a second list which is neither Draw()
'n nor Update()
'd, and move them back when you need to create a new bullet, and the list isn't empty.
You must be very careful though that the Bullet instance is properly reset before you re-insert it into the working list.
To check the distance between bullets, you can use Pythagoras' theorem. In 2D :
float Distance(Bullet a, Bullet b)
{
float xDistance = b.x - a.x;
float yDistance = b.y - a.y;
float sqDistance = (xDistance * xDistance) + (yDistance * yDistance);
return Math.sqrt(sqDistance);
}
In very time-critical sections, it is common to use squared positions of objects and not perform the square-root operation, as it is quite expensive.
No comments:
Post a Comment