Skip to main content
gamedev.net
🔒 Locked

Object Picking

Started by sdoherty55 1 reply 200+ views
Original Post
sdoherty55
sdoherty55
I seem to be able to detect an object collision, but only if the object is about 10 away from the mouse? Can anyone point me in the right direction? Thanks
    
#define	NEAR_Z		1.0f
#define FAR_Z		16000.0f
  
  
HRESULT CStar::Pick(D3DXVECTOR3 *pvNear, D3DXVECTOR3 *pvDir, float maxDist){

	// Clear the Mouse hit flag.

	m_bMouseHit = false;

	// yes, convert ray to model space

	D3DXVECTOR3 vNear,vDir;
	D3DXMATRIX invMat;
//		D3DXMatrixInverse(&invMat,NULL,&m_combinedMat);

	D3DXMatrixInverse(&invMat,NULL,(D3DXMATRIX*) &m_oMesh.matLocal);

	D3DXVec3TransformCoord(&vNear,pvNear,&invMat);
	D3DXVec3TransformNormal(&vDir,pvDir,&invMat);

	// test for intersection

	BOOL bHit;
	DWORD dwIndex;
	float u,v;
	float dist;

	D3DXIntersect(m_oMesh.mesh->m_pSysMemMesh,&vNear,&vDir,&bHit,&dwIndex,&u,&v,&dist,NULL,NULL);

	char sTest[80];
	sprintf(sTest, "...Data: %f\n", dist);
	OutputDebugString(sTest);


	// is there an intersection?

	if (bHit) {

		// yes, is it beyond the far plane?

		if (dist<maxDist) {
			m_bMouseHit = true;
			m_fMouseHitDist=dist;
		}
		else {
			// set the hit flag

			m_bMouseHit= false;
		}

	}
	return D3D_OK;
}
  
        
HRESULT CMyD3DApplication::Pick(){

    D3DXVECTOR3 vPickRayDir;
    D3DXVECTOR3 vPickRayOrig;

    // Get the pick ray from the mouse position

//    if( GetCapture() ){


//		POINT ptCursor;

//      GetCursorPos( &ptCursor );

//      ScreenToClient( m_hWnd, &ptCursor );


        // Compute the vector of the pick ray in screen space

        D3DXVECTOR3 v;
        v.x =  ( ( ( 2.0f * m_oWebMouse->m_pMousePos->x ) / m_d3dsdBackBuffer.Width  ) - 1 ) / m_matProj._11;
        v.y = -( ( ( 2.0f * m_oWebMouse->m_pMousePos->x ) / m_d3dsdBackBuffer.Height ) - 1 ) / m_matProj._22;
        v.z =  1.0f;

        // Get the inverse view matrix

        D3DXMATRIX m;
        D3DXMatrixInverse( &m, NULL, &m_matView );

        // Transform the screen space pick ray into 3D space

        vPickRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
        vPickRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
        vPickRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
		D3DXVec3Normalize(&vPickRayDir,&vPickRayDir);
        vPickRayOrig.x = m._41;
        vPickRayOrig.y = m._42;
        vPickRayOrig.z = m._43;

		// calc origin as intersection with near frustum


		vPickRayOrig+=vPickRayDir*NEAR_Z;

		// iterate through the scne and find closest match


//		CObj *hitObj=m_pScene->Pick(&vPickRayOrig,&vPickRayDir,FAR_Z-NEAR_Z);

		g_oSpace->Pick(&vPickRayOrig,&vPickRayDir,FAR_Z-NEAR_Z);
//	}


	return D3D_OK;
}
  
Edited by - sdoherty55 on December 29, 2001 4:31:23 PM
sdoherty55
sdoherty55
Here is a sub-set of my data as I zoom closer to the object located at {0,0,0}. How can I calculate why it is only intersecting at such a close range?

  
...Data: dist = -107374176.000000
...Data: vNear = ( x=29.227196 y=-21.938122 z=-8.401200 )
...Data: vDir = ( x=-0.830349 y=0.548554 z=0.098020 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=26.923872 y=-19.502520 z=-7.954119 )
...Data: vDir = ( x=-0.839385 y=0.532914 z=0.106942 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=24.572651 y=-17.174696 z=-7.460970 )
...Data: vDir = ( x=-0.847996 y=0.517156 z=0.115983 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=22.108849 y=-14.908391 z=-6.900057 )
...Data: vDir = ( x=-0.856181 y=0.501292 z=0.125139 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=19.867392 y=-12.922394 z=-6.372978 )
...Data: vDir = ( x=-0.863937 y=0.485333 z=0.134409 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=17.822906 y=-11.179346 z=-5.876051 )
...Data: vDir = ( x=-0.871260 y=0.469287 z=0.143787 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=15.933150 y=-9.635373 z=-5.399333 )
...Data: vDir = ( x=-0.878150 y=0.453168 z=0.153270 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=14.193608 y=-8.273228 z=-4.944378 )
...Data: vDir = ( x=-0.884604 y=0.436983 z=0.162856 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=12.598581 y=-7.076221 z=-4.512272 )
...Data: vDir = ( x=-0.890620 y=0.420745 z=0.172539 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=11.131214 y=-6.022916 z=-4.100046 )
...Data: vDir = ( x=-0.896197 y=0.404464 z=0.182318 )

...Data: dist = -107374176.000000
...Data: vNear = ( x=9.776204 y=-5.094737 z=-3.704797 )
...Data: vDir = ( x=-0.901335 y=0.388150 z=0.192187 )

...Data: dist = 9.466187
...Data: vNear = ( x=8.534365 y=-4.282847 z=-3.329197 )
...Data: vDir = ( x=-0.906032 y=0.371814 z=0.202143 )
sdoherty55
sdoherty55
Sorry,

Problem fixed, it works better if you pass the mouse x and y. Not just the x twice:

    
v.x = ( ( ( 2.0f * m_oWebMouse->m_pMousePos->x ) / m_d3dsdBackBuffer.Width ) - 1 ) / m_matProj._11;
v.y = -( ( ( 2.0f * m_oWebMouse->m_pMousePos->x ) / m_d3dsdBackBuffer.Height ) - 1 ) / m_matProj._22;





Edited by - sdoherty55 on December 29, 2001 7:34:52 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.