Jump to content
  • Advertisement
Sign in to follow this  

Mouse picking slightly inaccurate

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ello all, I've got some problems with picking, I've got it working but all my picks are slightly off target. The picking seems to work perfectly from the window coordinates 0,0 but as soon as I try anywhere else the actual location of the pointer and the location of the ray hit are increasingly wrong - worst in the bottom right of the window. Here's the code I'm using:
//SetCapture called in message handler
if( GetCapture() )
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( windowHandle, &ptCursor );

v.x =  ( ( ( 2.0f * ptCursor.x ) / winWidth  ) - 1 ) / projection._11;
v.y = -( ( ( 2.0f * ptCursor.y ) / winHeight ) - 1 ) / projection._22;
v.z =  1.0f;

D3DXMatrixInverse( &m, NULL, &view );

rayFromMouse.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
rayFromMouse.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
rayFromMouse.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
rayFromMousePos = camera->getPosition();


Does anyone know what I'm doing wrong? Cheers for any help, Duncan

Share this post

Link to post
Share on other sites
This happend to me when my client space is smaller than I thought.

Repro of problem:

1. Window was 640x480.
2. Client area of window is slightly smaller than 640x480 due to title bar, etc.
3. Created D3D surface using the default values for the window. (which uses the client area size).
4. My mouse positioning calculations used 640x480 ans the size of the surface and not the actual client size.

In step 3 I created the D3D surface to be of the proper size, 640x480, and everything lined up again.

Share this post

Link to post
Share on other sites
Are you running in fullscreen or windowed mode? If your running in windowed mode, are you sure the actual drawing area is what you think it is? I mean the window frame and titlebar etc takes away some of the actual drawing space. That might cause a small offset.

This was my problem, and took about a week to finally figure out. doh.

Share this post

Link to post
Share on other sites
Cheers for the replies, I've tried getting the exact (windowed) viewport size by getting the backbuffer and using the width and height from there - but it's still inaccurate. How are you getting your values of width and height?

Share this post

Link to post
Share on other sites
Thanks, that works perfectly. For anyone who searches the forum and needs a code sample:

RECT rect = { 0, 0, eng->winWidth, eng->winHeight };
if (eng->windowed) AdjustWindowRectEx(&rect, WS_CAPTION | WS_SYSMENU, false, WS_EX_APPWINDOW);

//try to create a window using class
eng->windowHandle = ::CreateWindow("DuncEngine", title, // class name, title
WS_EX_TOPMOST, //style
0, 0, rect.right-rect.left, rect.bottom-rect.top, //x,y,w,h
0, //no parent
0, //no menu
instance, //instance given
0); //no extra mem

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!