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

Author Topic: Tracking angle between sprite and mouse only seems to work in fullscreen mode  (Read 1224 times)

0 Members and 1 Guest are viewing this topic.

Triplanetary

  • Newbie
  • *
  • Posts: 2
    • View Profile
So I wrote the following lines in a RenderWindow's update loop to test making a sprite always rotate to face the mouse cursor:

sf::Vector2f spritePos = sprite.getPosition();
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition());
float angle = atan2(mousePos.y - spritePos.y, mousePos.x - spritePos.x) * 57.296f;
sprite.setRotation(angle);
 

It was very wonky - the angle was never exactly correct, was always more accurate in the lower-right quadrant than any other, seemed to get more accurate as the mouse got farther from the sprite (but still never particularly accurate). Just generally not behaving the way I expected.

I spent a while tweaking it and tearing my hair out trying to figure out if I just didn't understand the math. Then, on a whim, I decided to try it in fullscreen mode. In fullscreen mode it worked perfectly.

So... is there an issue with mouse accuracy in windowed mode? Is there anything I need to do in my code to make this work in windowed mode, or should I just resign myself to fullscreen mode if I'm going to be doing mouse tracking like this?

Using SFML 2.3 in Windows 7, compiled with Visual Studio 2013, if it matters. Is this a known issue specific to Windows' window system, by any chance? Thanks for any help.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Do
sf::Mouse::getPosition(window);

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
You can also get and set the current position of the mouse, either relative to the desktop or to a window:

// get the global mouse position (relative to the desktop)
sf::Vector2i globalPosition = sf::Mouse::getPosition();

// get the local mouse position (relative to a window)
sf::Vector2i localPosition = sf::Mouse::getPosition(window); // window is a sf::Window

Triplanetary

  • Newbie
  • *
  • Posts: 2
    • View Profile
Ack, of course, I was getting the mouse position relative to the desktop's (0, 0). I probably should've glanced at the sf::Mouse documentation before I posted. Thanks to you both!