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

Author Topic: Strange behaviour of sf::Shape::SetRotation?  (Read 2409 times)

0 Members and 1 Guest are viewing this topic.

Brandy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Strange behaviour of sf::Shape::SetRotation?
« on: February 09, 2009, 07:17:39 am »
I was writing some code yesterday which was just displaying a white rectangle following the mouse cursor with its orientation. This rectangle was a sf::Sprite, and therefore I set the Center to the middle of the Sprite. Then I then tried to do the same thing with a sf::Shape and created just the same rectangle again. But when I tried to apply the code for the rotation to it, the rectangle was behaving quite strange.
I really have no idea why this is not working, maybe I misunderstood some of the methods the class is offering. So here's the relevant code:

Code: [Select]
sf::Shape shape;
shape.AddPoint(300, 300); //I am aware of sf::Shape::rectangle, but I  think it should work like that anyway.
shape.AddPoint(500, 300);
shape.AddPoint(500, 500);
shape.AddPoint(300, 500);
shape.SetCenter(100,100); //Shouldn't this set the center exactly to the rectangle's middle?

//Event handling etc. is not mentioned here, please don't mention the code indent, it was kind of messed up by copy'n paste from the sourcefile.

if (input.GetMouseX() <= shape.GetPosition().x) {
shape.SetRotation  (-1 * 180 / 3.1415926535 * (atan( static_cast<double> (shape.GetPosition().y - input.GetMouseY()) / static_cast<double> (shape.GetPosition().x - input.GetMouseX()))));
} else {
shape.SetRotation  (-1 * 180 / 3.1415926535 *(atan( static_cast<double> (shape.GetPosition().y - input.GetMouseY()) / static_cast<double> (shape.GetPosition().x - input.GetMouseX()))));
}


This rotation code (any suggestions to improve it?) works just fine for sf::Sprite. But like I said, here it's messing up the whole thing.
When experimenting a bit with the values I passed to SetCenter() I noticed that the shape was seemingly displaced just about the parameters I passed to SetCenter().
What am I doing wrong?

Another little question:
When I am rotating the Shape, are the positions of the points added with AddPoint() influenced? Checking the points' coordinates made me think it wouldn't.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange behaviour of sf::Shape::SetRotation?
« Reply #1 on: February 09, 2009, 07:57:41 am »
Quote
Shouldn't this set the center exactly to the rectangle's middle?

Your rectangle is defined between (300, 300) and (500, 500). So no, (100, 100) is not exactly its center ;)

A lot of people are confused with shapes, they always expect their center and position to be automatically adjusted to fit the actual bounding rectangle of the shape. But no, they remain at (0, 0) unless you explicitely change them.
Laurent Gomila - SFML developer

Brandy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Strange behaviour of sf::Shape::SetRotation?
« Reply #2 on: February 09, 2009, 09:22:13 am »
Quote from: "Laurent"
Your rectangle is defined between (300, 300) and (500, 500). So no, (100, 100) is not exactly its center
I thought about that before, but somehow 400,400 didn't work either.
Also the documentation says it would be relative to the top left corner of the object, which is quite confusing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange behaviour of sf::Shape::SetRotation?
« Reply #3 on: February 09, 2009, 10:08:03 am »
Quote
I thought about that before, but somehow 400,400 didn't work either.

What result did you get?

Quote
Also the documentation says it would be relative to the top left corner of the object, which is quite confusing.

So as the points you provide. So (300, 300) is not the top-left corner of the object, it's just a point located (300, 300) away from the top-left corner of the object (which is (0, 0) unless you change it with SetCenter).
Laurent Gomila - SFML developer

Brandy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Strange behaviour of sf::Shape::SetRotation?
« Reply #4 on: February 09, 2009, 02:58:00 pm »
Quote from: "Laurent"
What result did you get?
I found the rectangle in the top left corner of the Window centered, so that 0,0 (of the window) was the middle of the rectangle.

Then I can set the position to where I want it to be. So the actual problem is solved now.
But did I get that right:
When I set the center to a position, it only means that from then on the rectangle is "grabbed" at that point, e.g. when setting the position, what should also be the reason why the rectangle is drawn at (0,0) when I set the Center at (400, 400).

Could one even say it doesn't matter where the absolute position of the points is, as you usually adjust the position and center yourself?

Oh boy don't hold it against me, but I guess I got my wires crossed.

Referring to my question about points: I guess they won't change their position then, do they?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange behaviour of sf::Shape::SetRotation?
« Reply #5 on: February 09, 2009, 03:55:37 pm »
Yes, you got it ;)

Actually, "Center" will be replaced with "Origin" or "LocalOrigin" in SFML 2.0.
That should make more sense.
Laurent Gomila - SFML developer

 

anything