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

Author Topic: Problem with setCenter() and setRotation()  (Read 1079 times)

0 Members and 1 Guest are viewing this topic.

mastermine

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with setCenter() and setRotation()
« on: October 27, 2014, 03:29:28 pm »
Hi there,

i am just coding a simple model-library and i have some problems with rotating the sprite by it's center.
I think the problem is somewhere by setCenter, it doesn't matter if i set the center to a new position or let it by (0, 0), it always rotates by the point (0, 0).

Here's the code:
bool ModelStatic::setRotation(Rotation rotation){
    if(!this->md_hasRotation[rotation] && this->md_perspective != Perspective::bird){
        cout << "[EIMODEL] Failed to set rotation " << rotation << " for model '" << this->md_name << "', rotation is not set for this model!" << endl;
        return false;
    }
    this->md_rotation = rotation;
       
    this->md_spBirdPerspective.setOrigin(this->md_spBirdPerspective.getGlobalBounds().width  / 2, this->md_spBirdPerspective.getGlobalBounds().height / 2);
    this->md_spBirdPerspective.setRotation((UINT)this->md_rotation * 90.0f);
    this->md_spBirdPerspective.setOrigin(0, 0);
       
    this->setSize(Vector2f(this->md_spBirdPerspective.getGlobalBounds().width, this->md_spBirdPerspective.getGlobalBounds().height));
       
    this->adjustHitbox();        
    return true;
}
 

Thanks in advance  ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with setCenter() and setRotation()
« Reply #1 on: October 27, 2014, 03:38:58 pm »
The transformations are not applied as you call the setXxx functions, they are applied when the object is drawn. So no matter how many times, and in which order, you call them, only the last value (for each transformation) will be used, and always in the same order since this order is fixed in SFML.

So, you can't rotate around a point while keeping the default origin. If you change the origin, all the other transformations will be impacted by it.

If you want more flexibility (ie. the ability to combine any transformation in any order), you must use the sf::Transform class.
Laurent Gomila - SFML developer

mastermine

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with setCenter() and setRotation()
« Reply #2 on: October 27, 2014, 04:10:35 pm »
Thank you, Laurent  :D

I have now changed my code here and there and it's now working fine ^^

~CLOSED