Checking collision of bullets and Asteroids
- by Moaz ELdeen
I'm trying to detect collision between two list of bullets and asteroids. The code works fine, but when the bullet intersects with an asteroid, and that bullet passes through another asteroid, the code gives an assertion, and it says about it can't increment the iterator.
I'm sure there is a small bug in that code, but I can't find it.
for (list<Bullet>::iterator itr_bullet = ship.m_Bullets.begin();
itr_bullet!=ship.m_Bullets.end();)
{
for (list<Asteroid>::iterator itr_astroid = asteroids.begin();
itr_astroid!=asteroids.end();
itr_astroid++)
{
if(checkCollision(itr_bullet->getCenter(),itr_astroid->getCenter(),
itr_bullet->getRadius(), itr_astroid->getRadius()))
{
itr_astroid = asteroids.erase(itr_astroid);
}
}
itr_bullet++;
}