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

Author Topic: Sprite rotation to mouse problem  (Read 1445 times)

0 Members and 1 Guest are viewing this topic.

Harsay

  • Newbie
  • *
  • Posts: 5
    • View Profile
Sprite rotation to mouse problem
« on: October 03, 2012, 09:49:48 pm »
Hi.

I've got strange problem with sprite rotation to mouse coords.

sf::Vector2i cursor = sf::Mouse::getPosition(game);
sf::Vector2f worldCursor = game.convertCoords(cursor.x, cursor.y);
sf::Vector2f direction = (worldCursor - Player.getPosition());
Player.setRotation(std::atan2(direction.y, direction.x));
 

Player is not rotating properly.
I found this code on this forum and to compile i need to add sf::Vector2i to worldCursor.

Thanks in advance for help.
« Last Edit: October 03, 2012, 09:59:03 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite rotation to mouse problem
« Reply #1 on: October 03, 2012, 10:00:23 pm »
Quote
Player is not rotating properly.
The code is ok so it should be almost correct. Adding a minus sign or PI/2 somewhere should solve your problem.
Laurent Gomila - SFML developer

Harsay

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite rotation to mouse problem
« Reply #2 on: October 03, 2012, 11:15:09 pm »
                sf::Vector2i cursor = sf::Mouse::getPosition(game);
                sf::Vector2f worldCursor = game.convertCoords(sf::Vector2i(cursor.x, cursor.y));
                sf::Vector2f direction = (worldCursor - Player.getPosition());
                Player.setRotation(std::atan2(direction.y, direction.x) * 180 / 3.14);
 

Now its working.

Thanks