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

Author Topic: [solved] sfml2 - Origin of shapes  (Read 2903 times)

0 Members and 1 Guest are viewing this topic.

Kened

  • Newbie
  • *
  • Posts: 4
    • View Profile
[solved] sfml2 - Origin of shapes
« on: August 24, 2011, 08:42:54 pm »
Hi, I've noticed something strange and I'm sorry if I overlooked something.
The documentation says
Quote
The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a drawable object is (0, 0).

This is true for sprites. However, when I was trying to rotate a line, I've noticed that shapes use the RenderWindow's 0, 0 point as origin. Is this at all intended behavior? It is not mentioned anywhere I've looked.
For instance, the following code:
Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow app(sf::VideoMode(400, 400, 32), "Lines");

    sf::Shape line1 = sf::Shape::Line(300.f, 100.f, 350.f, 100.f, 1.f, sf::Color::Blue);
    sf::Shape line2 = sf::Shape::Line(300.f, 100.f, 350.f, 100.f, 1.f, sf::Color::White);

    line1.SetRotation(10.f);
    line2.SetRotation(20.f);

    while (app.IsOpened())
    {
        sf::Event event;
        while (app.PollEvent(event)) {}

        app.Clear(sf::Color::Black);
        app.Draw(line1);
        app.Draw(line2);
        app.Display();

        //if I add these two lines, I can see the shapes orbiting around the top-left corner
        line1.Rotate(0.1f);
        line2.Rotate(0.1f);
    }

    return 0;
}

produces this:

To get the desired (and expected) effect, I'd have to do:
Code: [Select]

    sf::Shape line1 = sf::Shape::Line(300.f, 100.f, 350.f, 100.f, 1.f, sf::Color::Blue);
    sf::Shape line2 = sf::Shape::Line(300.f, 100.f, 350.f, 100.f, 1.f, sf::Color::White);
    line1.SetOrigin(300.f, 100.f);
    line1.Move(300.f, 100.f);
    line2.SetOrigin(300.f, 100.f);
    line2.Move(300.f, 100.f);
    line1.SetRotation(10.f);
    line2.SetRotation(20.f);

which feels very unnatural.

Am I doing something wrong? Did I miss something? I'd expect some mention of this in the documentation.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] sfml2 - Origin of shapes
« Reply #1 on: August 24, 2011, 10:38:28 pm »
The origin is (0, 0) by default, just like sprites or texts. The fact that you define the shape's points to be located at (300, 100) doesn't change this -- it rather means that the points are located 300 pixels away from the shape's local origin.

The conclusion is that you should create your shape's points around its origin (which is (0, 0)) and then move it to its final location with SetPosition. Just like you do with sprites and texts.
Laurent Gomila - SFML developer

Kened

  • Newbie
  • *
  • Posts: 4
    • View Profile
[solved] sfml2 - Origin of shapes
« Reply #2 on: August 24, 2011, 11:03:04 pm »
Thank you very much for your answer!
I blindly assumed that
Code: [Select]
sf::Shape line1 = sf::Shape::Line(300.f, 100.f, 350.f, 100.f, 1.f, sf::Color::Blue);
means "Make a shape with two points, with origin(0,0) relative to the shape, which is (300,100) in absolute coordinates in this case".
If only I had carefully read sf::Shape's detailed description. Sorry.