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

Author Topic: Sprite not centering properly - conflicts with Rotate...?  (Read 1950 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite not centering properly - conflicts with Rotate...?
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite not centering properly - conflicts with Rotate...?
« Reply #1 on: February 05, 2012, 07:08:33 pm »
Try this:
Code: [Select]
m_Sprite.SetPosition(m_RealPosition);
Laurent Gomila - SFML developer

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sprite not centering properly - conflicts with Rotate...?
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite not centering properly - conflicts with Rotate...?
« Reply #3 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.
Laurent Gomila - SFML developer

CL90

  • Newbie
  • *
  • Posts: 12
    • View Profile
Sprite not centering properly - conflicts with Rotate...?
« Reply #4 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.