Thread: Mouse problems

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Mouse problems

    Hi folks;

    I am making a 3D application in OpenGL (in C) where the camera view is controlled by the mouse. Here is a snippit of the code:

    void aim_camera(int x, int y) {

    if (lastPos[0] == x && lastPos[1] == y)
    return;
    if (x < lastPos[0])
    yrot += 1.5f;
    if (x > lastPos[0])
    yrot -= 1.5f;

    if (y < lastPos[1]) {
    z -= 0.2f;
    lookupdown -= 1.0f;
    }

    if (y > lastPos[1]) {
    z += 0.2f;
    lookupdown += 1.0f;
    }

    lastPos[0] = x;
    lastPos[1] = y;
    }

    My question is this; when I get to the edge of the screen I have the obvious problem where I cannot rotate the view any more. I want to restore the mouse position to the middile of the screen when this happens. I understand there are functions out there that allow a person to do that but thus far I've not come up with anything. Can anyone out there help?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Two useful functions in the Win32 API are GetCursorPos and SetCursorPos.

    Why not try this: Get the x diff and y diff from the current cursor position and the center of the screen. Then use this to determine how much to rotate about the two axis. Then at the end of the function set the cursor to the center of the screen. This way you will never reach the problem you described because every time the function is called the distance from center is measured and then it is reset back to center. Give it a try I think you will be happy with it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All you need to do is make your camera movement's offsets on x and y directly proportional to how far the mouse is from the center of the screen.

    Code:
     
    //Get current position
    Mouse.Update();
     
    //Clamp in value only - does not actually move the mouse cursor
    if (Mouse.x>=WIDTH) Mouse.x=WIDTH;
    if (Mouse.y>=HEIGHT) Mouse.y=HEIGHT;
    if (Mouse.x<=0) Mouse.x=0;
    if (Mouse.y<=0) Mouse.y=0;
     
    //Compute speed based on distance from center of screen, frame time, and modifier
    CameraSpeed.x=(Mouse.x-ScreenCenter.x)*frameTime*SomeCoefficient;
    CameraSpeed.y=(Mouse.y-ScreenCenter.y)*frameTime*SomeCoefficient;
     
    ::SetCursorPos(Mouse.x,Mouse.y);
    Last edited by VirtualAce; 11-27-2004 at 08:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with mouse wheel...
    By xkrja in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2006, 02:22 PM
  2. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  3. mouse
    By stilllearning in forum C Programming
    Replies: 0
    Last Post: 04-15-2003, 03:34 PM
  4. Friggin Mouse on Laptop
    By OneStiffRod in forum Tech Board
    Replies: 2
    Last Post: 03-27-2003, 07:49 PM