Thread: How to get relative mouse coordinates when GLUT return absolute

  1. #1
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499

    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?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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?
    Last edited by JaWiB; 04-25-2006 at 10:16 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    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.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    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...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  8. #8
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    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

  9. #9
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    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));
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  10. #10
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    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.

  11. #11
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    too bad...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  12. #12
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    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.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  13. #13
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    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

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    1
    Quote Originally Posted by joeprogrammer View Post
    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)

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 02-12-2009 at 01:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM