Skip to main content
gamedev.net
🔒 Locked

Picking Problem

Started by BornToCode Dec 21, 2005 at 5:59 PM 1 replies 950+ views
Original Post
BornToCode
BornToCode
the Problem tbat i am having is that sometime when i do a mouseclick on the mesh the intersection does not get register unless i click in the center of the screen, here is my code below.

bool IsHit(LPD3DXMESH mesh,LPDIRECT3DDEVICE9 device,D3DVIEWPORT9 v)
{
D3DXVECTOR3 RayPos;
D3DXVECTOR3 RayDir;
POINT Mouse;
GetCursorPos(&Mouse);
D3DXMATRIX Projection;
D3DXMATRIX View;
D3DXMATRIX World;
D3DXMATRIX Local;
D3DXVECTOR3 m;
BOOL hit=FALSE;
device->GetTransform(D3DTS_PROJECTION,&Projection);
device->GetTransform(D3DTS_VIEW,&View);
device->GetTransform(D3DTS_WORLD,&World);
D3DXMATRIX NView=World*View;
D3DXMatrixInverse(&Local,0,&NView);

m.x=(((2*Mouse.x)/v.Width)-1)/Projection._11;
m.y=-(((2*Mouse.y)/v.Height)-1)/Projection._22;
m.z=1.0f;

RayDir.x=(m.x*Local._11)+(m.y*Local._21)+(m.z*Local._31);
RayDir.y=(m.x*Local._12)+(m.y*Local._22)+(m.z*Local._32);
RayDir.z=(m.x*Local._13)+(m.y*Local._23)+(m.z*Local._33);
RayPos.x=Local._41;
RayPos.y=Local._42;
RayPos.z=Local._43;
D3DXIntersect(mesh,&RayPos,&RayDir,&hit,0,0,0,0,0);
return hit;
}

Mite51
Mite51
The generation of the ray looks correct ( comparing it to my own code ). That leaves either the viewport, the model, or the device state. If the model has a transform when rendered you have to apply the inverse to the ray. If the viewport is sized 0,0 then it might cause something like what you have desribed.

Its probably not good practice to get the current transforms from the D3D device. They may not be setup as they were when an object was rendered. For example if you rendered an object then some GUI element ( like DXUT stuff ) it may be messing around with transforms and viewports at the end of the render, which is where the state has been left when you get a mouse input message/event.

Jason
CycloCode
CycloCode
Unless your in full screen you might want todo :

POINT Mouse;GetCursorPos(&Mouse);ScreenToClient(hWndofrenderer, &Mouse);


And if so, you might wanna avoid testing if out of the hwnd :

RECT tRect;GetWindowRect(hWndofrenderer, &tRect);if (Mouse.x < 0 || Mouse.y < 0 ||    Mouse.x > (tRect.right - tRect.left) ||    Mouse.y > (tRect.bottom - tRect.top)){ return false; }


I hope this helps :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.