Ask Experts Questions for FREE Help !
Ask
    Ramilol's Avatar
    Ramilol Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 22, 2010, 08:12 PM
    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

Check out some similar questions!

Picking myself up!Should I let go? [ 4 Answers ]

Well me and this girl has said that we should take sometime apart from each other because she has some things in her life to take care of cool,me as well.On thurday and today is sat I have not talk to her.We went out on wed to a movie with some friends.It seemed like she was done with me the whole...

Installing directx 9c [ 1 Answers ]

How do I install directx 9c in service pack 2

Directx upgrade [ 2 Answers ]

What would be the appropriate Microsoft-Directx download for a computer running windows 2000 professional.

Directx 7 Problems on Win 2K [ 8 Answers ]

I installed a game requiring Directx 6.0 and recommending 7.0a. Every time I try to load it to play I get an error stating "ddraw init failed" I am running Windows 2000 with NT tech. on a Pentium 2 processor. When I try installing 6.0 or 7.0a, I get a message stating those service packs are...

DirectX Help [ 2 Answers ]

I run dxdiag and it shows that direct3d is not there. I thought that it installs with directx (I have just installed the latest version from microsoft) but I seem to be wrong. I checked Microsoft help but they were no help. So, my question is where or how can I install direct3d? Thanks


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.