SFML community forums

Help => General => Topic started by: noaboa on June 12, 2015, 04:30:35 pm

Title: Rotate sprite around mouse
Post by: noaboa on June 12, 2015, 04:30:35 pm
Hi,
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:

Quote
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);
}
Title: Re: Rotate sprite around mouse
Post by: Hapax on June 13, 2015, 04:04:17 am
the clooser it comes the more unaccurat it becomes.
What does this mean?

Heres the relevant code
I really think you should read this (http://en.sfml-dev.org/forums/index.php?topic=5559.0).
Also, it would be better if you put code inside [code=cpp][/code] tags as it includes syntax formatting and is generally easier to read. See below...

        if (!mPlayerTexture.loadFromFile("Media/Texture/eagle.png"))
        { }
Why do you test if loadFromFile() fails but then do nothing about it?