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

Author Topic: Problems with shape origins and rotation/positioning  (Read 7991 times)

0 Members and 1 Guest are viewing this topic.

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problems with shape origins and rotation/positioning
« on: January 07, 2011, 07:50:17 pm »
Hey,

I've run into a problem which I am sure must have a simple solution.  All I am trying to do is draw two squares, both set to the same position and one rotated by a set amount around the centre of the squares.  However, I cannot get my head around how origins work in relation to rotation and positioning.

No matter what I try to do, the origin is set before both the positioning and rotation, which means that the rotation is always relative to the top left corner of the square.  What I want to be able to do is position the two squares, then set a new origin just for the rotation - is this possible?  If not then I cannot see how I can accurately place the two squares without using some horrible trigonometry.

Here's some example code:
Code: [Select]
rect2.Origin = new Vector2(20, 20);
                rect2.Position = new Vector2(300, 300);
                rect2.Rotation = 45;

gives the same result as
Code: [Select]
rect2.Position = new Vector2(300, 300);
                rect2.Origin = new Vector2(20, 20);
                rect2.Rotation = 45;


How do I draw a square at 0,0 rotated 45 degrees around its centre?

Thanks in advance,

Wibbs

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with shape origins and rotation/positioning
« Reply #1 on: January 08, 2011, 01:41:27 pm »
Transformations are not applied when you set them, they are just stored and combined later, always in the same order.

The origin is the center of all transformations: rotation, scale and translation. So to rotate a square around its center, set the origin to its center. Then what does (0, 0) means for a rotated square? What point of the square is it the position of?
Laurent Gomila - SFML developer

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problems with shape origins and rotation/positioning
« Reply #2 on: January 08, 2011, 02:13:59 pm »
The problem I am having with setting the origin is that it is actually moving the square at the same time.  If I run the following code:
Code: [Select]
               Shape rect1 = Shape.Rectangle(new FloatRect(0, 0, 100, 100), new Color(0, 0, 0));
                rect1.EnableOutline(true);
                rect1.EnableFill(false);
                rect1.OutlineWidth = 2;
                rect1.Position = new Vector2(0, 0);
                rect1.Origin = new Vector2(-20, -20);


I end up with a square which has been translated by 20, 20, which doesn't seem right

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Problems with shape origins and rotation/positioning
« Reply #3 on: January 08, 2011, 02:28:44 pm »
That's because the shape affect position indirectly. If you set the origin to be in the middle then the position(0, 0) will be in the middle of the shape. So if we move the origin (-20,-20) then when drawn the shape will be (20,20) away from where it were before but the position is still 0, 0.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with shape origins and rotation/positioning
« Reply #4 on: January 08, 2011, 02:29:02 pm »
It's right because the Position property is the position of the Origin. So if you move the origin, it moves the shape as well.
Laurent Gomila - SFML developer

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problems with shape origins and rotation/positioning
« Reply #5 on: January 08, 2011, 02:57:34 pm »
Heh, you beat me to it - I've had a further play with my code and think I've got my head around how it's working now, but I have a follow up question.

I am trying to implement a system where you can carry out an arbitary number of translations and rotations (each potentially around a different point) in any order.  My trig. is not as hot as it used to be - is there a straightforward way of representing these transforms with the single setOrigin, Rotation and Position applied in a constant order that an SFML shape uses?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Problems with shape origins and rotation/positioning
« Reply #6 on: January 08, 2011, 04:06:14 pm »
You mean something like the: Move, Scale, Rotate methods?

Though I haven't had my coffee yet so I might have misread that.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problems with shape origins and rotation/positioning
« Reply #7 on: January 08, 2011, 04:19:38 pm »
Thing is, the only one of those I can access from the .net bindings seems to be scale.  There does not appear to be a move or scale method.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with shape origins and rotation/positioning
« Reply #8 on: January 08, 2011, 08:47:00 pm »
Quote
I am trying to implement a system where you can carry out an arbitary number of translations and rotations (each potentially around a different point) in any order. My trig. is not as hot as it used to be - is there a straightforward way of representing these transforms with the single setOrigin, Rotation and Position applied in a constant order that an SFML shape uses?

I think so. In the end, it doesn't matter how many transformations you applied, you can always go from initial pose to final pose with one rotation, one scale and one translation (except if scale and rotation don't have the same center). If you use matrices it's very easy to do.
Laurent Gomila - SFML developer