Skip to main content
gamedev.net
🔒 Locked

Picking problem

Started by MefistoManna 3 replies 1.4k views
Original Post
MefistoManna
MefistoManna
I am using this code:

bool IsPicked()
{
BOOL pickedtmp;
if(m_Input_->MouseButtonDown(0))
{
//getting location of mouse relative to game window, not whole screen
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( hwnd, &ptCursor );
//getting viewport (p_Device is LPDIRECT3DDEVICE9)
D3DVIEWPORT9 view;
m_D3D::GetDevice()->GetViewport( &view );
//getting projection matrix
D3DXMATRIX projMat;
m_D3D::GetDevice()->GetTransform(D3DTS_PROJECTION,&projMat);
//calculating.. mouse ray
float vx = (+2.0f*ptCursor.x/view.Width - 1.0f)/projMat._11;
float vy = (-2.0f*ptCursor.y/view.Height + 1.0f)/projMat._22;
//Vector is D3DVECTOR
D3DXVECTOR3 Origin(0.0f, 0.0f, 0.0f);
// I used Z as my UP VECTOR, not sure how it will work for you
D3DXVECTOR3 Direction(vx, vy, 1.0f);
//getting projection matrix
D3DXMATRIX viewMat;
m_D3D::GetDevice()->GetTransform(D3DTS_VIEW,&viewMat);
//inversing projection matrix
D3DXMATRIX iviewMat;
D3DXMatrixInverse(&iviewMat, 0, &viewMat);
D3DXVec3TransformCoord(&Origin, &Origin, &iviewMat);
D3DXVec3TransformNormal(&Direction, &Direction, &iviewMat);
//setting variables
DWORD dwFace;
FLOAT fBary1, fBary2, fDist;


D3DXIntersect( g_Mesh, &Origin, &Direction, &pickedtmp, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL );

return pickedtmp;
}
}


it works, but in this situation, i have two mesh:
24mc74w.png

Now, when I click on the light, the mesh picked is first the street, and if I click again the light.
I want pick only the Light, because it is before.
Sorry, for my English.
It isn't my language
Sesha
Sesha
Hello mefistomanna,

Try clicking at other places of the screen(near light) if light gets selected at some other point then i think you have to tune your origin and direction vectors.

And also have a look at this http://toymaker.info/Games/html/picking.html.
MefistoManna
MefistoManna
Thx Sesha,
If I click in one place where there isn't the street behind, the light select correctly, else the selected mesh is light, street, light, street...
I don't know if I was clear.
ankhd
ankhd
use the distance you want the object nearest the viewer.rolleyes.gif

Topic Locked

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

Sign in to reply to this topic.