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

Author Topic: Setting Circle Center [Solved]  (Read 18384 times)

0 Members and 1 Guest are viewing this topic.

kreadeth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Setting Circle Center [Solved]
« on: October 25, 2015, 04:22:15 pm »
When I write something like this:

sf::CircleShape circle;
circle.setRadius(5);
circle.setPosition(0, 0);

(0, 0) point would not be the circle center. What should I do in order to do this?
« Last Edit: October 25, 2015, 05:53:17 pm by kreadeth »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Setting Circle Center
« Reply #1 on: October 25, 2015, 04:42:38 pm »
You need setOrigin() -- have a look at its documentation.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kreadeth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Setting Circle Center
« Reply #2 on: October 25, 2015, 05:20:41 pm »
I did before posting here. See:

    sf::RenderWindow window (sf::VideoMode(800,600), "Circle");
    sf::CircleShape circle;
    circle.setPosition(400, 300);
    circle.setOrigin (400, 300);
    circle.setRadius(50);
 

or:

    circle.setOrigin (sf::Vector2f(400, 300));
 
or
    circle.setOrigin ({400, 300});
 

Anyway the result I get is a circle in the uppermost left corner of the window.
« Last Edit: October 25, 2015, 05:23:11 pm by kreadeth »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Setting Circle Center
« Reply #3 on: October 25, 2015, 05:39:17 pm »
That's because setOrigin and setPosition are different functions. the origin is the point which is moved to the position given to set position (which is a position in global coordinates). setOrigin moves the point around which the circle moves to a new point (in local coordinates). i.e. setOrigin (0,0) means that the shape will rotate and scale around the top left corner. It also means that when setPosition is called, the point that is lined up with the position passed to setPosition is the top left corner.What you are doing is setting the origin the 400 to the right of the top left corner of the circle and 300 below it. You then line that position up with the point 400,300. This is why the circle itself appears in the top left corner. I hope that made sense, but I'm not sure I explained it very well.
EDIT: To answer the original question, you should set the origin to (width/2,height/2) because that is the centre as coordinates start at the top left.

kreadeth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Setting Circle Center
« Reply #4 on: October 25, 2015, 05:49:22 pm »
I got it right. Thank you both.  :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Setting Circle Center
« Reply #5 on: October 26, 2015, 04:33:01 pm »
To answer the original question, you should set the origin to (width/2,height/2) because that is the centre as coordinates start at the top left.
Since a circle's width and height are identical and the length of two radii, it makes sense to set the origin of a circle to its centre thusly:
circle.setOrigin(circle.getRadius(), circle.getRadius());
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything