Jump to content
  • Advertisement
Sign in to follow this  

Mouse Picking Help - Vec3Unproject not working as intended

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

So I've spent several days scouring the web in regards to this with most links leading back to this forum. I've been attempting to do mouse picking and from everything i've read using the d3dx10Vec3unproject function seemed to be the best option (it looks to me as they simply wrapped the pick example in the d3d 9 sdk in a function). Unfortunately I've lost several days trying to get it to work to no avail. rayObjDirection and RayObjOrigin are completely different to the r and rDir i got from the unproject function. here is my pick code, using both the sdk and the unproject version but the results of unproject are completely different to the sdk method. So what the hell does unproject do internally? I can only assume its something to do with the viewport as that is the only structure that is not used in the sdk version (where i manually specify window width and height)... i've been pulling my hair out, any help would be appreciated. Thanks in advance.
			lDown = true;
			UINT hc;


			v.x =  ( ( ( 2.0f * mouseX ) / windowWidth  ) - 1 ) / ProjectionMatrix._11;
			v.y = -( ( ( 2.0f * mouseY ) / windowHeight ) - 1 ) / ProjectionMatrix._22;
			v.z =  1.0f;

			D3DXVECTOR3 rayOrigin,rayDir;

			D3DXMatrixInverse( &m, NULL, &ViewMatrix );

			// 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;

			// Use inverse of matrix
			D3DXMATRIX matInverse;

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;


			//unproject test
			D3DXVECTOR3 r, rDir;

			D3DXVec3Unproject( &r, &D3DXVECTOR3( mouseX, mouseY, 0 ), &viewPort, &ProjectionMatrix, &ViewMatrix, &cp.WorldMatrix);
			D3DXVec3Unproject( &rDir, &D3DXVECTOR3( mouseX, mouseY, 1 ), &viewPort, &ProjectionMatrix, &ViewMatrix, &cp.WorldMatrix);
			//ray direction
			rDir = rDir - r;

			BOOL hasHit;
			float distanceToCollision;

			cp.mesh->Intersect(&rayObjOrigin, &rayObjDirection, &hc, NULL, NULL, NULL, &distanceToCollision, NULL);
			if ( hc > 0) MessageBox(hWnd, "Hit!", "Hit!", MB_OK);

Share this post

Link to post
Share on other sites
Looks to me like you're mixing together two unprojection methods. A simple way is to unproject the point with z=0 and z=1. The ray is then v1-v0 and origin v0.

The Pick code does such calculations itself, which may be a little more efficient (it's less calculation, but I can't say how SSE and other optimisations will affect optimality).

Looks to me like you're doing both here. I'd suggest the first method, because IMO it results in clearer code.

Share this post

Link to post
Share on other sites
Where do you get the mouse point? If you use GetCursorPos(), you need to convert the point from screen to client coordinates, i.e., ScreenToClient(hwnd,&pt); or something similar.

Share this post

Link to post
Share on other sites
no you guys are missing the point, I'm using the first method to just illustrate the brokenness of the unproject function, i am converting my mouse co-ordinates, the first method works, but the second method, returns completely wrong unprojected co-ordinates, i think the dx10 guys broke something.

does any one have a working example of picking using vec3Unproject with the june2k8 sdk?

Share this post

Link to post
Share on other sites
"my" method is the method used in the d3d pick sample, so it is correct (it also returns the correct co-ordinates and so picking actually works), the vec3unproject tho does not return the correct co-ordinates. the return values from it are around 10+ times smaller than the expected values. Something is broken and i'd love to know what it is.

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!