1
General / Re: Collision "dont know how"
« on: July 21, 2012, 09:22:33 pm »
That's my way how to check the collision between the player (spaceship) and every asteroid im looping through:
void PlayState::CheckCollisions()
{
std::list<Asteroid>::iterator It;
for(It = asteroids.begin(); It != asteroids.end(); It++)
{
if(It->getPos().x <= playerRect.left + playerRect.width &&
It->getPos().x + It->getRect().width >= playerRect.left &&
It->getPos().y <= playerRect.top + playerRect.height &&
It->getPos().y + It->getRect().height >= playerRect.top) {
// asteroid collides player
It = asteroids.erase(It);
playerLives--;
if (playerLives == 0)
gameOver = true;
Lives.setString("Lives: " + toString(playerLives));
}
}
}
{
std::list<Asteroid>::iterator It;
for(It = asteroids.begin(); It != asteroids.end(); It++)
{
if(It->getPos().x <= playerRect.left + playerRect.width &&
It->getPos().x + It->getRect().width >= playerRect.left &&
It->getPos().y <= playerRect.top + playerRect.height &&
It->getPos().y + It->getRect().height >= playerRect.top) {
// asteroid collides player
It = asteroids.erase(It);
playerLives--;
if (playerLives == 0)
gameOver = true;
Lives.setString("Lives: " + toString(playerLives));
}
}
}