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

Author Topic: [solved] rotate a rectangle around its center (SFML 1.6)  (Read 12499 times)

0 Members and 1 Guest are viewing this topic.

Dobi

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[solved] rotate a rectangle around its center (SFML 1.6)
« on: June 25, 2012, 10:01:42 pm »
Hi,
I am trying to rotate a rectangle around its center in SFML 1.6, but it is rotating around about the upper left corner of the window.
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML rect rotation");
    float angle(0.f);
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
            if (Event.Type == sf::Event::Closed)
                App.Close();
        App.Clear();
        sf::Shape thing(sf::Shape::Rectangle(300, 300, 500, 500, sf::Color::Green));
        thing.SetCenter(100, 100);
        thing.Rotate(angle);
        angle += 0.1f;
        App.Draw(thing);
        App.Display();
    }
}
Any idea what I am doing wrong? :)
Regards,
Dobi
« Last Edit: June 26, 2012, 08:50:28 am by Dobi »

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: rotate a rectangle around its center (SFML 1.6)
« Reply #1 on: June 25, 2012, 10:33:23 pm »
Even if you create a rectangle with the upper-left corner located at (300, 300), the object position is still at (0, 0), because the upper-left corner and the position are two different things.
My advice is to create your rectangle with the upper-left at (0, 0), and then move it.
After that, you just need to set the center at (width / 2, height / 2).

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML rect rotation");
    float angle(0.f);
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
            if (Event.Type == sf::Event::Closed)
                App.Close();
        App.Clear();
        sf::Shape thing(sf::Shape::Rectangle(0, 0, 500, 500, sf::Color::Green));
        thing.SetPosition(300, 300);
        thing.SetCenter(250, 250);
        thing.Rotate(angle);
        angle += 0.1f;
        App.Draw(thing);
        App.Display();
    }
}

By the way, this tricky behavior has been fixed in SFML 2.0, you will only pass width and height parameters to the rectangle shape constructor.
« Last Edit: June 25, 2012, 10:35:09 pm by Haze »

Dobi

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: rotate a rectangle around its center (SFML 1.6)
« Reply #2 on: June 26, 2012, 08:50:15 am »
Ah, great. Thank you very much. :)