SFML community forums

Help => Graphics => Topic started by: djarkan on April 20, 2024, 01:42:17 am

Title: [solved] shifting center on rotation
Post by: djarkan on April 20, 2024, 01:42:17 am
hi

i have my drawables witch have their centre shifted 1 pixel x and y when i rotate them
i choosedodds width and height to have a real center point for the rotation
sprite are 49 * 35 i use setorigin(24,17)
tried 48 * 34 in texturerect or vertexbuffer but no

i don't know how to get rid of that. tried with

made a simple program with sf::sprites and it's the same

thx

original on bottom rotated above. red dot should always be in black roof

(https://i.postimg.cc/tTQbs8LF/shifting-center.png)

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(300, 300), "Test divers", sf::Style::Close);

    sf::Texture texture;
    texture.loadFromFile("cars.png");
    sf::Sprite car;
    car.setTexture(texture);
    car.setTextureRect(sf::IntRect(0,0,49,35));
    car.setOrigin(24, 17);
    car.setPosition(150,150);
    sf::CircleShape circle(1);
    circle.setFillColor(sf::Color::Red);
    circle.setPosition(car.getPosition());

    window.clear(sf::Color::White);

    window.draw(car);
    window.draw(circle);
    car.setPosition(150,90);
    car.setRotation(180);
    window.draw(car);
    circle.setPosition(car.getPosition());
    window.draw(circle);
    window.display();

    while(1) ;
}
Title: Re: shifting center on rotation
Post by: kimci86 on April 20, 2024, 10:06:10 am
You need to set the origin at the center, which is the size divided by two. For an odd size, that would have a fractional part.
To get the intuition, think about a 1 * 1 sprite. Then, you need the origin to be (0.5, 0.5).
So for your 49 * 35 sprite, you need to set the origin to (24.5, 17.5).
Title: Re: shifting center on rotation
Post by: djarkan on April 20, 2024, 01:24:09 pm
thanks

was just thinking in the entire pixel