Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite In For Loop Not Rotating Around the Center  (Read 1622 times)

0 Members and 1 Guest are viewing this topic.

maxwell79

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Sprite In For Loop Not Rotating Around the Center
« on: April 23, 2015, 01:05:40 am »
Hi there,

I can't seem to understand why my sprite is not rotating around it's center even after adjusting the origin.
In the code below, the first For loop chooses a particular texture, while the inner For loop draws the sprite multiple times at different positions but with random rotations. The drawn image gives incorrect positions.

Any suggestions would be appreciated.
 for (const auto& resource : list) {
    const auto& id = resource.mId;
    const auto& cells = resource.mCells;

    sf::Sprite sprite(mResources.get(id));
    const float w { sprite.getGlobalBounds().width };
    const float h { sprite.getGlobalBounds().height };
    const float r { h / w };
   
    const float drawWidth =  w > 3 * mLiAlpha ?
      2.0f * mLiAlpha : w;
   
    const float drawHeight = h > 3 * mLi ?
      r * drawWidth : h;
   
    float widthScale  { drawWidth / w };
    float heightScale { drawWidth / h };
     
      sprite.setScale(widthScale, heightScale);
      for (auto& i : cells) {
          sprite.setOrigin
            (sprite.getGlobalBounds().width  / 2.0f,
             sprite.getGlobalBounds().height / 2.0f);
          sprite.setRotation(rand(eng));
          sprite.setOrigin(0,0);
       
          sprite.setPosition(
                           mCellCentres[i].mXc -
                           sprite.getGlobalBounds().width / 2.0f,
                           mCellCentres[i].mYc -
                           sprite.getGlobalBounds().height / 2.0f);
          texture->draw(sprite);
     }
  }
  texture->display();
  return std::unique_ptr<sf::RenderTexture>(texture);
 
« Last Edit: April 23, 2015, 02:12:21 am by maxwell79 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Sprite In For Loop Not Rotating Around the Center
« Reply #1 on: April 23, 2015, 02:09:43 am »
Try again with using the [code=cpp][/code] tags and a compilable and minimal example.

Also this code of yours does? ;)
sprite.setOrigin(sprite.getGlobalBounds().width  / 2.0f, sprite.getGlobalBounds().height / 2.0f);
sprite.setOrigin(0,0);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite In For Loop Not Rotating Around the Center
« Reply #2 on: April 23, 2015, 08:00:41 am »
The transformations are stored separately in sf::Sprite (or rather its base class sf::Transformable), and applied in a fixed order when it is drawn. So calling setOrigin + setRotation + setOrigin has no effect, in the end only the last set origin will be used.

Use a sf::Transform if you want to be able to combine transformations in a custom way. And read the documentation and tutorials carefully, this kind of details is usually well explained ;)
Laurent Gomila - SFML developer