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

Author Topic: sf::Tranformable setOrigin  (Read 1638 times)

0 Members and 1 Guest are viewing this topic.

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
sf::Tranformable setOrigin
« on: May 27, 2023, 08:31:48 pm »
We have 2 overloads of sf::setOrigin:

setOrigin (float x, float y)
setOrigin (const Vector2f& origin)
 

There is another useful overload for getting origin to the center of the shape as it is quite useful as a lot of transformation has sence about center of the entity. For that it would be useful simple function without parameter. Or it could be through enum but it seems to me that function is used quite a lot and it would be really useful having it directly in the class.
setOrigin()
 

SFML has several types of entities:
CircleShape - it is easy by the radius
RectangleShape - it is easy by the size
ConvexShape - average of points
Sprite - from rectangle
Text - from its bounds

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Tranformable setOrigin
« Reply #1 on: May 27, 2023, 11:27:37 pm »
I think that because it's easy to do, it probably doesn't necessarily need to be in SFML itself. Who knows though?!

For a 'catch all', you can use the bounds for all of them:
template <class T>
void centerOrigin(T& object)
{
    const sf::FloatRect bounds{ object.getLocalBounds() };
    object.setOrigin({ bounds.left + (bounds.width / 2.f), bounds.top + (bounds.height / 2.f) });
}

That should work for all SFML objects and uses their actual bounds to calculate the centre.

It's also exactly how I did it in Plinth, along with other origin calculations...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: sf::Tranformable setOrigin
« Reply #2 on: May 29, 2023, 09:07:30 pm »
Yes, it works universally and that is probably the reason why not have it included in SFML as for convex shapes, bounding box center can be different from center of convex hull (center of mass).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Tranformable setOrigin
« Reply #3 on: May 30, 2023, 01:44:48 pm »
For shapes we've just merged getGeometricCenter(), which will be part of the still in development SFML 3.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything