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

Author Topic: SetCenter()  (Read 18098 times)

0 Members and 1 Guest are viewing this topic.

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
SetCenter()
« on: May 16, 2011, 12:32:56 pm »
HI,
       I have a doubt about SetCenter() function.

Like if i use
Code: [Select]

sf::Shape Box = sf::Shape::Rectangle(0, 0, 32, 32, sf::Color(0, 0, 0, 200));
        Box.SetCenter(16, 16);


here it means a rectangle of 32 width and 32 height. its center is 16,16 ??

Code: [Select]

           (0,0) ---------------
                 |              |
                 |   (16,16)    |               is this figure correct representation
                 |              |                      of default  Shape::Rectangle
                 ---------------  (32,32)


I am confused with the Setcenter() of SFML. Does it sets the center of the rectangle at (16,16) but then whats the center by default(0,0)???

By default How can (0,0) be the center of a rectangle of Rectangle(0, 0, 32, 32, sf::Color(0, 0, 0, 200));???
 (0,0) is the Origin of the screen co-ordinate !!!!!


The doubt basically comes up because I am using Box2d with SFML. Since its co-ordinate system is different from SFML, I am trying to  use SetCenter() to place the body correctly.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #1 on: May 16, 2011, 01:23:33 pm »
SetCenter is not the center (middle) of the shape, which would have to be calculated from the points.

SetCenter is the center (origin) of all transformations that you apply to it (SetPosition, SetRotation, SetScale), it changes only if you call SetCenter so it is always (0, 0) by default.

It is easy to see that the shape's points and the center are completely unrelated: the formers are defined in sf::Shape while the latter is a generic attribute defined in sf::Drawable (which known nothing about its derived classes).

And yes, "center" is a confusing name, it was renamed to "origin" in SFML 2 ;)
Laurent Gomila - SFML developer

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
SetCenter()
« Reply #2 on: May 16, 2011, 02:34:37 pm »
Quote from: "Laurent"

SetCenter is the center (origin) of all transformations that you apply to it (SetPosition, SetRotation, SetScale), it changes only if you call SetCenter so it is always (0, 0) by default.


That means by default the the shape are drawn like

Code: [Select]

                        SCREEN
     (0,0)    -------------------------
              |
              |
              |

if SetCenter /SetOrigin()       is used then it gets changed to

eg: SetCenter(16,16);

                                     SCREEN
     (0,0)    -------------------------
              |
              |   (16,16)  ------------
              |           |
                          |
                                   



Is this correct?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #3 on: May 16, 2011, 02:58:55 pm »
I'm not sure that I understand your ASCII-art, but it looks ok.
Laurent Gomila - SFML developer

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #4 on: May 17, 2011, 09:36:18 am »
This is driving me nuts. I can't understand how SetCenter works, or how it is different from Move and SetPosition. I have a line that starts out in the middle of a box at 160 degrees and I'm trying to make it pivot around point 0 (x1, y1), but when I run the following code the line ends up clear off the screen somewhere. I have it set up so when I press a button on my joystick it rotates by 30 degrees. The line seems to rotate around (0,0) even after changing the center with SetCenter as below.


Code: [Select]

 x1 = x_pos + (bounds_size * .5);
 y1 = y_pos + (bounds_size * .5);
 x2 = x1 - (Line_Length * cos(M_PI / 4));
 y2 = y1 - (Line_Length * sin(M_PI / 4));

 Line = sf::Shape::Line ( x1, y1, x2, y2, LINE_THICKNESS, LINE_COLOR, BORDER_WIDTH, BORDER_COLOR );
   
 Line.SetCenter(x1, y1);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #5 on: May 17, 2011, 10:00:41 am »
Be careful, when you change the center you change the global position of the shape as well, since GetPosition() refers to the position of the center point.
Laurent Gomila - SFML developer

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #6 on: May 17, 2011, 06:25:59 pm »
If GetPosition refers to the position of the center, what does GetCenter refer to?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #7 on: May 17, 2011, 06:32:31 pm »
The center is in local coordinates (ie. relative to the top-left corner of the entity) while the position is in global coordinates in the 2D scene.

It's like defining the hot spot of a mouse cursor: you define it relative to the cursor image, but then when you click (for example) you get the position of the hot spot in the whole screen.
Laurent Gomila - SFML developer

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #8 on: May 17, 2011, 06:48:42 pm »
Ok thank you it seems to be working better with the following:

Code: [Select]

x1 = x_pos + (bounds_size * .5);
y1 = y_pos + (bounds_size * .5);
x2 = x1 - (Line_Length * cos(M_PI / 4));
y2 = y1 - (Line_Length * sin(M_PI / 4));

Line = sf::Shape::Line ( x1, y1, x2, y2, LINE_THICKNESS, LINE_COLOR, BORDER_WIDTH, BORDER_COLOR );
   
Line.SetPosition(x1, y1);
Line.SetCenter(x1, y1);


After your last post I tried it using just SetPosition, and it change the point it rotates around to x1,y1, but the radius was still to large. Instead of the line pivoting around one of it's points, the entire line circled around x1,y1 with a large radius of maybe 300 or 400, at times going off the window entirely. After I used SetPosition and SetCenter together, it now pivots around the point.

I've played with different values for SetCenter and SetPosition and I have been unable to discern any pattern in order to figure out the difference, all I know is that if I use them together it sort of works. Could you please give a more detailed explanation?

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #9 on: May 17, 2011, 06:58:22 pm »
Ok I think I might be getting it. SetPosition doesn't set the position of the entity, it sets the position of the entity's center, which by default is at 0,0 - correct? So when I only use one of the functions it shifts the entity of the window. They both behave very similar but SetCenter allows finer control? Does SetCenter set the center to a point (with an ID) on the entity or just the top left position?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #10 on: May 17, 2011, 07:04:32 pm »
I don't know how to explain it better (a picture would certainly help more), but I'll do my best :D

If you call SetPosition(x, y), the top-left corner of your shape will move to (x, y).
If you call SetRotation(a), your shape will rotate around its top-left corner.
If you call SetScale(sx, sy), your shape will be stretched around its top-left corner.

So you see, the top-left corner is a special point, because all the transformations that you apply to your shape are made relatively to this point. It is the origin/center of the transformations. And its (local) coordinates are always (0, 0), no matter what transformation you apply to the shape -- because local coordinates are always defined relatively to the top-left corner of the untransformed shape.

What SetCenter does is to choose another point as the origin of transformations.

Let's now take an example. Let's say that you have a square shape of 5x5 pixels, and you call SetCenter(5, 5).
What happens? SetPosition(x, y) will move the bottom-right corner of your shape to (x, y), SetRotation(a) will rotate your shape around its bottom-right corner, and SetScale(sx, sy) will stretch it relatively to its bottom-right corner. So what you did is to change the "hot spot" of the shape to be its bottom-right corner instead of the default top-left corner.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #11 on: May 17, 2011, 07:07:06 pm »
Or, if it's clearer for you, just imagine that SetCenter sets the center of rotation of the shape. Focus on this idea (the concept of "center of rotation" is usually simple to understand).
And then imagine that it does the same thing with scale and position.
Laurent Gomila - SFML developer

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #12 on: May 17, 2011, 07:14:01 pm »
Ok that definitely helps, but there must be something wrong with my code because it doesn't seem to be working that way. If I use only SetPosition, the top left corner of my line doesn't move to the new position, instead it is moving completely off the window where I can't even see it.

Maybe this will help me understand. You said that the default position of the center is (0,0). Is that (0,0) on the line or the global (0,0) on the window? If it's on the window then the behavior I'm seeing sort of makes sense.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SetCenter()
« Reply #13 on: May 17, 2011, 07:20:48 pm »
What happens is that your line is already offsetted when you create it, because you define its points starting at (x1, y1), but the center/origin stays at (0, 0).

What you should do instead is to create it at (0, 0) and then play with SetCenter/SetPosition/... It's much more convenient.
Laurent Gomila - SFML developer

tanders12

  • Newbie
  • *
  • Posts: 9
    • View Profile
SetCenter()
« Reply #14 on: May 17, 2011, 08:54:16 pm »
Ah... that makes sense I will try that tonight. Is it normal practice to initialize entity's at 0,0 and then move them where you want them?