SFML community forums

Help => Graphics => Topic started by: Evaghetti on April 25, 2017, 12:42:05 am

Title: Problem with Rotating a Sprite
Post by: Evaghetti on April 25, 2017, 12:42:05 am
So...i am making a game for my girlfriend (Pipeline) in this games i use tilesets to show the map on screen, in the spritesheet for each type of pipe there's a sprite for it, but i didn't do a seperate sprite for the pipes that are rotated, in theses cases i would rotate them on my code, and that's what i (almost) did, the thing is that my sprite is rotating but getting out of the grid in wich it should be drawn
Here's the code for the constructor of the pipe
Quote
Pipe::Pipe(const int tipoCano, const unsigned dir, const sf::Vector2f pos, const float scale) {
   std::size_t cont = 0;
   ladosPipe = std::bitset<4>(tipoCano);
   switch (ladosPipe.to_ullong()) {
      case 0b1100:
         for(int i = dir; i>0; i--) {
            cont++;
            ladosPipe >> 1;
            if (cont == 4) {
               cont = 0;
               ladosPipe = 0b1100;
            }
         }
         spriteSheet = spriteManager("Images/tilesetCanos.png", 1, 4, 256, 256, 1);
      break;
      case 0b1010:
         for (int i = dir; i>0; i--) {
            cont++;
            ladosPipe >> 1;
            if (cont == 2) {
               cont = 0;
               ladosPipe = 0b1010;
            }
         }
         spriteSheet = spriteManager("Images/tilesetCanos.png", 1, 4, 256, 256);
      break;
      case 0b1000:
         for (int i = dir; i>0; i--) {
            cont++;
            ladosPipe >> 1;
            if (cont == 4) {
               cont = 0;
               ladosPipe = 0b1000;
            }
         }
         spriteSheet = spriteManager("Images/tilesetCanos.png", 1, 4, 256, 256, 3);
      break;
   }
   spriteSheet.aplicarTexturaSprite(sprCano);
   sprCano.setOrigin(128.f, 128.f);
   sprCano.rotate(angle * dir);
   sprCano.setOrigin(0.f, 0.f);
   sprCano.scale(scale, scale);
   sprCano.setPosition(pos);
}
And where i call the constructor
Quote
Mapa::Mapa(char tipo, const sf::Vector2f pos, float scale) : Cano (nullptr) {
   if (tipo == '0') {
      spriteSheet = spriteManager("Images/tilesetCampo.png", 2, 1, 256, 256, 1);
      colocavel = true;
   }
   else if (tipo == '1') {
      spriteSheet = spriteManager("Images/tilesetCampo.png", 2, 1, 256, 256);
      colocavel = false;
   }
   else if(tipo >= '2' && tipo <= '5'){
      Cano = std::make_unique<Pipe>(Pipe(0b1000, 2, pos, 0.3125f));
      spriteSheet = spriteManager("Images/tilesetCampo.png", 2, 1, 256, 256, 1);
      colocavel = false;
   }
   else {
      Cano = std::make_unique<Pipe>(Pipe(0b1000, 0, pos, 0.3125f));
      spriteSheet = spriteManager("Images/tilesetCampo.png", 2, 1, 256, 256, 1);
      colocavel = false;
   }
   spriteSheet.aplicarTexturaSprite(tile);
   tile.setPosition(pos);
   tile.scale(scale, scale);
   this->scale = scale;
}
and last but not least, how it's shown on the screen (the red square is where the pipe should be drawn)
https://goo.gl/photos/BNS69bitmaCmq4Tg8
and the pipes' spritesheet
https://goo.gl/photos/KPiTqkJgSDTVX1H68
can anyone help me find the error?
(sorry if i wrote something wrong, english isn't my native language)
Title: Re: Problem with Rotating a Sprite
Post by: eXpl0it3r on April 25, 2017, 12:46:57 pm
Use a debugger or some simple std::cout to check the values you're setting. Might just be that your expected value is wrong and then you can track the issue from there.
Title: Re: Problem with Rotating a Sprite
Post by: Evaghetti on April 25, 2017, 11:46:57 pm
Found out the bug, it's impossible to rotate a sprite in a specific origin and then change it to another origin so that i can position the sprite, i should use the same origin to do this.
Title: Re: Problem with Rotating a Sprite
Post by: Hapax on April 26, 2017, 09:36:04 pm
Note that the transformations given to a sprite are all performed at the time of drawing it using the latest values set for them.