SFML community forums

Help => Graphics => Topic started by: GarrickW on February 05, 2012, 06:35:40 pm

Title: Sprite not centering properly - conflicts with Rotate...?
Post by: GarrickW on February 05, 2012, 06:35:40 pm
Note: This is for SFML 1.6.

So I have a sprite, which I want to be centered on the real position of the object it represents.  I center it by calling the following:

Code: [Select]
       m_Sprite.SetSubRect(sf::IntRect(0, 65, 65, 86));
        m_Sprite.SetPosition(m_RealPosition.x - (m_Sprite.GetSize().x / 2), m_RealPosition.y - (m_Sprite.GetSize().y / 2));
        m_Sprite.SetCenter(m_Sprite.GetSize().x / 2, m_Sprite.GetSize().y / 2);


This apparently doesn't work.  I discovered this by creating an sf::Shape::Circle at the place represented by m_RealPosition, as such:

Code: [Select]
       m_Marker = sf::Shape::Circle(m_RealPosition.x,
                                     m_RealPosition.y,
                                     5,
                                     sf::Color(255, 0, 0));


The Sprite does its thing left of, and slightly above, the red circle it is supposed to be centered on.  I rotate the Sprite very frequently using sf::Sprite::SetRotation, so I'm wondering if that is causing the problem.

I should note that the object in question inherets from an abstract class that has this in its constructor (the above code is being called every frame after construction, despite any inefficiency; m_Sprite from above is inherited from this abstract class):

Code: [Select]
   m_Sprite.SetPosition(m_RealPosition.x - (m_Sprite.GetSize().x / 2),
                         m_RealPosition.y - (m_Sprite.GetSize().y / 2));

    m_Sprite.SetCenter(m_Sprite.GetSize().x / 2,
                       m_Sprite.GetSize().y / 2);


So my question is basically this: are there any conflicts between SetCenter, SetRotation, SetPosition and SetSubRect that I should be aware of?  Or can anyone else see what I can't?  Why would my Sprite not be centering on m_RealPosition?

Thanks for any help anyone can offer!
Title: Sprite not centering properly - conflicts with Rotate...?
Post by: Laurent on February 05, 2012, 07:08:33 pm
Try this:
Code: [Select]
m_Sprite.SetPosition(m_RealPosition);
Title: Sprite not centering properly - conflicts with Rotate...?
Post by: GarrickW on February 05, 2012, 07:21:05 pm
Perfect!  That works marvelously.

Out of curiosity, why does the parameter to SetPosition() become the Sprite's center, and not its upper left corner?  I thought the upper-left was always what was being set by SetPosition().  Is it something to do with the inheritance?

Edit: ... It's because SetPosition() sets the position of the sprite's center, as determined by SetCenter() and which is normally (0,0).  Right?
Title: Sprite not centering properly - conflicts with Rotate...?
Post by: Laurent on February 05, 2012, 09:02:22 pm
Quote
It's because SetPosition() sets the position of the sprite's center, as determined by SetCenter() and which is normally (0,0). Right?

Yes.
Title: Sprite not centering properly - conflicts with Rotate...?
Post by: CL90 on February 06, 2012, 12:34:04 pm
Hi!

may i can help you^^
i have had the problem befor about 30mins.

my image is (because i thought of center) is: 101x101 pixels.
dont know if images with a pair pixelcwidth can be centered well.

so i center it like this:
Code: [Select]
   ship.SetPosition(100.f, 100.f);
ship.SetCenter(50,50);


my image rotates clearly around the 100x 100y pixel.