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

Author Topic: How do I convert local relative sf::Rect to global coordinates?  (Read 3513 times)

0 Members and 1 Guest are viewing this topic.

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
Hello,

So, I have a simple problem (or at least it seems). Let's say I have a sf::Sprite, and a sf::IntRect. The sf::IntRect covers a certain region of the sf::Sprite, and is relative to the sf::Sprite, but that's in local space of the sf::Sprite, what would I do if I wanted to find out where this sf::IntRect was in global space? I think I'm missing something big here.
Zzz

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: How do I convert local relative sf::Rect to global coordinates?
« Reply #1 on: June 04, 2017, 10:47:26 am »
Use the sprite (or any other sf::Transformable) transform

sprite.getTransform().transformRect()

The same works for inverse transforms

https://www.sfml-dev.org/tutorials/2.4/graphics-transform.php

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How do I convert local relative sf::Rect to global coordinates?
« Reply #2 on: June 04, 2017, 11:02:41 am »
Use the sprite (or any other sf::Transformable) transform

sprite.getTransform().transformRect()

The same works for inverse transforms

https://www.sfml-dev.org/tutorials/2.4/graphics-transform.php

I'm not sure I understand how I would apply this to my current situation?

For example:

sf::Sprite sprite;
sf::IntRect rect(3, 11, 3, 6);
sf::RectangleShape shape;

// I want shape to be in the same place as the textureRect (this is just for debugging):
sprite.setTextureRect(rect);

// How would I know where the above appears in world space? Ignoring the fact of textureRect affecting origin
shape.setPosition(??, ??);

// I can't put (3, 11), because shape isn't supposed to be relative to sprite!

 
Zzz

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How do I convert local relative sf::Rect to global coordinates?
« Reply #3 on: June 04, 2017, 12:28:11 pm »
I didn't realize I had increased the scale of sf::Sprite, it was as simple as getting the position of sf::Sprite and adding the value of the sf::IntRect position multiplied by the scale, then the sf::Shape can also be sized up accordingly by getting the sf::IntRect dimensions, and once again, multiplying by scale.
Zzz

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do I convert local relative sf::Rect to global coordinates?
« Reply #4 on: June 05, 2017, 06:21:42 pm »
The scale is included in the transform.
A result of a transformed rectangle is an axis-aligned rectangle.

It's likely you want to transform a point instead:
sprite.getTransform().transformPoint(localPoint)
becomes the final, global position of that local point.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything