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

Author Topic: Invisible side-effects of setting the mouse position?  (Read 5126 times)

0 Members and 1 Guest are viewing this topic.

dandyolaga

  • Newbie
  • *
  • Posts: 2
    • View Profile
Invisible side-effects of setting the mouse position?
« on: August 17, 2018, 06:27:00 pm »
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?
« Last Edit: August 17, 2018, 06:31:23 pm by dandyolaga »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Invisible side-effects of setting the mouse position?
« Reply #1 on: August 17, 2018, 09:07:44 pm »
Works just fine on my Windows machine.

Might be driver related or OS related, but I don't really see why it would be harder to go in one direction.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dandyolaga

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Invisible side-effects of setting the mouse position?
« Reply #2 on: August 17, 2018, 09:55:20 pm »
I guess I should try on other machines then. On my tests the difference is clearly visible : about 4 or 5 times less lines printed in the terminal for the right and bottom directions compared to the other two when moving the mouse very slowly.

My idea was that it could be linked with the things discussed in this SFML ticket about accessing raw input for mouse movement : https://github.com/SFML/SFML/issues/304. I thought that maybe if there is this raw mouse movement/position that exists somewhere, possibly sf::Mouse::setPosition resets it.

Thanks for looking at the problem anyway eXpl0it3r, but of course if you can't reproduce it it will be hard to do more.