SFML community forums

Help => Graphics => Topic started by: Garwin on May 27, 2023, 08:31:48 pm

Title: sf::Tranformable setOrigin
Post by: Garwin 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
Title: Re: sf::Tranformable setOrigin
Post by: Hapax 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... (https://github.com/Hapaxia/Plinth/blob/master/Plinth/Sfml/Anchor.inl#L68-L73)
Title: Re: sf::Tranformable setOrigin
Post by: Garwin 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).
Title: Re: sf::Tranformable setOrigin
Post by: eXpl0it3r on May 30, 2023, 01:44:48 pm
For shapes we've just merged getGeometricCenter() (https://github.com/SFML/SFML/pull/2537), which will be part of the still in development SFML 3.