# [C++] Problems with Pick function

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

## Recommended Posts

im having problems getting the pick sample from the SDK to work on my game, it line of the pick seems way off from it is supposed to be. check this screenshot the white line is drawn between the ray origin and the ray direction.. for this test i clic on the "car" but all the lines are drawn on the same place or really close ( changing the position acording to where i clic on screen ) here is the code used..
void CGameLogic::SelectUnitAt(POINT pMousePosition)
{
LPD3DXMESH	pMesh;
CUnit* tempUnit = static_cast<CUnit*>(*it);
LPD3DXMESH g_Mesh = tempUnit->GetMesh()->GetMesh();
g_Mesh->CloneMeshFVF(D3DXMESH_MANAGED, g_Mesh->GetFVF(), TheGraphics.GetDevice(), &pMesh);

D3DXVECTOR3 vPickRayDir;
D3DXVECTOR3 vPickRayOrig;

const D3DXMATRIX *pmatProj = &TheGraphics.GetCamera().GetProjectionMatrix();

POINT ptCursor = pMousePosition;

// Compute the vector of the Pick ray in screen space
D3DXVECTOR3 v;
v.x =  ( ( ( 2.0f * ptCursor.x ) / TheSettigs.m_iScreenWidth ) - 1 ) / pmatProj->_11;
v.y = -( ( ( 2.0f * ptCursor.y ) / TheSettigs.m_iScreenHeight ) - 1 ) / pmatProj->_22;
v.z =  1.0f;

// Get the inverse view matrix
const D3DXMATRIX matView = *(&TheGraphics.GetCamera().GetViewMatrix());
const D3DXMATRIX matWorld = *(&TheGraphics.GetCamera().GetWorldMatrix());
D3DXMATRIX mWorldView = matWorld * matView;
D3DXMATRIX m;
D3DXMatrixInverse( &m, NULL, &mWorldView );
m = mWorldView;

// Transform the screen space Pick ray into 3D space
vPickRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
vPickRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
vPickRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;

TheSceneGraph.Vertices[0].x = vPickRayOrig.x;
TheSceneGraph.Vertices[0].y = vPickRayOrig.y;
TheSceneGraph.Vertices[0].z = vPickRayOrig.z;

TheSceneGraph.Vertices[1].x = vPickRayDir.x;
TheSceneGraph.Vertices[1].y = vPickRayDir.y;
TheSceneGraph.Vertices[1].z = vPickRayDir.z;

pMesh->GetVertexBuffer( &pVB );
pMesh->GetIndexBuffer( &pIB );

WORD*      pIndices;
D3DVERTEX* pVertices;

pIB->Lock( 0, 0, (void**)&pIndices, 0 );
pVB->Lock( 0, 0, (void**)&pVertices, 0 );

// When calling D3DXIntersect, one can get just the closest intersection and not
// need to work with a D3DXBUFFER.  Or, to get all intersections between the ray and
// the mesh, one can use a D3DXBUFFER to receive all intersections.  We show both
// methods.

// Collect only the closest intersection
BOOL bHit;
DWORD dwFace;
FLOAT fBary1, fBary2, fDist;
D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir , &bHit, &dwFace, &fBary1, &fBary2, &fDist,  NULL, NULL);

if( bHit )
{
static_cast<CUnit*>(*it)->SetSelected(true);
m_SelectedUnits.push_back(static_cast<CUnit*>(*it));
}
pVB->Unlock();
pIB->Unlock();

SAFE_RELEASE(pMesh);
SAFE_RELEASE(pVB);
SAFE_RELEASE(pIB);
}


thanks in advance and sorry for the long post

*bump*