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

Author Topic: Problems with Get and Set Position after setting Center  (Read 3493 times)

0 Members and 1 Guest are viewing this topic.

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Problems with Get and Set Position after setting Center
« 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 :-)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problems with Get and Set Position after setting Center
« Reply #1 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();
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problems with Get and Set Position after setting Center
« Reply #2 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.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Problems with Get and Set Position after setting Center
« Reply #3 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problems with Get and Set Position after setting Center
« Reply #4 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problems with Get and Set Position after setting Center
« Reply #5 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.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Problems with Get and Set Position after setting Center
« Reply #6 on: May 02, 2011, 10:13:49 pm »
no problem, sfml is very nice anyway :-)
I found an more or less acceptable workaround now.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problems with Get and Set Position after setting Center
« Reply #7 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Problems with Get and Set Position after setting Center
« Reply #8 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problems with Get and Set Position after setting Center
« Reply #9 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything