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

Author Topic: sprite scaling: local-space vs. world-space  (Read 2882 times)

0 Members and 1 Guest are viewing this topic.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
sprite scaling: local-space vs. world-space
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sprite scaling: local-space vs. world-space
« Reply #1 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)
Laurent Gomila - SFML developer

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: sprite scaling: local-space vs. world-space
« Reply #2 on: April 04, 2017, 01:12:52 pm »
Ok, I'll give that a shot.  Thanks for the quick reply!

 

anything