1

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;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
broncoian2
  • 59
  • 5
  • With this code you make a rectangle, move it, draw it, move it again, draw it again, etc. There is still only one "real" rectangle - you are only checking for collision against the rectangle's final position. If I understand the problem correctly, you probably want `gridArray` to contain `sf::RectangleShape`s instead of `int`s. – 0x5453 Sep 24 '20 at 16:22
  • Ahhhhhh that makes a lot of sense actually thanks! – broncoian2 Sep 24 '20 at 16:30
  • @0x5453 while that might still work out for this particular use case with only 28x36 cells, having an sf::RectangleShape for each individual cell is going to be horrible for performance. A much better solution would be to entirely decouple the collision/game logic from the graphics. – Eric Sep 24 '20 at 16:35
  • @Eric I will do that don't worry, I'm simply trying to gain an understanding of how to perform logic based on what lies ahead of pacman, in this case the overall goal is to prevent him from moving through walls that are supposed to stop him. – broncoian2 Sep 24 '20 at 16:39
  • @Eric Decoupling the graphics from the collision detection is a good idea from a design perspective but It won't necessarily increase performance. Sprites are essentially just AABBs with a pointer to a texture. – Lucas Charbonnier Sep 25 '20 at 06:39
  • @LucasCharbonnier But it allows you to simplify the drawing, for example by using a texture atlas for the tiles and drawing the entire map in one go. You cannot do that if you need the individual RectangleShapes for collision. – Eric Sep 25 '20 at 08:11

0 Answers0