Original Post
Hi all. I've got a problem with my "picking" code in DirectX. It works perfectly fine on any number of meshes _until_ I scale one of the meshes up or down. Here are some pictures and a description of what's happening in each (the mouse cursor is kind of hard to see, sorry bout that :D).
Here are two meshes. Obviously the yellow one was "picked" and is highlighted yellow. Both meshes are their original size, orientation and placement.

The next two screenshots have the floor mesh scaled by a factor of 5 all around. You can see that when selecting the vertical wall mesh, it still wants to select the ground mesh (the scaled mesh).


Here are two meshes. Obviously the yellow one was "picked" and is highlighted yellow. Both meshes are their original size, orientation and placement.

The next two screenshots have the floor mesh scaled by a factor of 5 all around. You can see that when selecting the vertical wall mesh, it still wants to select the ground mesh (the scaled mesh).


If I try this code with one model that is scaled, the picking works correctly, it only seems to not pick the right one when there are multiple meshes with one or more being scaled. Now, in my code, I am choosing the picked mesh by the closest distance to the cursor position. Here is some of my code. Is there anything glaringly wrong here?
bool CCollision::PickModel( LPDIRECT3DDEVICE9 displayDevice, LPD3DXBASEMESH mesh, D3DXMATRIXA16 *worldMatrix,
float *distance, D3DXVECTOR2 *cursorPos, D3DXVECTOR3 *hitPos )
{
D3DXMATRIXA16 matProj, matView;
displayDevice->GetTransform( D3DTS_PROJECTION, &matProj );
displayDevice->GetTransform( D3DTS_VIEW, &matView );
D3DXVECTOR3 v;
v.x = ( ( ( 2.0f * cursorPos->x ) / DISPLAY_SCREEN_WIDTH ) - 1 ) / matProj._11;
v.y = -( ( ( 2.0f * cursorPos->y ) / DISPLAY_SCREEN_HEIGHT ) - 1 ) / matProj._22;
v.z = 1.0f;
D3DXMATRIX m;
D3DXVECTOR3 rayOrigin,rayDir;
D3DXMatrixInverse( &m, NULL, &matView );
// Transform the screen space pick ray into 3D space
rayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
rayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
rayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;
rayOrigin.x = m._41;
rayOrigin.y = m._42;
rayOrigin.z = m._43;
// Use inverse of matrix
D3DXMATRIX matInverse;
D3DXMatrixInverse(&matInverse,NULL,worldMatrix);
// Transform ray origin and direction by inv matrix into this object's model space
D3DXVECTOR3 rayObjOrigin,rayObjDirection;
D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);
bool hasHit = false;
float hitU, hitV = 0.0f;
DWORD faceIndex = 0;
D3DXIntersect(mesh, &rayObjOrigin, &rayObjDirection, reinterpret_cast<BOOL*>(&hasHit), &faceIndex, &hitU, &hitV, distance, NULL, NULL);
// determine the hit position
if ( hasHit )
{
*hitPos = rayObjOrigin + (rayObjDirection * (*distance));
D3DXVec3TransformCoord( hitPos, hitPos, worldMatrix );
}
return hasHit;
}