SFML community forums

Help => Graphics => Topic started by: kazen on November 22, 2018, 12:48:27 am

Title: Only negative coordinates are showing up in the window
Post by: kazen 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
Title: Re: Only negative coordinates are showing up in the window
Post by: Hapax 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.
Title: Re: Only negative coordinates are showing up in the window
Post by: ZeroZ30o on November 24, 2018, 08:00:59 pm
Allow me to steal an image from the only google image I could find:

(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS2S_2RwNzj3cBJl16D7exGMazO3R0FNaVffDnCImWvbh1hJC1n)

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.