Hey,
So I have got this asteroid type game and today I encountered a new issue while testing this game.
What happened was that two asteroids were close to each other and I shot a bullet at them. The asteroids were so close to each other that a single bullet could collide to both of them.
It collided and my game crashed there itself. I figured out it happened because two asteroids and one bullet collided in the same frame.
This is the code -
```void Collision::DoCollisions(Game *game) const
{
for (ColliderList::const_iterator colliderAIt = colliders_.begin(), end = colliders_.end();
colliderAIt != end;
++colliderAIt)
{
ColliderList::const_iterator colliderBIt = colliderAIt;
for (++colliderBIt; colliderBIt != end; ++colliderBIt)
{
Collider *colliderA = *colliderAIt;
Collider *colliderB = *colliderBIt;
if (CollisionTest(colliderA, colliderB))
{
game->DoCollision(colliderA->entity, colliderB->entity);
}
}
}
}```
```
void Game::DoCollision(GameEntity *a, GameEntity *b)
{
Ship *player = static_cast<Ship *>(a == player_ ? a : (b == player_ ? b : 0));
Bullet *bullet = static_cast<Bullet *>(IsBullet(a) ? a : (IsBullet(b) ? b : 0));
Asteroid *asteroid = static_cast<Asteroid *>(IsAsteroid(a) ? a : (IsAsteroid(b) ? b : 0));
Bullet *bulletMode = static_cast<Bullet *>(IsBulletMode(a) ? a : (IsBulletMode(b) ? b : 0));
if (player && asteroid)
{
player->playerCollided = true;
//AsteroidHit(asteroid);
//DeletePlayer();
}
if (bullet && asteroid)
{
collidedBullets.push_back(bullet);
collidedAsteroid.push_back(asteroid);
//AsteroidHit(asteroid);
//DeleteBullet();
}
if(bulletMode && asteroid)
{
collidedBulletMode.push_back(bulletMode);
collidedAsteroid.push_back(asteroid);
}
}```
```
void Game::CollisionResponse()
{
if(player_->playerCollided == true)
{
DeletePlayer();
}
else
{
if(!collidedAsteroid.empty())
{
for(AsteroidList::const_iterator collidedAsteroidIt = collidedAsteroid.begin(), end = collidedAsteroid.end(); collidedAsteroidIt != end ; ++collidedAsteroidIt )
{
AsteroidHit(*collidedAsteroidIt);
}
collidedAsteroid.clear();
}
if(!collidedBullets.empty())
{
for (BulletList::const_iterator bulletIt = collidedBullets.begin(), end = collidedBullets.end() ; bulletIt!=end; ++bulletIt)
{
DeleteBullet(*bulletIt);
}
collidedBullets.clear();
}
if(!collidedBulletMode.empty())
{
for (BulletList::const_iterator bulletIt = collidedBulletMode.begin(), end = collidedBulletMode.end() ; bulletIt!=end; ++bulletIt)
{
DeleteBulletMode(*bulletIt);
}
collidedBulletMode.clear();
}
}
}```
in my game->docollision() -
whenever an asteroid and a bullet used to collide, the collided objects get collected in collidedasteroids and collidedbullets respectively. When two asteroids collided with the same bullet, the two asteroids got collected safely in collidedAsteroid but the single bullet got collected in collidedBullets twice, so when the deletion was happening, the second time iteration of the bullet couldn't find the respective bullet and it got crashed.
How am I supposed to approach this problem now?
Thanks