Original Post
Hi. i have pick ray working now and what i want is that when a onject is clicked on the mouse will be position over the center of the object. I thought of doing this by finding the ray direction between the origin and the meshs center position in 3D space. Then reverse the usual pick ray sequence to find the co-ordinates of the mouse. Below is the code i have used for this The problem seems to be getting the ray direction correct. At present im dividing each vector component by 1.05 until each part is less than 1.0. This is inefficient and means that neither of the direction components can be greater than 1 which in some cases when ray picking it can be. I believe this to be the only reason why the function does not work. Is there a method to get a ray direction vector from two co-ordinates? It needs to be done in reverse so it obviously can not be found in the same way as the ray picking. Thank you Stuart EDIT: Please remember to use [source] tags... [Edited by - jollyjeffers on May 22, 2006 5:46:38 AM]
void Engine::positionMouseForPicked(LPDIRECT3DDEVICE9 g_pd3dDevice, D3DXVECTOR3 meshPos)
{
D3DVIEWPORT9 m_mainViewport;
g_pd3dDevice->GetViewport( &m_mainViewport );
D3DXMATRIX matProj;
g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( h_Wnd, &ptCursor );
D3DXVECTOR3 rayOrigin,rayDir, v;
D3DXMATRIX matView, m;
if ( currentCamState == state_cam_deck1 )
{
rayOrigin.x = 0.0f; rayOrigin.y = 150.0f; rayOrigin.z = 0.0f;
}
float tempDiffX = meshPos.x - rayOrigin.x;
float tempDiffY = meshPos.y - rayOrigin.y;
float tempDiffZ = meshPos.z - rayOrigin.z;
while ( tempDiffX > 1.0f || tempDiffX < -1.0f || tempDiffY > 1.0f || tempDiffY < -1.0f || tempDiffZ > 1.0f || tempDiffZ < -1.0f )
{
tempDiffX /= 1.005; tempDiffY /= 1.005; tempDiffZ /= 1.005;
}
rayDir.x = tempDiffX; rayDir.y = tempDiffY; rayDir.z = tempDiffZ;
g_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );
D3DXMatrixInverse( &m, NULL, &matView );
v.x = rayDir.x/m._11;
v.y = (rayDir.y - m._32)/m._22;
ptCursor.x = (((v.x * matProj._11) + 1)*m_mainViewport.Width)/2.0f;
ptCursor.y = ( m_mainViewport.Height * ( 1 - (v.y * matProj._22) ) ) / 2.0f;
SetCursorPos( ptCursor.x, ptCursor.y );
}