# How to get relative mouse coordinates when GLUT return absolute

• 04-25-2006
joeprogrammer
How to get relative mouse coordinates when GLUT return absolute
I've just been trying to add mouse input to my 3D world, but I've been running into some problems with GLUT mouse input. When I used to use DirectInput, it let you choose the option of returning relative coordinates, so if you wanted to compute player position, you could do something like this:
Code:

```Player.x += iAmountMoved.x; Player.y += iAmountMoved.y;```
However, when GLUT returns mouse coordinates, they are in absoloute coordinates, whithin the range of the user's screen res. So I thought, "No problem; I can just subtract the previous location of the mouse to get relative coordinates."

But of course I didn't think of the problem that I get when the mouse reaches the screen's edge. So my mouse-input method only works as long as the mouse is not at the edge of the screen. Not very practical for a FPS.

So now what? Is there a way to request relative coordinates from GLUT, or should I just scrap this idea and start over using something more flexible, like SDL?
• 04-25-2006
Perspective
set your stored position to the mouse position each time, then you can calculate the difference between your stored position and the new position the next time the call back is called to get the movement.
• 04-25-2006
Magos
Quote:

Originally Posted by Perspective
set your stored position to the mouse position each time, then you can calculate the difference between your stored position and the new position the next time the call back is called to get the movement.

As he explained, that won't work. If screen res is 1024x768 and mouse X is at 1023 (assumin 0-based coordinates). Moving it 10 pixels to the right will still keep it at 1023, thus 1023 - 1023 == 0 != 10.
• 04-25-2006
JaWiB
Except that most (or at least a lot of) FPS's don't move the cursor. Instead, they leave the cursor in the center of the screen and just rotate everything based on how much the mouse has moved.

Still, there must be a way to allow the cursor to move (i.e., get the absolute coords). I remember having this problem a long time ago and I don't remember how/if I ever solved it.

Edit: Ok I guess even in an FPS you'd need to move the cursor for menus, but who needs those?
• 04-26-2006
The Wazaa
You could create a hook for the mouse, but that is unfortunatly not platform independant. At least not in the way I'd do it. Personally, I set the mouse to be in the center after each frame and I guess it's just to skip that line if you are in a menu.
• 04-26-2006
Magos
Quote:

Originally Posted by JaWiB
Except that most (or at least a lot of) FPS's don't move the cursor. Instead, they leave the cursor in the center of the screen and just rotate everything based on how much the mouse has moved.

And exactly how would you know how much to rotate without the relative mouse movement??? I think you got his question backwards.
• 04-26-2006
ElastoManiac
Have to agree with Wazaa, if you can change the current mouse position from within code. Then you can set it in center in every frame. That way it's easy to calculate the relative movment.
If there isn't such a function in GLUT ( i'm not sure ). You can initialize your window mannualy, using <windows.h> you can get just about anything about mouse. For such initialization look for NeHe tutorials.
I preffer GLUT over Windows myself...
• 04-26-2006
joeprogrammer
So there's no way to reset the cursor using GLUT? I sure couldn't find anything.

EDIT: And I'm using Mac OS X, so don't tell me how to reset using Windows ;)
• 04-26-2006
ElastoManiac
I found those in "glut.h", dunno what they do but check them out :
Code:

```extern void APIENTRY glutMouseFunc(void (*func)(int button, int state, int x, int y)); extern void APIENTRY glutMotionFunc(void (*func)(int x, int y)); extern void APIENTRY glutPassiveMotionFunc(void (*func)(int x, int y)); extern void APIENTRY glutEntryFunc(void (*func)(int state));```
• 04-26-2006
joeprogrammer
Yeah, I've already checked those out. Those are simply callback functions that get called, more-or-less, when the mouse does something. They do not really change the position of the mouse.
• 04-27-2006
ElastoManiac
• 04-27-2006
psychopath
Use glutWarpPointer() to set the mouse position to the center (after you calculate the screen center coords). Use glutMotion() to actually move the mouse. glutMotion() gives you the current mouse coords. All you have to do is calculate how much the mouse moved.
• 04-27-2006
joeprogrammer
OK, thanks that worked. I'm going to hide the cursor now (I'll have to look for a function in GLUT to do that too), because it looks silly when the mouse is seen wiggling around like that :)
• 02-10-2009
taur
Quote:

Originally Posted by joeprogrammer
OK, thanks that worked. I'm going to hide the cursor now (I'll have to look for a function in GLUT to do that too), because it looks silly when the mouse is seen wiggling around like that :)

glutSetCursor(cursor_state=GLUT_CURSOR_NONE)
• 02-10-2009
VirtualAce
First you only want to use DirectInput for non-GUI type interactions. It is complete junk for GUI or 2D coordinate information since it works in device units rather than pixels.

Second you really don't need to tell DirectInput to do relative coordinates since this can be easily implemented in your own code.

Code:

```struct MousePositionData {     int x;     int y;     int prev_x;     int prev_y;     int rel_x;     int rel_y; }; ... ... void DXMouse::Update(bool useDirectInput) {   if (useDirectInput)   {       DIMOUSESTATE mouseState;       m_pDirectInputDevice8->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mouseState);       m_MousePositionData.x += static_cast<int>(mouseState.lX);       m_MousePositionData.y += static_cast<int>(mouseState.lY);         m_MousePositionData.rel_x = m_MousePositionData.x - m_MousePositionData.prev_x;       m_MousePositionData.rel_y = m_MousePositionData.y - m_MousePositionData.prev_y;       m_MousePositionData.prev_x = m_MousePositionData.x;       m_MousePositionData.prev_y = m_MousePositionData.y;   }   else   {       //...Win32 mouse code snipped   } } void DXMouse::Center(int cx,int cy) {   POINT pt;   pt.x = cx;   pt.y = cy;   SetCursorPos(int cx,int cy);     m_MousePositionData.x = pt.x;   m_MousePositionData.y = pt.y;   m_MousePositionData.prev_x = pt.x;   m_MousePositionData.prev_y = pt.y;   m_MousePositionData.rel_x = 0;   m_MousePositionData.rel_y = 0; } void DXMouse::Init() {   //...snipped device setup code   Center(); } ... ...```
EDIT:
Of course this is a DirectInput option which you said you are no longer using. But just showing that you don't need the driver to get relative mouse coords.