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

Author Topic: How to Make a Rotatable Rect  (Read 2124 times)

0 Members and 1 Guest are viewing this topic.

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
How to Make a Rotatable Rect
« 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.
« Last Edit: October 10, 2012, 12:58:54 am by Ruckamongus »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How to Make a Rotatable Rect
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: How to Make a Rotatable Rect
« Reply #2 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
« Last Edit: October 10, 2012, 01:05:12 am by Ruckamongus »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How to Make a Rotatable Rect
« Reply #3 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 and the example there. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything