// declare an object in the world (floor)
sf::Image floortile;
floortile.LoadFromFile("world_objects/floor.png");
sf::Texture floortileTexture;
floortileTexture.LoadFromImage(floortile);
sf::Sprite floortileSprite(floortileTexture);
floortileSprite.SetPosition(70, 530);
// get the objects width and height
sf::Vector2f floorTileSize = floortileSprite.GetSize();
int floorTileX = floorTileSize.x;
int floorTileY = floorTileSize.y;
/* we now know the following:
The object is 359px width and 81px high.
It is located at 70x530 pixels pixels from the left top of the screen
Our character is located at (for instance) 80x352 from the left top of the screen
this means collision would occure between
pixel 70 untill pixel 429 in width and...
pixel 530 untill pixel 611 in height.
If the current position of the sprite is the same or within the two lines of pixels
set above, collision is occuring and walking speed should be set to 0.
*/
sf::Image floortile;
floortile.LoadFromFile("world_objects/floor.png");
sf::Texture floortileTexture;
floortileTexture.LoadFromImage(floortile);
sf::Sprite floortileSprite(floortileTexture);
floortileSprite.SetPosition(70, 530);
/////////////////////////////////// SPRITE 1 FRAME 1 //////////////////////
sf::Image character1;
character1.LoadFromFile("character_sprites/character_1.png");
sf::Texture charTexture1;
charTexture1.LoadFromImage(character1);
sf::Sprite charSprite1(charTexture1);
charSprite1.SetPosition(80, 352); // do +100
/////////////////////////////////// SPRITE 1 FRAME 2 //////////////////////
sf::Image character2;
character2.LoadFromFile("character_sprites/character_2.png");
sf::Texture charTexture2;
charTexture2.LoadFromImage(character2);
/////////////////////////////////// SPRITE 1 FRAME 3 //////////////////////
sf::Image character3;
character3.LoadFromFile("character_sprites/character_3.png");
sf::Texture charTexture3;
charTexture3.LoadFromImage(character3);
/////////////////////////////////// SPRITE 1 FRAME 4 //////////////////////
sf::Image character4;
character4.LoadFromFile("character_sprites/character_4.png");
sf::Texture charTexture4;
charTexture4.LoadFromImage(character4);
/////////////////////////////////// SPRITE 1 FRAME 5 //////////////////////
sf::Image character5;
character5.LoadFromFile("character_sprites/character_5.png");
sf::Texture charTexture5;
charTexture5.LoadFromImage(character5);
if(r1.x() <= r2.x()+r2.width() && r1.x()+r1.width() >= r2.x() && r1.y() <= r2.y()+r2.height() && r1.y()+r1.height() >= r2.y())
{
// Collision!
}
else
{
// nothing...
}
Please don't revive 5 years old threads.