Problem with directX picking, hits seem to be offset?

Started by
5 comments, last by Miikka_J 17 years, 9 months ago
Have been attempting to put together a mouse picking program and have been running into a couple of problems. My function is derived from the pick sample in the SDK but it seems to register hits slightly below and to the right of the object, as if the object has been drawn slightly off of where it should be. Problem persists in fullscreen and windowed, and also when using other peoples algorithms for the picking. Seems to be a consistent problem amoungst them. Thing is the picking demo itself works fine, could it be some sort of windows / d3d init problem (I don't use CD3D application but demo does). I wouldn't know where to look for that sort of thing. Any suggestions would be appreciated.
Advertisement
Are you using ScreenToClient() to translate the GetCursorPos()?

The window border and caption/title need to be compensated for. D3D's (0,0) is not the same as your window's (0,0).

If you look at the Pick sample, there is a pair of calls (run a "find" on 'screentoclient') that handle this - you need to correct for it before you start doing the normal maths stuff.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

ScreenToClient() and GetCursor() are already being used, did try compensating for the title with GetSystemMetrics() but the numbers I was getting back for that wern't correct.

Plus none of that explains the persistance of the problem in fullscreen.

Sorry, you can tell I've been working on this for a while.

You gotta make sure that your backbuffer width and height are exact for the actual client window, adjusted for the window frame's dimensions (title bar, menu, scrollbars, etc.). Fullscreen should be easier. In MFC, I get the WM_SIZE message and use it for the backbuffer dimensions. For Win32, I think you can do the same thing.

Oh yea, you also have to use the same width/height in your projection matrix.
--------------------------Most of what I know came from Frank D. Luna's DirectX books
POINT ptCursor;
GetCursorPos( &ptCursor );

D3DVIEWPORT9 mainViewport;
m_pd3dDevice->GetViewport( &mainViewport );

ScreenToClient( m_hWnd, &ptCursor );
mouseX = ptCursor.x - mainViewport.X;
mouseY = ptCursor.y - mainViewport.Y;
m_pd3dDevice->SetCursorPosition( ptCursor.x, ptCursor.y, 0 );

mouseX and mouseY are what i use for the co-ords for the mouse co-ords, compensates for the screen position

[Edited by - Stowelly on July 9, 2006 3:07:43 PM]
http://stowelly.co.uk/
When you created the window, you did use AdjustWindowRect() to specify the client area size to be the same as the backbuffer size, right?
Thanks for all the replies. What I ended up doing was adding the following code after the D3D initialization and in WM_SIZE in winproc to set the correct info in D3DPRESENT_PARAMETERS d3dpp:

HRESULT SetWindowSize()
{
RECT m_rcWindowClient; // Saved client area size for mode switches

// Update window properties
GetClientRect( hwnd, &m_rcWindowClient );

d3dpp.BackBufferWidth = m_rcWindowClient.right - m_rcWindowClient.left;
d3dpp.BackBufferHeight = m_rcWindowClient.bottom - m_rcWindowClient.top;

return S_OK;
}

Although I suspect I will change to AdjustWindowRect() to keep the ratio's right

This topic is closed to new replies.

Advertisement