Original Post
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
Edited by - sdoherty55 on December 29, 2001 4:31:23 PM
#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;
}
|