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

Author Topic: Rotating a RectangleShape according to another RectangleShapes origin  (Read 1559 times)

0 Members and 1 Guest are viewing this topic.

ScriptingTacos

  • Newbie
  • *
  • Posts: 7
    • View Profile
I want to be able to rotate a rectangle using another rectangles origin
So I thought I would have to do this:

rect2.setOrigin(rect1.getOrgin());

But that made rect2 rotate differently.
Any help?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
All transformations are applied relative to the origin. If you set the origin to the top left corner (default) your rectangle shape will rotate around the top left corner. If you set the origin to the middle of the rectangle shape it will rotate around its center.

As such the origin is local to an object and if you "copy" the origin of another rectangle shape, you'll just copy the local origin. If you want to rotate around another object, you need to use the global position instead:

rect2.setOrigin(rect.getPosition());
« Last Edit: July 16, 2015, 06:33:08 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ScriptingTacos

  • Newbie
  • *
  • Posts: 7
    • View Profile
All transformations are applied relative to the origin. If you set the origin to the top left corner (default) your rectangle shape will rotate around the top left corner. If you set the origin to the middle of the rectangle shape it will rotate around its center.

As such the origin is local to an object and if you "copy" the origin of another rectangle shape, you'll just copy the local origin. If you want to rotate around another object, you need to use the global position instead:

rect2.setOrigin(rect.getPosition));

This did work but it sets rect2 far from the rect1
If I try to move it closer to rect1 also moves rects2 orgin

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
You really need to understand how the transformations (translation, rotation and scale) works, what roles position and origin have and how global and local space operate.
Once you got that, it's trivial to implement whatever you're trying to implement. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything