Skip to main content
Code formatting, for legibility
Source Link
Trevor Powell
  • 21.5k
  • 1
  • 63
  • 96

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++);   {itr_astroid++)   {  if(checkCollision(itr_bullet->getCenter(),itr_astroid->getCenter(),  itr_bullet->getRadius(), itr_astroid->getRadius()))   {    itr_astroid = asteroids.erase(itr_astroid);   }   }     itr_bullet++;  } 

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++;  } 

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++; } 
Source Link
Ahmed Saleh
  • 129
  • 1
  • 11

Checking collision of bullets and Asteroids

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++; }