SFML community forums

Help => Graphics => Topic started by: MickeyKnox on June 07, 2011, 01:43:53 pm

Title: Sprite: Corner Points
Post by: MickeyKnox on June 07, 2011, 01:43:53 pm
I want to access the corner Points of a Sprite, but i can't
find a suitable method. Is it possible to get these points directly?
Title: Sprite: Corner Points
Post by: Nexus on June 07, 2011, 01:50:06 pm
Not directly, but like this:
Code: [Select]
leftUpperCorner = position - origin;
rightLowerCorner = position - origin + size;

By the way, I should really add methods for direct access of border coordinates to my Thor library. In fact, I had implemented them, but removed them before 1.0 because they weren't mature enough.
Title: Sprite: Corner Points
Post by: Laurent on June 07, 2011, 01:51:24 pm
The points in local coordinates are:
- (0, 0)
- (0, sprite.GetSubRect().GetHeight())
- (sprite.GetSubRect().GetWidth(), 0)
- (sprite.GetSubRect().GetWidth(), sprite.GetSubRect().GetHeight())

The points in global coordinates are:
- sprite.TransformToGlobal(px)
where px is one of the local points.
Title: Sprite: Corner Points
Post by: Nexus on June 07, 2011, 02:03:23 pm
Laurent, your approach is better since it takes rotation and scale into account. Apparently, I was right with the "not mature" ;)

But this raises another question: How would one implement something like
Code: [Select]
float GetRight(const sf::Sprite& sprite);? There is no meaningful way because the right edge may be rotated. There are however still enough cases where a sprite is axis-aligned... Hm, I'll probably just wait with this stuff, there are more important tasks anyway :)
Title: Sprite: Corner Points
Post by: MickeyKnox on June 07, 2011, 02:04:04 pm
Well, my sprites may have an arbitrary rotation. Thus, in my case it is not
that easy. So i guess i have to do some trigonometrics?

By the way, what i want to do is to draw a rectangle (with sf::Shape)
around the sprite, when the user selects it. If there is a better way
to archive this, please let me know.
Title: Sprite: Corner Points
Post by: MickeyKnox on June 07, 2011, 02:11:00 pm
Another approach i can think of is to have another image/sprite with just
the rectangle, and the rest of it transparent. Then i keep the position and
rotation of the two synchronized and only draw the second sprite, when
the user selects that unit.

Which approach is better in your opinion?
Title: Sprite: Corner Points
Post by: Nexus on June 07, 2011, 02:30:32 pm
Quote from: "MickeyKnox"
Well, my sprites may have an arbitrary rotation. Thus, in my case it is not
that easy. So i guess i have to do some trigonometrics?
No, that should work the way Laurent explained. It is just my approach that doesn't cope with rotation :D

Quote from: "MickeyKnox"
By the way, what i want to do is to draw a rectangle (with sf::Shape)
around the sprite, when the user selects it. If there is a better way
to archive this, please let me know.
I think this is a good way, better than creating a separate sprite with only a border. The sf::Shape rectangle can be rotated and scaled the same way as the original sf::Sprite in order to align it correctly.
Title: Sprite: Corner Points
Post by: MickeyKnox on June 07, 2011, 03:35:44 pm
ok, thank you guys ~