I'm trying to check for collisions against a shape stored in a 2D array.
I'm trying to use for logic in a Pacman clone I'm creating. I have no issue detecting collisions between two sprites, however I cannot seem to get it to detect a collision between the Pacman sprite and the rectangle shape. How can I get a sprite to collide with any rectangle in an array, and how would I be able to check the value of the tile the sprite collides with. Thanks!
const int rows = 28; //width
const int columns = 36; //height
int gridArray[rows][columns];
gridArray[6][3] = 1; //I want to check if the rectangle I collide with is = to 1, then do logic.
sf::RectangleShape grid1;
grid1.setFillColor(sf::Color::Transparent);
grid1.setOutlineColor(sf::Color::White);
grid1.setOutlineThickness(.5f);
grid1.setSize(sf::Vector2f(16, 16)); //each square is 16 x 16
int positionx = -16;
int positiony = 0;
for (int i = 0; i < 28; i++)
{
positionx = positionx + 16;
positiony = 0;
for (int j = 0; j < 36; j++)
{
grid1.setPosition(positionx, positiony);
window.draw(grid1);
positiony = positiony + 16;
}
}
window.draw(pacman->pacManSprite);
if (pacman->pacManSprite.getGlobalBounds().intersects(grid1.getGlobalBounds())) //me attempting to detect collision of ANY rectangle.
{
pacman->canMove = false;
std::cout << "UGHGHGHGHG" << std::endl;
}