Skip to main content
GameDev.net gamedev.net
🔒 Locked

Can't delete elements of vector

Started by recon6 Oct 15, 2007 at 1:00 PM 1 replies 900+ views
Original Post
recon6
recon6
Hi. In the game i've created in SDL, i've got a vector of aliens and a vector of bullets. The vectors allow me to create multiple aliens and bullets. The aliens and bullets are both represented by circles. I've used a collision function which works when one alien object and one bullet object collide. What i'm trying to now do is delete the alien and bullet when they collide using the vectors. This is the collision detection i've used:

//Collision detection----------------------------------------------------------- 
bool Collision(int x1 , int y1 , int r1 , 
                                 int x2 , int y2 , int r2) 
{ 
   int xdiff = x2 - x1;   // x plane difference 
   int ydiff = y2 - y1;   // y plane difference 

   /* distance between the circles centres squared */ 
   int dcentre_sq = (ydiff*ydiff) + (xdiff*xdiff); 

   /* calculate sum of radii */ 
   int sumRadii = r1 + r2; 

   if(dcentre_sq < sumRadii ) 

      return true; 
      else 
      return false; 
} 


Below, i've shown how i've defined the vectors and am trying to delete the alien and bullet when they collide.

vector<Alien>Aliens1; 
vector<Alien>::iterator it1 = Aliens1.begin(); 

vector<Bullet>Bullets; 
vector<Bullet>::iterator it3 = Bullets.begin(); 

if(Collision(it1->getPositionX(),it1->getPositionY(),2.5,it3->getPositionX(), 
        it3->getPositionY(),1)) 
        { 

            iterAlien  = Aliens1.begin(); 
            iterAlien++; 
            Aliens1.erase(iterAlien); 
          
            // Bullets.erase(it1); 
           //  it1--; 
            
        } 


Where getPositionX & Y return the x & y positions of the bullet and alien at any given time. The problem is that firstly the collisions don't even work properly and sometimes the aliens are deleted 3 or 4 at a time and all them eventually get deleted even though the bullet doesn't hit it. if i just create one bullet and one alien object, the following code works.


if(Collision(alien.getPositionX(),alien.getPositionY(),2.5,bullet.getPositionX(),bullet.getPositionY(),1)) 

How can i get an individual alien and bullet from the vector to be destroyed every time they collide? Please let me know if you need to see any of the other code. Thanks.
ToohrVyk
ToohrVyk
Erasing from a vector invalidates all iterators from that vector. erase returns an iterator that comes "right after" the erased element.

You should decouple removal from logic. Have a first pass mark your objects as "dead", and a second pass std::remove_if the marked objects.
TheAdmiral
TheAdmiral
Or, if circumstances allow, you could use a list. Considering that bullets are typically only accessed in a sequential manner, the iterator limitations shouldn't hold you back, and you'd have the luxury of minimal invalidation.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.