Jump to content
  • Advertisement
Sign in to follow this  
ashwathh

3D X,Y,Z Coordinates

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
I think you might have a problem with this line:

// find the inverse matrix
D3DXMatrixInverse(&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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!