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

Author Topic: [SOLVED] Mouse motion help  (Read 3888 times)

0 Members and 1 Guest are viewing this topic.

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
[SOLVED] Mouse motion help
« on: June 11, 2024, 04:12:16 am »
Hi, I'm new to this board! I've been tinkering with SFML on a hobby-programming basis for about 3 years.

I'd like to create mouse motion akin to the old Oxyd marble games. It may be a very simple and common idiom for all I know, but I've got a little hangup.

My current approach is something like this:
```
lastMouse = mouse x/y from last frame;
curMouse = mouse x/y this frame;
newMouseDirection = curMouse - lastMouse;
newMouseDirection *= reducingScaleFactor;
marble.velocity += newMouseDirection;
//add friction and whatnot
```
The problem is that when the (set-to-invisible) cursor goes to (say) the left border of the screen, leftward mouse motion no longer impels the marble, because lastMouse.x was 0 and curMouse.x is still 0. Same principle with any border of the screen.
I feel like I need the capability to set the cursor position, so that if it hits a screen border, it appears on the opposing side, or whatever, so that motion can be continued.
Does SFML provide this means, or do I need to access some kind of system API to handle this mouse/cursor issue? Can SFML "read" the mouse motion if the cursor is already at 0,0 coordinates and the direction is towards negative values?
Thanks!
« Last Edit: June 27, 2024, 01:45:24 am by johnnywz00 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Mouse motion help
« Reply #1 on: June 11, 2024, 09:21:13 am »
You can grab the mouse with window.setMouseCursorGrabbed(true), that way you can prevent the cursor from leaving the window. Then you can reset the mouse position to the center of the window every frame sf::Mouse::setPosition(window.getSize() / 2, window) or similar.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse motion help
« Reply #2 on: June 11, 2024, 04:10:53 pm »
Thanks a ton! I don't know how I forgot the Mouse class... I was too stuck on looking into things that included "cursor". Cheers!

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse motion help
« Reply #3 on: June 11, 2024, 06:09:36 pm »
You can grab the mouse with window.setMouseCursorGrabbed(true), that way you can prevent the cursor from leaving the window. Then you can reset the mouse position to the center of the window every frame sf::Mouse::setPosition(window.getSize() / 2, window) or similar.

Hum... the statement
Code: [Select]
    Mouse::setPosition(Vector2i(720,450));  //using namespace sf;
doesn't seem to have any effect in my program. For testing purposes, I put the statement within a function that is called on mouseclick. Other statements in the function take effect when the mouse is clicked, but the cursor does not change position. Adding the window reference as second argument doesn't change anything, neither does the state of cursor grabbing.
Any thoughts?

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse motion help
« Reply #4 on: June 11, 2024, 06:31:24 pm »
I've found the problem, for anyone else that may encounter a similar situation.
I had to go into the Privacy settings of my (MacOS) operating system, and permit the SFML application to have control of the computer before it would allow a call to Mouse::setPosition.
Thanks again!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Mouse motion help [SOLVED]
« Reply #5 on: June 12, 2024, 08:32:28 am »
Yes, on macOS you'll need some permission set.

On Linux with Wayland setPosition will not work at all.

With SFML 3, we've added MouseMovedRaw which should provide values even when the mouse is hitting the side of a window.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse motion help [SOLVED]
« Reply #6 on: June 12, 2024, 03:12:03 pm »
Good to know!

As a matter of fact I'm still struggling with the mouse thing... I can successfully watch the cursor jump in test projects, but in the particular project where I'm trying to actually use it, I'm still getting no effects from Mouse::setPosition even though the application has a checkmark under the system privacy settings (and even though a few seemingly random times this application actually did execute the cursor jump!)...

Also, I didn't realize 3 was out, but I don't know if I'd want to make the switch mid-project...
« Last Edit: June 12, 2024, 03:13:38 pm by johnnywz00 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Mouse motion help
« Reply #7 on: June 12, 2024, 04:15:09 pm »
SFML 3 is still work in progress, so it isn't out yet. ;)

Maybe when running it under a debugger, the debugger needs the permission as well?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse motion help
« Reply #8 on: June 13, 2024, 05:03:00 am »
I think what's happening is that every time I rebuild and run after adding a few tweaks to the code (using XCode), the computer sees it as a separate application and denies access again. That could be quite an impediment to continuing to develop the game project, but at least I think I know what's going on.
Is there any kind of workaround for this? Thanks again for the help. I have really enjoyed tinkering with SFML!
« Last Edit: June 13, 2024, 05:09:44 am by johnnywz00 »