I have this problems with picking ray on multiple mesh:
1) if I get the world matrix in this way (with GetTrasform) , the picking result is wrong when I traslate or scale the meshes (work on original position or size)
2) Else if I pass in the function the single meshes 's world matrix (array) the result is correct but if I scale the mesh, the ray pick more distant mesh and I have to get close for get the correct result.
Here is my code (I have other function that get the closest mesh, but I am sure that the problems is here):
[source lang="cpp"]if(m_Input::MouseButtonDown(0)) { g_D3DDevice_->GetTransform(D3DTS_PROJECTION, &matProj ); g_D3DDevice_->GetTransform(D3DTS_VIEW, &matView); g_D3DDevice_->GetTransform(D3DTS_WORLD, &matWorld); g_D3DDevice_->GetViewport( &m_Viewport ); D3DXMatrixInverse(&ImatView, NULL, &matView ); D3DXMatrixInverse(&ImatWorld,NULL,&matWorld); v.x = ( ( ( 2.0f * Cursor.x ) / m_Viewport.Width ) - 1 ) / matProj._11; v.y = -( ( ( 2.0f * Cursor.y ) / m_Viewport.Height ) - 1 ) / matProj._22; v.z = 1.0f; rayDir.x = v.x*ImatView._11 + v.y*ImatView._21 + v.z*ImatView._31; rayDir.y = v.x*ImatView._12 + v.y*ImatView._22 + v.z*ImatView._32; rayDir.z = v.x*ImatView._13 + v.y*ImatView._23 + v.z*ImatView._33; rayOrigin.x = ImatView._41; rayOrigin.y = ImatView._42; rayOrigin.z = ImatView._43; D3DXVec3TransformCoord(&rayOrigin,&rayOrigin,&ImatWorld); D3DXVec3TransformNormal(&rayDir,&rayDir,&ImatWorld); D3DXVec3Normalize(&rayDir,&rayDir); D3DXIntersect(g_Mesh[NumModel], &rayOrigin, &rayDir, &picked, NULL, NULL, NULL, &dist, NULL, NULL); if(picked) { NumMeshHit[NumHit] = NumModel; MeshDist[NumHit] = dist; NumHit++; } }[/source]
Sorry again for my bad English.
Edited by MefistoManna, 26 June 2012 - 08:04 AM.






