Sign in to follow this  

Picking Ray Question

This topic is 3596 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

I have been working on a picking ray and I have noticed that if I select on an object I sometimes get the one behind it. I tried making it so that it would sort by z and pick the one closest to the camera (not in code below), however, that does not work if say I am looking up and there is two objects in the same spot but have a different y value. I read somewhere there is an equation I could do. Does anyone know how to solve this problem? Below is all the code associated with my picking ray. struct Entity3D { D3DXMATRIX matTransform; D3DMATERIAL9 * pMaterial; ID3DXMesh * pMesh; char name[7]; Entity3D() { memset(this, 0, sizeof(Entity3D)); } }; struct Ray { D3DXVECTOR3 vPosition; D3DXVECTOR3 vDirection; }; void Pick(Entity3D *pShade, BOOL &phit) { Ray ray; D3DXMATRIX matView, matWorld, matInverse; CalculatePickingRay(&ray, g_Cursor.position.x, g_Cursor.position.y); g_pD3DDevice->GetTransform(D3DTS_VIEW, &matView); matWorld = pShade->matTransform; matWorld *= matView; D3DXMatrixInverse(&matInverse, 0, &matWorld); TransformRay(&ray, &ray, &matInverse); D3DXIntersect(pShade->pMesh, &ray.vPosition, &ray.vDirection, &phit, 0,0,0,0,0,0); } void CalculatePickingRay(Ray * pRayOut, long lX, long lY) { if(!g_pD3DDevice) return; D3DXMATRIX matProj; D3DVIEWPORT9 vp; g_pD3DDevice->GetTransform(D3DTS_PROJECTION, &matProj); g_pD3DDevice->GetViewport(&vp); pRayOut->vPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f); float pX, pY; pX = (((2.0f * lX) / vp.Width) - 1.0f) / matProj(0, 0); pY = (((-2.0f * lY) / vp.Height) + 1.0f) / matProj(1, 1); pRayOut->vDirection = D3DXVECTOR3(pX, pY, 1.0f); } void TransformRay(Ray *pRayOut, Ray * pRayIn, D3DXMATRIX *pMat) { if(!pRayIn || !pMat) return; D3DXVec3TransformCoord(&pRayOut->vPosition, &pRayIn->vPosition, pMat); D3DXVec3TransformNormal(&pRayOut->vDirection, &pRayIn->vDirection, pMat); D3DXVec3Normalize(&pRayOut->vDirection, &pRayOut->vDirection); }

Share this post


Link to post
Share on other sites
Well, you obviously can implement some culling (you can only pick things that are visible, i.e. drawn).

You can sub-optimize by checking bounding boxes before doing actual line to triangle checks.

But in the end you'll always have the possibility of a few objects being hit by the pick ray.

Store the closest distance and its object.
On every hit check that value. If the new distance is closer to the camera store the new values.
Repeat for every object you want to check.



Share this post


Link to post
Share on other sites

This topic is 3596 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this