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

Author Topic: Rotate sprite around mouse  (Read 1528 times)

0 Members and 1 Guest are viewing this topic.

noaboa

  • Newbie
  • *
  • Posts: 14
    • View Profile
Rotate sprite around mouse
« 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);
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotate sprite around mouse
« Reply #1 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.
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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything