Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C++ (https://www.askmehelpdesk.com/forumdisplay.php?f=439)
-   -   C directx picking ray is not working (https://www.askmehelpdesk.com/showthread.php?t=528332)

  • Nov 22, 2010, 08:12 PM
    Ramilol
    c directx picking ray is not working
    so I'm working with my picking in directx but it didn't work :/ I have no idea why.
    here is my code
    BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
    {

    POINT ptCursor;
    GetCursorPos( &ptCursor );
    ScreenToClient( hWnd, &ptCursor );

    D3DXMATRIX matProj;
    d3ddev->GetTransform( D3DTS_PROJECTION, &matProj );

    // Compute the vector of the pick ray in screen space
    D3DXVECTOR3 v;
    v.x = ( ( ( 2.0f * ptCursor.x ) / 800 ) - 1 ) / matProj._11;
    v.y = -( ( ( 2.0f * ptCursor.y ) / 600 ) - 1 ) / matProj._22;
    v.z = 1.0f;

    // Get the inverse view matrix
    D3DXMATRIX m, matView;
    d3ddev->GetTransform( D3DTS_VIEW, &matProj );
    D3DXMatrixInverse( &m, NULL, &matView );

    D3DXVECTOR3 vPickRayDir, vPickRayOrig;

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

    D3DXVECTOR3 vNear,vDir;
    D3DXMATRIX invMat, matCombined;
    d3ddev->GetTransform( D3DTS_WORLD, &matCombined );
    D3DXMatrixInverse(&invMat,NULL,&matCombined);
    D3DXVec3TransformCoord(&vNear,&vPickRayOrig,&invMa t);
    D3DXVec3TransformNormal(&vDir,&vPickRayDir,&invMat );

    // test for intersection
    BOOL bHit;
    float dist;
    D3DXIntersect(entity->pDrawMesh,&vNear,&vDir,&bHit,NULL,NULL,NULL,&dist ,NULL,NULL);

    if(bHit)
    {
    PostQuitMessage(0);
    }

    rayObjSpace=vNear;

    return bHit;
    }
    I tried that but it didn't work ;/ I also tried this I think this one is little closer but still doesn't work
    BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
    {
    D3DXVECTOR3 v;
    D3DXMATRIX matProj;
    POINT pt;
    D3DVIEWPORT9 vp;
    D3DXMATRIX matInverse, matWorld;
    D3DXMATRIX m;
    D3DXVECTOR3 rayOrigin,rayDir;
    D3DXMATRIX matView;
    D3DXVECTOR3 rayObjOrigin,rayObjDirection, rayDirection;

    GetCursorPos(&pt);
    ScreenToClient(hWnd, &pt);
    d3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
    d3ddev->GetViewport(&vp);

    d3ddev->GetTransform(D3DTS_VIEW, &matView);

    // Use inverse of matrix
    d3ddev->GetTransform(D3DTS_WORLD, &matWorld);
    D3DXVECTOR3 vec3( pt.x, pt.y, 1.0f );
    D3DXVec3Unproject( &rayObjSpace, &vec3, &vp, &matProj, &matView, &matWorld );
    // Transform ray origin and direction by inv matrix

    D3DXMATRIX invWorld;
    D3DXMatrixInverse( &invWorld, NULL, &matWorld );

    D3DXVECTOR3 camObjSpace;
    camPos.x=0.0;
    camPos.y=0.0;
    camPos.z=z;
    D3DXVec3TransformCoord( &camObjSpace, &camPos, &invWorld );

    rayDir = rayObjSpace - camObjSpace;

    BOOL ha****;
    float distanceToCollision;

    if(FAILED(D3DXIntersect(entity->pDrawMesh, &rayObjSpace, &rayDir, &ha****, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
    {
    PostQuitMessage(0);
    };``

    if(ha****==1)
    {
    PostQuitMessage(0);
    }

    return bHit;
    }
    but no luck at all any ideas

  • All times are GMT -7. The time now is 05:39 AM.