I am currently working on adding collision between a Sprite controlled by the player, and the walls of my tile map. At the moment the player can walk on any tile (or no tile at all), so I've been trying to figure out how to differentiate between tiles to no avail. Any help is appreciated, thanks.
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
}
}
Window.clear(sf::Color(0, 0, 0));
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
switch (map[i][j])
{
case 'A':
x = 6;
y = 1;
colMap[i][j] = 1;
break;
case 'F':
x = 5;
y = 1;
colMap[i][j] = 0;
break;
case 'W':
x = 0;
y = 0;
colMap[i][j] = 1;
break;
case 'D':
x = 1;
y = 0;
colMap[i][j] = 0;
break;
case 'U':
x = 8;
y = 29;
colMap[i][j] = 0;
break;
case 'S':
x = 8;
y = 0;
colMap[i][j] = 0;
break;
default:
x = 6;
y = 1;
colMap[i][j] = 1;
};
tiles.setPosition(j * 16, i * 16);
tiles.setTextureRect(sf::IntRect(x * 16, y * 16, 16, 16));
Window.draw(tiles);
}
}
One technique is to check if the sprites of the various entities intersect using their dimensions. If any intersection is detected, a collision is triggered.