Original Post
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: Below, i've shown how i've defined the vectors and am trying to delete the alien and bullet when they collide. 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. 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.
//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;
}
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--;
}
if(Collision(alien.getPositionX(),alien.getPositionY(),2.5,bullet.getPositionX(),bullet.getPositionY(),1))