[LWJGL 3] GLFW Cursor Input

Hey :slight_smile:

So I ran into a problem with polling input in LWJGL3, I want to have the mouse speed at each frame, so for that I use the following code in my update method:


DoubleBuffer xpos = BufferUtils.createDoubleBuffer(1);
DoubleBuffer ypos = BufferUtils.createDoubleBuffer(1);
GLFW.glfwGetCursorPos(window, xpos, ypos);
xpos.rewind();
ypos.rewind();
float dx = 640 - xpos.get();
float dy = 400 - ypos.get();
GLFW.glfwSetCursorPos(window, 640, 400);

The 640 and 400 come from my window dimensions (1280x800) to keep the mouse centered. Now the problem is the GLFW.glfwSetCursorPos does not seem to work as I can move my mouse freely with the whole scene rotating like crazy.
I am working with multithreading since I have 2 windows. With just having one window the code works just fine, but since the method gets the window as a parameter it shouldn’t be a problem to lock the cursor to that window no?

Additional Information:
-I did not create a cursor object from GLFW but it should work without that

Edit:

GLFW.glfwSetInputMode(display.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);

does not work either

Your ability to recenter the mouse depends on how often your event loop runs. In most cases, even without vsync, it’s not often enough. That’s why using glfwSetCursorPos is not meant to be used for centering the mouse. You should try to use glfwSetInputMode with GLFW_CURSOR_DISABLED.

A few other notes:

  • Using createDoubleBuffer in a loop is not efficient. Consider stack allocation (see MemoryStack) or caching the buffers.
  • You don’t have to rewind() or flip() after calling LWJGL methods.
  • GLFW_CURSOR_HIDDEN does not restrict mouse movement, within or outside the window. It simply makes the cursor invisible.

-am I still able to get mouse movement if I use GLFW_CURSOR_DISABLED? also I am calling glfwSetCursorPos every frame so …
-the DoubleBuffer was just for demonstration in the Code
-ah okey, have seen it in a tutorial so I just used it
-but the cursor doesn’t even get invisible, it just stays as usual

[quote=“LoonTech,post:3,topic:57249”]
Yes, you still get cursor events via the callbacks. You also don’t have to call glfwSetCursorPos at all.

[quote=“LoonTech,post:3,topic:57249”]
GLFW_CURSOR_HIDDEN hides the cursor when it passes over the client area of the window. If it moves to the border or outside the window, it is visible. Isn’t that what you see?

No, the cursor is visible all the time and the Setpos doesn’t work at all. I switched it to use GLFW_CURSOR_DISABLED but that doesn’t work either, it just stays normal, without the multithreading it works fine …

Is the GLFW context loaded into that thread when you execute? (if there is a context)

Yes it is, rendering and key input works fine, just the cursor modes don’t

The most likely reason is that you’re calling GLFW functions in threads that are only supposed to be called on the main thread. Please read the GLFW documentation and make sure you follow the rules. The LWJGL javadoc on each method also mentions if it can be called on any thread or only on the main thread. If you really do everything correctly and it still doesn’t work, I’d be very interested to see sample code the reproduces the problem.

GLFW glfwSetCursorPos() docs:

[quote]Do not use this function to implement things like camera controls. GLFW already provides the GLFW_CURSOR_DISABLED cursor mode that hides the cursor, transparently re-centers it and provides unconstrained cursor motion. See glfwSetInputMode for more information.
[/quote]
http://www.glfw.org/docs/latest/group__input.html#ga04b03af936d906ca123c8f4ee08b39e7