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

Author Topic: Newbie Question: How to set an object's position to the middle of the screen?  (Read 8893 times)

0 Members and 1 Guest are viewing this topic.

Uk Marine

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Hi everyone, can anyone tell me how to calculate both x and y values of a circle to set its position to the center of the screen? i tried dividing the screen resolution by /2 but it didn't seem to work, can someone please remind me how to do this? Thank you.

mWindow(sf::VideoMode(1280 , 800)
{
   mPlayer.setRadius(40.f);
   mPlayer.setPosition(640.f , 400.f);
   mPlayer.setFillColor(sf::Color::Blue);
}

Result:


Barlog

  • Guest
You should use
setOrigin(x,y)
to center origin point of a sprite. Set it to half of a size of a sprite;
Or you need to subtract half of width and height of sprite from sprite position each time you reposition your sprite;

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
mPlayer.setOrigin(mPlayer.getRadius(), mPlayer.getRadius());
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Unless otherwise specified, you set the left-upper corner of an object with setPosition(). As mentioned by Tirion Helkaraxe, you should set the origin to the center. In the case of a sf::CircleShape, the origin is simply at position (radius, radius).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Uk Marine

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Thanks for your answers guys, the mPlayer.setOrigin(mPlayer.getRadius(), mPlayer.getRadius()); worked, but why did we do (mPlayer.getRadius(), mPlayer.getRadius) twice?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
X and Y components of the origin. The radius is a scalar, the origin a vector.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Thanks for your answers guys, the mPlayer.setOrigin(mPlayer.getRadius(), mPlayer.getRadius()); worked, but why did we do (mPlayer.getRadius(), mPlayer.getRadius) twice?
The default origin is the top-left of the boundary box of the circle. To get the origin into the centre, you need to move it radius to the right, and radius down.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Uk Marine

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Thank you guys so much, really appreciate your help.