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

Author Topic: Rotate the shapes relative to center  (Read 2258 times)

0 Members and 2 Guests are viewing this topic.

Lekter

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Rotate the shapes relative to center
« on: October 20, 2014, 11:06:21 pm »
Hi, I have RectangleShape, how to rotate it relative to center.
I have tried the following:
shape.setOrigin(size_a/2, size_b/2); //size_a, size_b - size of shape
shape.setPosition(100, 100);
shape.setSize({ size_a, size_b });
shape.setRotation(20);
 
However, rotate relative top-left corner. Am I doing something wrong?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Rotate the shapes relative to center
« Reply #1 on: October 20, 2014, 11:10:53 pm »
Not sure, but what if you set the size first, then the origin and the rotate it?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotate the shapes relative to center
« Reply #2 on: October 21, 2014, 07:50:33 am »
Your code looks ok. Can you show a complete and minimal example that reproduces the problem?
Laurent Gomila - SFML developer

Lekter

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rotate the shapes relative to center
« Reply #3 on: October 21, 2014, 10:46:29 am »
Here is my full code:

const int windowWidth = 500;
const int windowHeight = 400;

class Box
{
public:
        RectangleShape shape;

        Box(float size_a, float size_b)
        {
                shape.setFillColor(Color::Blue);
                shape.setPosition(100, 100);
                shape.setSize({ size_a, size_b });
                shape.setOutlineThickness(2);
                shape.setOutlineColor(Color::Black);
        }

};

int main()
{
        Box box(200,150);

        RenderWindow window(VideoMode(windowWidth, windowHeight), "Box");
        window.setFramerateLimit(60);

        while (window.isOpen())
        {
                Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window.close();
                }

                window.clear(Color::White);
                window.draw(box.shape);
                window.display();

        }
        return 0;
}
 
Get the following:


If I change constructor:
Box(float size_a, float size_b)
        {
                shape.setOrigin(100, 100);
                shape.setFillColor(Color::Blue);
                shape.setPosition(150, 150);
                shape.setSize({ size_a, size_b });
                shape.setOutlineThickness(2);
                shape.setOutlineColor(Color::Black);
               
                shape.setRotation(20);
        }
 
Produce this:

Top-left corner moving coordinates (x-100,y-100), and rotate relative to top-left corner.
« Last Edit: October 21, 2014, 11:10:02 am by Lekter »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Rotate the shapes relative to center
« Reply #4 on: October 21, 2014, 11:11:45 am »
All transformations (position, scale, rotation) will happen around the origin. So if you set the origin to (100, 100) the rotation will happen around that point. If you want the rotation to happen around the center of your rectangle you need to set it to (size_a/2.f, size_b/2.f).
« Last Edit: October 21, 2014, 12:50:57 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/

Lekter

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Rotate the shapes relative to center
« Reply #5 on: October 21, 2014, 12:02:52 pm »
Thank You so much, in my first post all right, i was wrong.