Mouse.setGrabbed() does not seem to do what it should

Started by Hotshot5000, August 19, 2013, 22:20:18

Previous topic - Next topic

Hotshot5000

I am using libgdx which uses lwjgl and try to catch the cursor using setCursorCatched() which in turn calls Mouse.setGrabbed(). The data passes unfiltered through libgdx from lwjgl but on the platforms I have tested (Windows 7 and Ubuntu 13.04) it the values seem to be invalid:

The coordinates seem to return some form of absolute coordinates based on the center of the screen but even if the screen is let's say 480x320 moving from the center to the utmost right, X should be 240 but it's not. And the cursor is not confined to the screen as the coords increase if for example I continue to move to the right of the screen when I reach its limit.

This is the thread in libgdx forums where I have raised the issue: http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=10500 if you want to contribute to that discussion. It seems it's either a bug or a documentation bug so far.

quew8

This is directly from the documentation for Mouse.setGrabbed():
http://www.lwjgl.org/javadoc/

QuoteIf grab is false, the getX() and getY() will return delta movement in pixels clamped to the display dimensions, from the center of the display.

Admittedly, that should be "grab is true," but I think you get the idea. So how do you know where the cursor is when it is grabbed? Grabbed means that it is hidden and it can't really go outside the display when it is.

quew8

For the record, in case it has an effect on your problem.

1) Mouse coords can be clipped to the display dimensions using Mouse.setClipMouseCoordinatesToWindow() but I don't believe this has any effect on grabbed cursors.

2) You can allow or disallow negative coords with a LWJGL hidden switch. The last line of this page should explain: http://www.lwjgl.org/wiki/index.php?title=LWJGL_Hidden_Switches

Hotshot5000

int x = Mouse.getEventX();
int y = Gdx.graphics.getHeight() - Mouse.getEventY() - 1;
int button = Mouse.getEventButton();

TouchEvent event = usedTouchEvents.obtain();
event.x = x;
event.y = y;
event.button = toGdxButton(button);
event.pointer = 0;
event.timeStamp = Mouse.getEventNanoseconds();


When setGrabbed(true) the getEventX() and getEventY() return the absolute coordinates from the center of the screen (or they should from what I understand), but the coordinates are off. Going from center at x == 0 to the absolute right of the screen and then back to the center I get a negative x when it should be back to 0. A big negative number like -90, so it can't just be that I move too far out of the window.

Hotshot5000

Bump... anyone? This is important! When the mouse is grabbed you do not receive correct coords!

Fool Running

The coordinates are in delta coordinates (i.e. how much the mouse moved since the last call). You can't get the exact coordinates when the mouse is grabbed, you need to start somewhere (like the middle of the screen) and keep track of how much the mouse moved each frame to get your coordinates.

So if you assume the mouse is in the middle of the screen then get a +20, then you would add 20 to your previous mouse coordinates to get the new coordinates. If you then get a -15 you would then subtract 15 from your last coordinates (i.e. you would be at +5 from where you originally started in the middle of the screen).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Hotshot5000

Quote from: Fool Running on August 26, 2013, 16:28:07
The coordinates are in delta coordinates (i.e. how much the mouse moved since the last call). You can't get the exact coordinates when the mouse is grabbed, you need to start somewhere (like the middle of the screen) and keep track of how much the mouse moved each frame to get your coordinates.

So if you assume the mouse is in the middle of the screen then get a +20, then you would add 20 to your previous mouse coordinates to get the new coordinates. If you then get a -15 you would then subtract 15 from your last coordinates (i.e. you would be at +5 from where you originally started in the middle of the screen).

Well getEventX() and getEventY() don't seem to be in delta coords, just return the coords from the center of the screen. So if you move +20 you get 20 if you move -5 you get 15. And even then, it doesn't work correctly. Moving 200 to the right and -200 doesn't get you back at 0. It doesn't seem to be related to mouse acceleration, because this works wrong even with mouse acceleration disabled in Windows. But it is an acceleration issue: if I move the mouse 2 cm to the right slowly and then 2 cm to the left but fast, I don't get the same coords. The movement speed greatly affects the returned coordinates.

quew8

Quote from: quew8 on August 20, 2013, 09:13:27
So how do you know where the cursor is when it is grabbed? Grabbed means that it is hidden and it can't really go outside the display when it is.

I'll ask again. There might as well be no cursor when you grab the mouse. The only way to know how the mouse is moving should be through the very methods you say are wrong, so how do you know they are wrong?

Hotshot5000

Quote from: quew8 on August 27, 2013, 10:14:07
Quote from: quew8 on August 20, 2013, 09:13:27
So how do you know where the cursor is when it is grabbed? Grabbed means that it is hidden and it can't really go outside the display when it is.

I'll ask again. There might as well be no cursor when you grab the mouse. The only way to know how the mouse is moving should be through the very methods you say are wrong, so how do you know they are wrong?

I draw a pointer with opengl, it isn't that hard. And I know the window is 480x320. So I start with the pointer in the center and then move to the utmost right for example. I should have 240 as X coord. And I don't. Sometimes not even close. Then moving back to the center I should have 0. Again, I don't