SFML community forums

Help => Graphics => Topic started by: Jabberwocky on April 04, 2017, 12:56:46 pm

Title: sprite scaling: local-space vs. world-space
Post by: Jabberwocky on April 04, 2017, 12:56:46 pm
This seems like it should be a remarkably easy thing to solve.  But for some reason it's stumping me.

I have a sprite.  This sprite might be scaled, rotated, or otherwise transformed in any way.
Now, I want to further stretch (scale) this sprite along the x axis of the screen (i.e. stretch left and right) regardless of the sprite's orientation.  I do not want to stretch along the x axis of the sprite itself.

Simply calling this:
sf::Sprite::scale(scaleFactor, 1.f)
doesn't work, because it would only stretch in the proper direction if the sprite was non-rotated.  For example, if I turned the sprite 90 degrees, then it would now be stretched on the screen y axis.  I always want the stretching on the screen x axis.

In other words, the sf::Sprite::scale function works as a local-space scaling factor.  I need a world-space scaling factor.

Is there something simple I can do with an sf::Transform here?  It's no problem if I have to perform a per-frame function call to accomplish this, to account for a rotating sprite.

It seems like I'm missing something obvious.
Thanks!
Title: Re: sprite scaling: local-space vs. world-space
Post by: Laurent on April 04, 2017, 01:05:34 pm
You will never be able to scale the sprite this way if you only rely on its own transformations. SFML transformable entities are designed so that transformations are applied in a specific order, which guarantees a specific behaviour -- and the behaviour for scaling is to scale along the local axes of the entity.

So you probably need this:

window.draw(sprite, sf::Transform().scale(scaleFactor, 1.f));

(not sure if it is applied before or after the sprite's transform, I have not tested, so it might or it might not work ;D)
Title: Re: sprite scaling: local-space vs. world-space
Post by: Jabberwocky on April 04, 2017, 01:12:52 pm
Ok, I'll give that a shot.  Thanks for the quick reply!