Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect cursor position with GLFW_CURSOR_DISABLED #1523

Open
andrey0722 opened this issue Jun 27, 2019 · 1 comment
Open

Incorrect cursor position with GLFW_CURSOR_DISABLED #1523

andrey0722 opened this issue Jun 27, 2019 · 1 comment
Assignees
Labels
bug Bug reports and bugfix pull requests input Keyboard, joystick or mouse verified Reproduced or otherwise verified bugs Windows Win32 specific (not Cygwin or WSL)
Projects

Comments

@andrey0722
Copy link

andrey0722 commented Jun 27, 2019

Hello,

I'm using GLFW 3.2.1 on Windows as follows:

  • One window is created with CursorPosCallback and MouseButtonCallback set
  • on left mouse pressed GLFW_CURSOR_DISABLED mode is set
  • on left mouse released GLFW_CURSOR_NORMAL mode is set

When I press left mouse during rapid mouse movement inside the window sometimes cursor hops to strange coords and smooth camera movement breaks. The most notable effect is achieved closer to window's border.

I debugged the library and tried to investigate the problem. Here what I found:

On left mouse pressed glfwSetInputMode(GLFW_CURSOR_DISABLED) centers real cursor and updates virtual. After that the windowProc on GLFW_CURSOR_DISABLED expects cursor somewhere around win32.lastCursorPos (which is already centered). But on first glfwPollEvents() call after disabling cursor it gets cursor around the original position where GLFW_CURSOR_DISABLED mode was set (which is not centered). And it gives incorrect delta, so virtual cursor hops far away. On next glfwPollEvents() calls the cursor position is around center as expected.

Once I've commented out centerCursor() call inside _glfwPlatformSetCursorMode this issue doesn't reproduce anymore. In this case when windowProc is called on the first time the win32.lastCursorPos is not centered and delta is correct.

My guess is that SetCursorPos has already centered cursor but there are some "old" WM_MOUSEMOVE messages in event queue (because cursor moves rapidly at the moment ofGLFW_CURSOR_DISABLED setting) that are not affected by cursor moved.

Looks like similar behavior already has been described in #193 and #1269.

What do you think? Is it a bug?

@elmindreda elmindreda self-assigned this Jun 27, 2019
@elmindreda elmindreda added the Windows Win32 specific (not Cygwin or WSL) label Jun 27, 2019
@floooh
Copy link

floooh commented Jul 20, 2020

In case it helps (even if the issue is about Windows): I found this issue googling for a similar problem I'm seeing in my macOS mouse-lock code (I also looked at how GLFW does it before). I'm seeing a similar jump for the deltaX/deltaY values for exactly one mouseMoved event shortly after mouse lock is activated, usually if the mouse was moving quickly while activating mouse-lock, but it's not the very first event in my case, but the second after mouse lock is activated.

Everything seems to work fine when simply not centering the mouse via CGWarpMouseCursorPosition (I thought that this would be a problem when the mouse is already at the edge of the screen when the mouse lock is activated, but it seems to work fine).

So what I'm doing now (which seems to fix the issue):

On mouse-lock enable:

        [NSEvent setMouseCoalescingEnabled:NO];
        CGAssociateMouseAndMouseCursorPosition(NO);

...and on mouse-lock disable:

        CGAssociateMouseAndMouseCursorPosition(YES);
        [NSEvent setMouseCoalescingEnabled:YES];

...but no CGWarpMouseCursorPosition at all! I've read somewhere else that the 'setMouseCoaliscingEnabled' would improve mouse event latency and/or frequency, but I haven't checked in detail whether this is actually true.

(...and what's missing is making the cursor invisible).

While mouse-lock is active in the mouseMoved event handler (and mouseDragged, etc...), I just look at [event deltaX] and [event deltaY] values (these are also correct when the mouse is at the edge of the screen at the start of mouse look, so it seems like CGAssociateMouseAndMouseCursorPosition() does the trick).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug reports and bugfix pull requests input Keyboard, joystick or mouse verified Reproduced or otherwise verified bugs Windows Win32 specific (not Cygwin or WSL)
Projects
Queue
Todo
Development

No branches or pull requests

3 participants