Please help me with picking objects in Direct3d world

Started by
2 comments, last by CEA 18 years, 6 months ago
Sorry for bad english (i'm from Russia) I have X and Y screen coordinates (from mouse) I need to find objects in the 3d world that lie under mouse cursor. My UpdateScreenFunction: D3DXMATRIX T, Rx, Ry, S, R; D3DXMatrixTranslation(&T, sceneSettings->Tx, sceneSettings->Ty, 0.0f); D3DXMatrixRotationX(&Rx, sceneSettings->Rx); D3DXMatrixRotationY(&Ry, sceneSettings->Ry); D3DXMatrixScaling(&S, sceneSettings->Scaling, sceneSettings->Scaling, sceneSettings->Scaling); R = Ry*Rx*S*T; m_device->SetTransform(D3DTS_WORLD, &R); m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( sceneSettings->BackgroundColor[0], sceneSettings->BackgroundColor[1], sceneSettings->BackgroundColor[2]), 1.0f, 0); m_device->BeginScene(); //*************************** drawind 3d objects //*************************** m_device->EndScene(); m_device->Present(0, 0, 0, 0); so, how can i find D3DXVECTOR3 vPickRayDir - Direction D3DXVECTOR3 vPickRayOrig - Position ?????
Advertisement
Hi there CEA,
How are you doing?

The Problem
Ray picking examples and implementation.

The Solution
Cea, I thought that I might refer you to this implementation in C# which goes through the logic and is pretty easy to follow.

Ray Picking example, post.

The basic idea would be the following.

1) Get mouse coordinates in screen space
2) The near vector is near = new Vector3(cursor.X, cursor.Y, 0);
3) The far vector would be far = new Vector3(cursor.X, cursor.Y, 1);
4) You have 2 vectors now and if you unproject them you would go from screen space to world space using the D3DXVec3Unproject method in c++.
5) If you then subtract the 2 vectors from each other you get a ray.
6) You would then have the Ray Position and the Ray Direction. They are...
If you subtract the near from the far vector you get the ray direction and the ray position is the near vector. This means you have all the information needed for the D3DXIntersect method and you can test if it intersects the mesh.

I hope this helps buddy.
Take care.

In addition to Armadon notes you could check the Toymaker page, you will find a tutorial on DirectX raypicking:
http://www.toymaker.info/Games/html/picking.html

My project`s facebook page is “DreamLand Page”

Thank you very much guys, all works now!!!!!!!!!!!!!!

This topic is closed to new replies.

Advertisement