SFML community forums

Help => Graphics => Topic started by: Ruckamongus on October 10, 2012, 12:44:12 am

Title: How to Make a Rotatable Rect
Post by: Ruckamongus on October 10, 2012, 12:44:12 am
Hello all,

I'm wondering what would be the best way to create a rotatable rectangle? I don't want it to have a texture, or be drawable, however I do want it to have an origin (and location) and be able to retrieve it's four coordinates depending on its rotation.

For example (all psuedocode of course):
RotateRect Rect;
Rect.setSize(x1, y1, x2, y2);//x2/y2 can be width and height, it doesn't matter
Rect.setOrigin(sf::Vector2f(10, 10));
Rect.setRotation(63);
 

Now I want to be able to to get the x1, x2, y1, and y2 coordinates on runtime given it's rotation. Would it be better to create a class that does the trig-math stuff on it's own, or try to inherit from sf::Transformable? If it does inhert from sf::Transformable, how do I get the vertices' coordinates?

Again, I do not want it to be drawn, so inheriting from sf::Shape or sf::RectShape is a bit too heavy.
Title: Re: How to Make a Rotatable Rect
Post by: eXpl0it3r on October 10, 2012, 01:00:03 am
Just use a sf::Transform, rotate it and convert the wanted points. If you want to wrap it into a class you can derive from sf::Transformable. ;)
sf::Transform is basically a transformation matrix.
Title: Re: How to Make a Rotatable Rect
Post by: Ruckamongus on October 10, 2012, 01:01:25 am
I don't know how to use sf::Transform unfortunately. :/
Matrix calculations have never been my strong point. I'll look into it though.

Edit: I just looked at sf::Transform. SFML takes care of all the nasty linear algebra for me! I'm lovin' it, thanks eXpl0it3r (for the guidance), and thanks Laurent! :P
Title: Re: How to Make a Rotatable Rect
Post by: eXpl0it3r on October 10, 2012, 01:04:17 am
Well it's very straight forward and you actually don't need to calculate anything on your own...

Take a look at the documentation  (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transform.php)and the example there. ;)