# 3D X,Y,Z Coordinates

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

## Recommended Posts

Hi All, I have written the below code to get the 3D X,Y,Z Coordinates of a 3D Object. Please let me know if i am right. void CTestDlg::DetectMouseCoordinates(CPoint point1) { // get the current transform matrices D3DXMATRIX matProjection, matView, matWorld, matInverse; d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection); d3ddev->GetTransform(D3DTS_VIEW, &matView); d3ddev->GetTransform(D3DTS_WORLD, &matWorld); float xAngle = (((2.0f * point1.x) / DISPLAY_SCREEN_WIDTH) - 1.0f) / matProjection(0, 0); float yAngle = (((-2.0f * point1.y) / DISPLAY_SCREEN_HEIGHT) + 1.0f) / matProjection(1, 1); D3DXVECTOR3 origin, direction; origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); direction = D3DXVECTOR3(xAngle, yAngle, 1.0f); // find the inverse matrix D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView)); // convert origin and direction into model space D3DXVec3TransformCoord(&origin, &origin, &matInverse); D3DXVec3TransformNormal(&direction, &direction, &matInverse); D3DXVec3Normalize(&direction, &direction); bool hasHit = false; float hitU, hitV = 0.0f; DWORD faceIndex = 0; D3DXVECTOR3 hitPos ; float distance; // detect picking BOOL hit; D3DXIntersect(meshSpaceship, &origin, &direction, &hit, &faceIndex, &hitU, &hitV, &distance, NULL, NULL); if(hit) { hitPos = origin + (direction * distance); D3DXVec3TransformCoord(&hitPos, &hitPos, &matWorld ); CString str; str.Format(L"x=%f,y=%f,z=%f",hitPos.x,hitPos.y,hitPos.z); ::MessageBox(this->m_hWnd,str,L"Data",MB_OK); } } Thanking you in advance, Ash.

##### Share on other sites
Looks correct to me at first glance. Do you think it doesn't work for some reason? If so, why? Does it not do what you expect? If not, what does it do and what do you expect?

##### Share on other sites
I think you might have a problem with this line:
// find the inverse matrixD3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView));

If you are using the same world matrix for everything then that's fine, if not this will transform the coordinate to the object space of the last object to have been rendered.

Also you will get the object space coordinate not world space coordinates, I'm not sure which you wanted.

##### Share on other sites
Also, if you create a pure device (i.e. you specified the D3DCREATE_PUREDEVICE flag when creating the device), IDirect3DDevice9::GetTransform() will not return the current device state.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011702
• ### Who's Online (See full list)

There are no registered users currently online

×