Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

Problem with picking


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 MefistoManna   Members   -  Reputation: 139

Like
0Likes
Like

Posted 25 June 2012 - 02:01 AM

Hi guys, sorry for my English,
I have this problems with picking ray on multiple mesh:
1) if I get the world matrix in this way (with GetTrasform) , the picking result is wrong when I traslate or scale the meshes (work on original position or size)
2) Else if I pass in the function the single meshes 's world matrix (array) the result is correct but if I scale the mesh, the ray pick more distant mesh and I have to get close for get the correct result.

Here is my code (I have other function that get the closest mesh, but I am sure that the problems is here):
[source lang="cpp"]if(m_Input::MouseButtonDown(0)) { g_D3DDevice_->GetTransform(D3DTS_PROJECTION, &matProj ); g_D3DDevice_->GetTransform(D3DTS_VIEW, &matView); g_D3DDevice_->GetTransform(D3DTS_WORLD, &matWorld); g_D3DDevice_->GetViewport( &m_Viewport ); D3DXMatrixInverse(&ImatView, NULL, &matView ); D3DXMatrixInverse(&ImatWorld,NULL,&matWorld); v.x = ( ( ( 2.0f * Cursor.x ) / m_Viewport.Width ) - 1 ) / matProj._11; v.y = -( ( ( 2.0f * Cursor.y ) / m_Viewport.Height ) - 1 ) / matProj._22; v.z = 1.0f; rayDir.x = v.x*ImatView._11 + v.y*ImatView._21 + v.z*ImatView._31; rayDir.y = v.x*ImatView._12 + v.y*ImatView._22 + v.z*ImatView._32; rayDir.z = v.x*ImatView._13 + v.y*ImatView._23 + v.z*ImatView._33; rayOrigin.x = ImatView._41; rayOrigin.y = ImatView._42; rayOrigin.z = ImatView._43; D3DXVec3TransformCoord(&rayOrigin,&rayOrigin,&ImatWorld); D3DXVec3TransformNormal(&rayDir,&rayDir,&ImatWorld); D3DXVec3Normalize(&rayDir,&rayDir); D3DXIntersect(g_Mesh[NumModel], &rayOrigin, &rayDir, &picked, NULL, NULL, NULL, &dist, NULL, NULL); if(picked) { NumMeshHit[NumHit] = NumModel; MeshDist[NumHit] = dist; NumHit++; } }[/source]

Sorry again for my bad English.

Edited by MefistoManna, 26 June 2012 - 08:04 AM.


Sponsor:

#2 ankhd   Members   -  Reputation: 415

Like
0Likes
Like

Posted 25 June 2012 - 05:48 AM

Hi there.
here http://msdn.microsoft.com/en-us/library/windows/desktop/bb172882(v=vs.85).aspx
it say's that the member ppAllHits may hold 1 or more D3DXINTERSECTINFO structure.

you then need to loop through the ppallhits by this many times pCountOfHits
you then need to compare the ppAllHits[ctr]->Dist to the camera's pos.

#3 MefistoManna   Members   -  Reputation: 139

Like
0Likes
Like

Posted 25 June 2012 - 12:33 PM

Thanks ankhd,
I do this, and it works perfectly, but the problem is when the mesh is traformed. I think that it' s a wrong use of matrix. Do I have to use the inverse of matrix obtained with the function GetTrasform or the matrix of single mesh?

#4 ankhd   Members   -  Reputation: 415

Like
0Likes
Like

Posted 26 June 2012 - 02:46 AM

I think you need to do this.
D3DXMATRIX matWorld = world;

D3DXMATRIX mWorldView = matWorld * matView;
D3DXMATRIX m;
D3DXMatrixInverse( &m, NULL, &mWorldView );
that should work.

#5 ankhd   Members   -  Reputation: 415

Like
0Likes
Like

Posted 26 June 2012 - 02:48 AM

oops and this
// Transform the screen space pick ray into 3D space
rayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;
rayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;
rayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;
rayOrigin.x = m._41;
rayOrigin.y = m._42;
rayOrigin.z = m._43;

and no transforming off coords

#6 MefistoManna   Members   -  Reputation: 139

Like
0Likes
Like

Posted 26 June 2012 - 08:07 AM

It still don't work
Posted Image .I am doing in this way:
[source lang="cpp"] g_D3DDevice_->GetTransform(D3DTS_PROJECTION, &matProj ); g_D3DDevice_->GetTransform(D3DTS_VIEW, &matView); g_D3DDevice_->GetTransform(D3DTS_WORLD, &matWorld); g_D3DDevice_->GetViewport( &m_Viewport ); D3DXMatrixInverse(&ImatView, NULL, &matView ); D3DXMatrixInverse(&ImatWorld, NULL, &matWorld); v.x = ( ( ( 2.0f * Cursor.x ) / m_Viewport.Width ) - 1 ) / matProj._11; v.y = -( ( ( 2.0f * Cursor.y ) / m_Viewport.Height ) - 1 ) / matProj._22;v.z = 1.0f; D3DXMATRIX mWorldView = matWorld * matView; D3DXMATRIX m; D3DXMatrixInverse( &m, NULL, &mWorldView ); rayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31; rayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32; rayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33; rayOrigin.x = m._41; rayOrigin.y = m._42; rayOrigin.z = m._43; D3DXVec3TransformCoord(&rayOrigin,&rayOrigin,&ImatWorld); D3DXVec3TransformNormal(&rayDir,&rayDir,&ImatWorld); D3DXVec3Normalize(&rayDir,&rayDir); D3DXIntersect(g_Mesh[NumModel], &rayOrigin, &rayDir, &picked, NULL, NULL, NULL, &dist, AllHit, HitCount);[/source]
If I multiply matworld and matview the picking is completly wrong.

Edited by MefistoManna, 26 June 2012 - 12:06 PM.


#7 ankhd   Members   -  Reputation: 415

Like
0Likes
Like

Posted 26 June 2012 - 09:52 PM

I think you need to have a copy of your transform matrix and not use the GetTransform its for rendering each time u render a mesh
you will set the transform right so you would need to set the transform to the device each time you do a new mesh pick.

My code works for my meshes they all scaled and rotated and translation.

#8 MefistoManna   Members   -  Reputation: 139

Like
0Likes
Like

Posted 27 June 2012 - 06:51 AM

Thanks again,
I modified my code like you said...it's incredible still don't work for scaled mesh... I did some research and this is my same problem http://www.gamedev.n...t-mesh-problem/ but a concrete solution I did't find.

EDIT. I solved by multiply the maximum scale factor of 3 dimensions (x,y,z) for the distance, thanks for your help.

Edited by MefistoManna, 27 June 2012 - 12:38 PM.


#9 ankhd   Members   -  Reputation: 415

Like
0Likes
Like

Posted 28 June 2012 - 03:12 AM

Oh I see from the post link you set. You don't have a picking problem it's more of a selecting the nearest object problem.
As in the link he trys to pick the wall but the ground is picked. this is due to the fact that the camera is near the ground based on how the camera is looking.

#10 MefistoManna   Members   -  Reputation: 139

Like
0Likes
Like

Posted 28 June 2012 - 08:45 AM

ahhh for the moment it's working, but if there is a clearest method it' s better.

Edited by MefistoManna, 28 June 2012 - 08:48 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS