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

Author Topic: Only negative coordinates are showing up in the window  (Read 2536 times)

0 Members and 1 Guest are viewing this topic.

kazen

  • Newbie
  • *
  • Posts: 1
    • View Profile
Only negative coordinates are showing up in the window
« on: November 22, 2018, 12:48:27 am »
I am having a problem with drawing entities at positive coordinates.  The 0,0 coordinate draws correctly, but as the x and y setPosition/setOrigin/etc values increase the entity moves up and to the left.  I have to enter negative values for it to show up in the window.

Also, setOrigin is acting like setPosition and not centering the circle in the window, instead it is offset like the top left "corner" of the circle is in the middle of the window.


#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>

int main()
{
        sf::RenderWindow window;
        window.create(sf::VideoMode(300,300),"!!!");
        sf::CircleShape circle(30);
        circle.setOrigin(-150,-150);    //This has to be negative to show up at (150,150)
        window.clear(sf::Color(100,100,100));
        window.draw(circle);
        window.display();
        std::cin.get();
}
 

Thanks

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Only negative coordinates are showing up in the window
« Reply #1 on: November 22, 2018, 02:19:21 am »
Setting the origin moves the origin within the local bounds and setting the position changes the global position of the shape so that its origin is at that position. If the shape is not rotated or scaled, the origin works as a negative position.

It seems as if you want to position the circle using its centre?
First, set the origin so that it is no longer in its top-left corner but in its centre:
circle.setOrigin({ circle.getRadius(), circle.getRadius() });

Then, you can place that centre of the circle where you want it to be. For example, the centre of the window:
circle.setPosition(sf::Vector2f(window.getSize() / 2u));

That places your circle in the centre of the window.



IMPORTANT NOTE: in your code, your window becomes immediately unresponsive and your operating system will be unhappy that it isn't dealing with the events that it is sending to it.

Remember the main loop as shown in the tutorials, including the getting started ones.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Only negative coordinates are showing up in the window
« Reply #2 on: November 24, 2018, 08:00:59 pm »
Allow me to steal an image from the only google image I could find:



The origin point for an object does not have to be its center. The object will rotate according to this point, and it (the origin point) may not even be INSIDE the object.

Ironically, you could look at blender tutorials to better understand origin points, as they are very similar in many different programs (photoshop, blender...) and the concept is the same in SFML as well.

 

anything