SFML community forums

Help => Graphics => Topic started by: P@u1 on May 02, 2011, 09:13:11 pm

Title: Problems with Get and Set Position after setting Center
Post by: P@u1 on May 02, 2011, 09:13:11 pm
Hi everyone,

I'm facing huge problems with getting and setting the position of a sprite after I set the center.
I know that there are methods to transform to global/local coordinates, but I think that these methodes are not good here, because they also apply rotating and more things, which should not be taken into consideration.

By default the center is on the top left corner (0,0), but I need to set it to the real center (width/2, height/2) so that rotation works the way I need it.

But when I then want to get/set the position I get lots of problems, because the positions I get / set seem to be no longer the left top corner, but the middle (the real center).
I tried to handle this with methods which subtract the half of the center before getting/setting, but this doesent work good, because if i for example use somethign like

Code: [Select]
setPosition(getPosition); //modified setters/getters which subtract half of center

It will do strange changes...

I'm a bit confused, maybe I have to substract for getting and add for setting or the other way round?
There must be some better approches...

Please help :-)
Title: Problems with Get and Set Position after setting Center
Post by: Nexus on May 02, 2011, 09:20:21 pm
When you set the origin to the sprite's center, you do want to get the position at this point. That's the meaning of origin, i.e. this behavior is a feature.

In case you still need to access the left-upper corner, just subtract the origin from the position:
Code: [Select]
sf::Vector2f GetLeftUpperCorner(const sf::Drawable& drawable)
{
    return drawable.GetPosition() - drawable.GetOrigin();
}
Title: Problems with Get and Set Position after setting Center
Post by: Laurent on May 02, 2011, 09:21:55 pm
It is indeed the position of the center that you set with SetCenter. In other words, the center is the origin for all transformations: translation, rotation and scaling.

So yes, if you still want the position to be the top-left corner, you must subtract the half-size after calling GetPosition() and add it before you call SetPosition.
Title: Problems with Get and Set Position after setting Center
Post by: P@u1 on May 02, 2011, 09:28:20 pm
ok, I got it working now with subtracting / adding half of the size, but I don't really like this.
Actually I only want to change the center for rotation, for nothing else...

@Nexus: I can't find the GetOrigin() method, is it maybe a new SFML 2.0 feature?
Title: Problems with Get and Set Position after setting Center
Post by: Nexus on May 02, 2011, 09:29:53 pm
Just to avoid confusion: In SFML 1, the methods are called SetCenter/GetCenter, while SFML 2 uses SetOrigin/GetOrigin. The term "origin" is more expressive in this context.

And I would subtract the origin rather than the half-size from the position, because this works also if the origin is not at the center of the sprite.
Title: Problems with Get and Set Position after setting Center
Post by: Laurent on May 02, 2011, 09:57:10 pm
Quote
ok, I got it working now with subtracting / adding half of the size, but I don't really like this.
Actually I only want to change the center for rotation, for nothing else...

SFML doesn't allow this, sorry.
Title: Problems with Get and Set Position after setting Center
Post by: P@u1 on May 02, 2011, 10:13:49 pm
no problem, sfml is very nice anyway :-)
I found an more or less acceptable workaround now.
Title: Problems with Get and Set Position after setting Center
Post by: Nexus on May 02, 2011, 10:19:43 pm
I don't really see the problem... Just define your own global function instead of using sf::Sprite::GetPosition(). This is not a workaround, this is the usual way to implement advanced functionality on top of an existing class with a basic, given interface.
Title: Problems with Get and Set Position after setting Center
Post by: P@u1 on May 03, 2011, 08:13:13 pm
Quote from: "Nexus"
I don't really see the problem... Just define your own global function instead of using sf::Sprite::GetPosition(). This is not a workaround, this is the usual way to implement advanced functionality on top of an existing class with a basic, given interface.


That's what I did, but it can still easily happen that i accidentily use the normal methods.
Title: Problems with Get and Set Position after setting Center
Post by: Nexus on May 03, 2011, 10:38:11 pm
Yes, another option is to write a wrapper (adapter) around sf::Sprite. But I think that's not worth the effort, since the accidental use of sf::Sprite::GetPosition() probably occurs rarely.