Hi,
It seems that setting the mouse position has invisible side-effects. I don't understand why and it's a problem for me in one of my programs. To illustrate this, I wrote a small code snippet that sets the mouse position to its own position every 20ms and shows in terminal output when the mouse moves. Here's the code snippet :
#include <chrono>
#include <iostream>
#include <thread>
#include <SFML/Window/Mouse.hpp>
int main(void){
sf::Vector2i oldMousePosition = sf::Mouse::getPosition();
while(true){
// If the mouse moved, print its position
if(sf::Mouse::getPosition() != oldMousePosition){
std::cout << sf::Mouse::getPosition().x << ", " << sf::Mouse::getPosition().y << std::endl;
}
// Set the mouse to its own position (THIS IS THE INTERESTING LINE)
sf::Mouse::setPosition(sf::Mouse::getPosition());
// Store the mouse old position and sleep
oldMousePosition = sf::Mouse::getPosition();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
return 0;
}
I compile the code snippet with "g++ main.cpp -o main -lsfml-system -lsfml-window" using gcc 8.2, SFML 2.5 and I run it on an up-to-date 64-bit Archlinux with up-to-date graphics card drivers.
The interesting line is "sf::Mouse::setPosition(sf::Mouse::getPosition());". Indeed :
-
Without this line, the mouse moves fine in all directions.
-
With this line, when trying to move the mouse
very slowly, it moves fine to the top and left, but it is very hard to move to the right and bottom. This is clearly shown by the terminal output.
As I interpret it, it feels like there might be an "internal" mouse position, more precise than the mouse position exposed by SFML, and that setting the mouse position might delete some of the precision of this "internal" mouse position, therefore causing the problem. But I might be completely wrong in my interpretation.
The thing is that the problem also occur, for example, when setting the mouse position to a fix point, always the same (which is useful for example for locking the mouse in FPS-style games). In the same way, after locking the mouse to the fix point, it is harder to move the mouse at very slow speeds to the right and bottom directions.
Does someone have an idea of why this happens and how to deal with it?