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

Author Topic: Rotation convex shape (ver 2.2).  (Read 2702 times)

0 Members and 1 Guest are viewing this topic.

PL_Andrev

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rotation convex shape (ver 2.2).
« on: October 17, 2018, 03:41:02 pm »
Hello,

I'm trying to rotate convex shape (3 vertex) but without no success.
Code is build as:
x = 400;
y = 400;
sf::ConvexShape polygon;
polygon.setPointCount(3);
polygon.setPoint(0, sf::Vector2f(x, y);
polygon.setPoint(1, sf::Vector2f(x-5, y-5));
polygon.setPoint(2, sf::Vector2f(x+5, y-5));

Now if I'm trying rotate it (by 10 degs) shape is moving away from point (400,400).
I tried to use Origin function but didn't help.

Not sure what's wrong here,

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotation convex shape (ver 2.2).
« Reply #1 on: October 17, 2018, 04:03:50 pm »
Your shape's points should be relative to the origin, it's the shape's position itself (polygon.setPosition) that should be (400, 400).
Laurent Gomila - SFML developer

Antonio9227

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Rotation convex shape (ver 2.2).
« Reply #2 on: October 17, 2018, 04:13:33 pm »
Solution 1:
Code: [Select]
sf::ConvexShape polygon;
polygon.setPointCount(3);
polygon.setPoint(0, sf::Vector2f(0, 0);
polygon.setPoint(1, sf::Vector2f(-5,-5));
polygon.setPoint(2, sf::Vector2f(+5,-5));

polygon.setPosition(400,400);
polygon.rotate(10);

Solution 2:
Code: [Select]
x = 400;
y = 400;
sf::ConvexShape polygon;
polygon.setPointCount(3);
polygon.setPoint(0, sf::Vector2f(x, y);
polygon.setPoint(1, sf::Vector2f(x-5, y-5));
polygon.setPoint(2, sf::Vector2f(x+5, y-5));

polygon.setOrigin(x,y);

The way you're doing it your convex shape has the position (0,0) and when you apply rotation, it pivots around that origin point.
You can either reset the origin point (2nd example) or construct the shape in local space and move it to (400,400) like in the first example (which I prefer).



Longer explanation:
When you build your polygon with setPoint you're building it in local space. In local space you should always define how your object looks without any rotation or translation applied; in this case you should not set the points . When the polygon is being drawn (moved to world space), it is translated and rotated according to it's transform.
I know this is a pretty crappy explanation so I googled a better one for you. This one is for unity but it's pretty much the same thing in SFML too

Edit: basically what Laurent just said