Cześć,
piszę grę Arkanoid i jestem już prawie ją skończyłem,jedank męczy mnie jeden problem.Moje cegiełki są ustawione jedna obok drugiej i gdy moja piłka dotyka dwie. lub trzy cegiełki naraz,dochodzi do znisczenia wszystkich cegiełek.Chcę sprawić,aby piłka mogła za jednym razem zniszczyć tylko jedną cegiełkę, o to kod kolizji z przykładową cegiełką:
void Engine::collisions(RectangleShape &rec, CircleShape &ball, RectangleShape brick[], int &toWin, sf::Vector2f &pos, Sprite &won)
{
sf::FloatRect box1(rec.getGlobalBounds());
sf::FloatRect box2(ball.getGlobalBounds());
sf::FloatRect box3(brick[0].getGlobalBounds());
sf::FloatRect box4(brick[1].getGlobalBounds());
sf::FloatRect box5(brick[2].getGlobalBounds());
sf::FloatRect box6(brick[3].getGlobalBounds());
sf::FloatRect box7(brick[4].getGlobalBounds());
sf::FloatRect box8(brick[5].getGlobalBounds());
sf::FloatRect box9(brick[6].getGlobalBounds());
sf::FloatRect box10(brick[7].getGlobalBounds());
sf::FloatRect box11(brick[8].getGlobalBounds());
sf::FloatRect box12(brick[9].getGlobalBounds());
sf::FloatRect box13(brick[10].getGlobalBounds());
sf::FloatRect box14(brick[11].getGlobalBounds());
sf::FloatRect box15(brick[12].getGlobalBounds());
sf::FloatRect box16(brick[13].getGlobalBounds());
sf::FloatRect box17(brick[14].getGlobalBounds());
sf::FloatRect box18(brick[15].getGlobalBounds());
sf::FloatRect box19(brick[16].getGlobalBounds());
sf::FloatRect box20(brick[17].getGlobalBounds());
ball.move(pos.x, pos.y);
//paddle collision
if (box1.intersects(box2))
{
//pos.x = -pos.x;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y - 2);
pos.y = -pos.y;
}
//left side collision
else if (ball.getPosition().x <= 0)
{
pos.x = -pos.x;
//pos.y = -pos.y;
}
//right side collision
else if (ball.getPosition().x >= 960)
{
pos.x = -pos.x;
//pos.y = -pos.y;
}
//top side colliosn
else if (ball.getPosition().y <= 0)
{
//pos.x = -pos.x;
pos.y = -pos.y;
}
//bricks collision
else if (box3.intersects(box2) && canDestroy==true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[0].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box4.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[1].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box5.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[2].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box6.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[3].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box7.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[4].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box8.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[5].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box9.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[6].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box10.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[7].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box11.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[8].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box12.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[9].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box13.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[10].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box14.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[11].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box15.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[12].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box16.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[13].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box17.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[14].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box18.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[15].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box19.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[16].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
else if (box20.intersects(box2) && canDestroy == true)
{
canDestroy = false;
pos.x = -pos.x;
pos.y = -pos.y;
ball.setPosition(ball.getPosition().x - 1, ball.getPosition().y + 1);
brick[17].setPosition(1000, 1000); toWin++;
canDestroy = true; sound.play();
}
}