Thread: Mouse Coordinates are Off

  1. #1
    Yah. Morgul's Avatar
    Join Date
    Feb 2005

    Mouse Coordinates are Off

    I'm working on capturing the mouse coordinates in a little game I'm making during some time off. I go to work on the ability to select different units, and I notice that my selection appears off. Upon further investigation, I can't find anything wrong with my code. So, I decide to output mouse coordinates to the screen so I can run some tests. The mouse starts at 0,0 in the top right perfectly fine. I have a small test map up, and I go to the corner of it which the game tells me is at 512,512. The mouse is telling its 508,484. So, I think maybe something is stretching my map. To test to see if maybe it's my map drawing algorithm that's off, I have a box drawn using 0,0 512,0 512,512 and 0,512 as the corners. It lines up with corners of what I have drawn, but not with the mouse.

    I'm using Direct3D 9, on Windows XP. Is there any reason anyone can think of that the coordinates might be off? In the far right corner of my window it tells me the coordinates are 795,567 and I figure that's about right for an 800x600 window taking the caption/system menu bar into account and the border.

    Here's how I query for my mouse coordinates:

    PHP Code:

    // ... handle coordinates and test for selection ... 

    I checked MSDN and they gave me no reason to believe that the coordinates which this give me should be wrong. I use the exact same method for WM_MOUSEMOVE so I can record the mouse coordinates on the screen for testing (Source). On that source, it mentions using GET_X_LPARAM and GET_Y_LPARAM, however I've seen it done correctly the way I am doing it, and MSVC++ 2005 Express Edition complains that they don't exist even though they are supposed to be in windows.h.

    My screen resolution is 1280x1024, but I doubt that has anything to do with it.

    Is there any reason the drawing could be stretched or something? I don't have any reason to believe that its a projection problem, however I'm open to any and all suggestions you might have. The back buffer is the same size as the window by the way, so it isn't becuase there is a size difference between the two.

    Thanks for any help in advance.
    Last edited by Morgul; 08-18-2006 at 11:56 AM.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Are you doing this by projecting the screen coordinates of the mouse into your 3D game space? If so, remember that the "screen" is your near clip plane, So your calculation shouldn't start at your camera position. It should start at your camera postion + near clip plane value.

    Or maybe you're not doing that, in which case I have no suggestions. Sorry =(

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    Sounds like a problem I had. I had a small window containing my directx window, and if I set the window size to the size I wanted, the dx window had the wrong resolution (so instead of 800x600, it was something weird and the mouse position didn't line up). The simple solution for me was to set the client size to the expected size. One possible method for that:
    void SetClientSize( HWND hwnd, int clientWidth, int clientHeight )
      if ( IsWindow( hwnd ) )
        RECT winRC,clientRC;
        GetWindowRect(hwnd,&winRC); //get the current window rect
        GetClientRect(hwnd,&clientRC); //get the current client rect
        int dx = (clientRC.right-clientRC.left)-clientWidth; //calculate difference between current client width and desired client width
        int dy = (; //same for height
        //adjust the size
    Hopefully that made some sense
    "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."

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    go with his answer lol

  5. #5
    Registered User
    Join Date
    May 2006
    If you are creating the window using CreateWindow(), did you call AdjustWindowRect() ? You should be creating your window like this:

    RECT rc;
    rc.left = 0; = 0;
    rc.right = 800;
    rc.bottom = 600;
    AdjustWindowRect(&rc, WS_OVERLAPPED_WINDOW, false);
    CreateWindow(/* ... */ , rc.right - rc.left, rc.bottom -, /* ... */ );
    Edit: Beaten. However, I might add that it's not weird at all to meet this problem since CreateWindow()'s width and height parameters are used for the *total* size of the window so the client area is smaller because there is a title bar, there are edges... AdjustWindowRect() resizes the RECT so that the width and height -- calculated as shown above -- match the width and height you want for the client area.
    Last edited by Desolation; 08-18-2006 at 09:57 PM.

  6. #6
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    My game is 2d, but thanks any skorman00.

    I tried both of your ideas, Desolation and JaWib, and though both made sense, JaWiB's fixed my problem. Sorry, Desolation.

    Thanks everyone.
    Sic vis pacum para bellum. If you want peace, prepare for war.

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. Finding mouse coordinates without disturbing its position
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-21-2008, 11:19 AM
  3. Replies: 2
    Last Post: 02-18-2008, 04:53 AM
  4. Retrieving mouse coordinates behind a window.
    By Kurisu33 in forum Windows Programming
    Replies: 3
    Last Post: 10-04-2006, 02:11 PM
  5. Screen coordinates and mouse pointer manipulation
    By Zewu in forum Windows Programming
    Replies: 1
    Last Post: 03-25-2005, 05:30 PM