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

Author Topic: Set Mouse Position without triggering Event  (Read 4464 times)

0 Members and 1 Guest are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Set Mouse Position without triggering Event
« on: December 25, 2012, 01:26:19 pm »
I want to implement a first person camera, so I need to figure out the mouse movement.

Therefore my function listens to the "MouseMoved" event. When triggered I calculate the offset of the new mouse position to the center of the window. Then I set the mouse position back to the center of the window. So I get the mouse position delta each frame the mouse moved.

Vector2i center(window->getSize().x / 2, window->getSize().y / 2);
Vector2i delta = Vector2i(event.mouseMove.x, event.mouseMove.y) - center;
Mouse::setPosition(center, *window);

Unfortunately this results in an endless circle since "setPosition" triggers the "MouseMoved" event again and my function is executes over and over.

How can I set the mouse position without triggering the "MouseMoved" event?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11037
    • View Profile
    • development blog
    • Email
AW: Set Mouse Position without triggering Event
« Reply #1 on: December 25, 2012, 02:18:30 pm »
I wouldn't rely on the MouseMoved event, but rather do a check on your own with sf::Mouse::getPosition().
If you insist on using the event you could use some boolean and set it to false before proccessing any events and to true with in the MouseMoved section. Then whenever another MouseMoved event would get into the queue you ignore it.
Or another would be to where the mouse was moved to, if it's the center of the screen then ignore the event. ;)

Keep in mimd though that events are not 100% real time and with sf::Mouse you'd simply check the state and not rely on anything else.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Set Mouse Position without triggering Event
« Reply #2 on: December 25, 2012, 02:59:09 pm »
Quote
Or another would be to where the mouse was moved to, if it's the center of the screen then ignore the event.

That's what I will use, thanks! By the way, the SFML function "Mouse::setPosition()" should check if the mouse is actually moved or just set to the position where it already is. Then it could fire the event only when it applies.

Quote
Keep in mimd though that events are not 100% real time [...]

They are not? I decided to use events to not perform useless calculations when the mouse is not moved.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11037
    • View Profile
    • development blog
    • Email
Re: Set Mouse Position without triggering Event
« Reply #3 on: December 25, 2012, 03:10:04 pm »
By the way, the SFML function "Mouse::setPosition()" should check if the mouse is actually moved or just set to the position where it already is. Then it could fire the event only when it applies.
sf::Mouse and events don't have anything in common. They are two completely different systems. And the MouseMoved event gets triggered if the mouse has been moved. If it has moved to the same position again is not something it should care about. Anyways the events are generated by the OS and not SFML. SFML just translates them to a crossplatform format.

They are not? I decided to use events to not perform useless calculations when the mouse is not moved.
Nope they are not, but they are also neither far from real time. The thing is as already said that sf::Mouse/sf::Keyboard/sf::Joystick simply check the state of the device (getPosition/isButtonPressed/etc.), while the events get triggered from the OS, then pushed into a queue and then later processed by the application/SFML, thus you can get some delay there and if the queue doesn't get emptied right away events could pile up there and lead to noticeable delays. As for keyboard buttons, the events get only triggered a few times, which is nice for entering text, but no very practical for games (except if you only check for the states pressed/released). ;)

So both have their use cases, but one needs to know when to use which.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Set Mouse Position without triggering Event
« Reply #4 on: December 25, 2012, 03:14:20 pm »
Thanks for your explanation!