Thread: mouse input/aiming FPS style using glut

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    mouse input/aiming FPS style using glut

    I'm trying to work on a simple FPS style game (only aiming, no translational movements) and I'm having a hard time with getting the correct mouse interaction. Mainly I don't know how to set it up where the mouse cursor/crosshair stays in the center of the window while the scene rotates (currently I can move around, but once the mouse cursor gets to the edge of the screen it stops, and it's annoying to have the cursor moving around). I know there are functions like glutWarpPointer and SetCursorPos, but then I can't seem to move at all.

    Can anyone help point me in the right direction, or is this an effort in futility with glut? Thanks for any help.

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Im no GLUT pro, but I'm pretty sure that for FPS style, the idea is to move the screen not the mouse. So if viewing rectangle is currently (100, 100, 150, 150) (x, y, w, h) and you receive mouse input, you would then just subtract/add left/right input to the X value and up/down input to the Y value, then re-calculate the view and redraw the frame.

    It should be noted though, that there are FPSs that implement both targetting styles at the same time (sticky centre and point anywhere) (yes I made up those names, someone find me the real terms)

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    You're not really moving the screen as such; your changing your projection matrix by rotating your camera vectors (up, forward, left).

    You want to use glutWarpPointer() at the end of your view rotation function (which sounds like you have working) to reset the cursor, and use the delta of the mouse movement between the current frame and the last (from the screen's centre) to determine how much to rotate the camera.

    Code:
    void RotateView(){
         PONT cursor = GetCursorPos(); /* has a GLUT equivalent I think, but I can't remember what it is */
    
         int deltaX = cursor.x - middleX
         int deltaY = cursor.y - middleY;
    
         glutWarpPointer(middleX, middleY); /* could also use SetCursorPos() if you're only going to use Windows */
    
         YourRotationFunctionThatTakesDelta(deltaX, deltaY);
    }
    Last edited by psychopath; 10-24-2007 at 08:09 AM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Basically what I have now for the function passed to glutPassiveMotionFunc is...(I know, not the best math for computing the rotation amount...maybe some tips on that too?):
    Code:
    void mouseMove(int x, int y) 
    {
    
      changex = x - prevx;
      changey = y - prevy;
      prevx = x;
      prevy = y;
    
      rotatex = rotatex + 0.25f*changey;
      rotatey = rotatey + 0.25f*changex;
    
     
      //glutWarpPointer(middlex, middley);
     
    }
    If I use glutWarpPointer there then glutPassiveMotionFunc seems to recall mouseMove and it just loops over on itself holding my cursor in the middle and not moving the scene.

    Also, I'm currently using the rotatex and rotatey to move my scene around instead of the camera. Do I not want to do it that way?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You should have a camera class or orientation class that handles 3D rotations. You would pass the deltas to functions such as Roll(), Pitch(), and Yaw().

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by Bubba View Post
    You should have a camera class or orientation class that handles 3D rotations. You would pass the deltas to functions such as Roll(), Pitch(), and Yaw().
    Sorry, I know I'm a terrible programmer, but that doesn't seem to help with the issue I'm having

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I'm not sure how one takes offense to my post but that was not the point.

    Read some books on 3D mathematics.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    n/m, I thought you were referring to a different issue.

    I did manage to get it working, although it still seems like a hack way of doing it:

    Code:
    void mouseMove(int x, int y)
    {
    	if(!warped)
    	{
    		changex = x - GW/2;
    		changey = y - GH/2;
    
    		//keep camera from rotating beyond vertically up/down
    		if(rotatex>=-90 && changey < 0)
    			rotatex = rotatex + 0.25f*changey;
    		else if(rotatex<=90 && changey > 0)
    			rotatex = rotatex + 0.25f*changey;
    
    		rotatey = rotatey + 0.25f*changex;
    		warped = true;
    		glutWarpPointer(GW/2, GH/2);
    	}
    	else
    		warped = false;
      glutPostRedisplay();
    }
    Thanks for the help

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    One other issue, unrelated - if I try to use glutEnterGameMode() I can enter and exit successfully, but in game mode full screen it won't go into 16 or 32bit color depth, only 8bit and it acts like it's software rendering. Both windowed and glutFullScreen() look correct and run quickly. I enter using the code:

    Code:
    	#ifndef WIN32
    		glutGameModeString("1280x1024:32@60");
    	#else
    		glutGameModeString("width=1280;height=1024;bpp=32;");
    	#endif
    		glutEnterGameMode();

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm not an OpenGL man so I'll try to address this without using code from D3D or OGL.

    For moving the mouse you will want something like this:

    Code:
    struct Mouse
    {
      int x;
      int y;
      int button_down;
      int lastx;
      int lasty;
    
      Mouse():x(0),y(0),button_down(0),lastx(0),lasty(0) { }
    };
    
    
    
    void InputMgr::Turn(float fRadians)
    {
      GetMouseData();
    
      int diffx=TheMouse.x-TheMouse.lastx;
     
      if (CameraMgr.GetActiveType()==PLAYER) CameraMgr.Yaw((float)diffx*Options.fMouseSmoothX);
    }
    I'm not sure how else I can explain it.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Yes, reading is an important skill too. All too often people are referred to the How To Ask Questions The Smart Way page. I think we need a "how to read smartly asked questions properly" page too. The poster clearly stated that he "don't know how to set it up where the mouse cursor/crosshair stays in the center of the window while the scene rotates".

    Anyway, the glutWarpPointer is a good way to solve the problem. IIRC the linux port of glQuake uses a similar method but it is based on XWarpPointer as it doesn't use GLUT. And AFAIK there is no mouse grabbing feature present in GLUT. If you want a "less hacky" solution, you might want to give SDL a try. Otherwise your solution should be fine.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    While we appreciate your input please do not bump old threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get relative mouse coordinates when GLUT return absolute
    By joeprogrammer in forum Game Programming
    Replies: 14
    Last Post: 02-10-2009, 06:35 PM
  2. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM
  3. Game update...
    By jdinger in forum Game Programming
    Replies: 14
    Last Post: 11-08-2002, 07:10 AM
  4. SkyLock graphics demo (scrolling, etc.)
    By jdinger in forum Game Programming
    Replies: 9
    Last Post: 06-30-2002, 08:18 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM