Rotate sprite around mouse

393 views Asked by At

I have been trying SFML for 3 weeks soon. I've found this thing about rotating a sprite around the mouse. From long range it looks good. But the clooser it comes the more unaccurat it becomes. My image is 960 x 540

Heres the relevant code:

Game::Game()
    : mWindow(sf::VideoMode(WindowWidth, WindowHeight), WindowName, sf::Style::Close)
    , mPlayerTexture()
    , mPlayer()
    , mIsMovingDown(false)
    , mIsMovingLeft(false)
    , mIsMovingRight(false)
    , mIsMovingUp(false)
 {
    if (!mPlayerTexture.loadFromFile("Media/Texture/eagle.png"))
   { }
    mPlayer.setTexture(mPlayerTexture);
    mPlayer.setScale(sf::Vector2f(0.25, 0.25));
    mPlayer.setOrigin(sf::Vector2f(120.f, 135.f));
 }

 void Game::lookAtMouse(sf::RenderWindow &win)
 {
    sf::Vector2f curPos = mPlayer.getPosition();
    sf::Vector2i position = sf::Mouse::getPosition(win);

    const float PI = 3.14159265; 

    float dx = curPos.x - position.x;
    float dy = curPos.y - position.y;

    float rotation = (atan2(dy, dx)) * 180 / PI;

    mPlayer.setRotation(rotation + 180);
 }
0

There are 0 answers