Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Ray Casting Problem in DirectX

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

My ray's dont seem to be work at all, and the origin doesn't seem to be changing with the mouse movement. Heres the where I make the ray. For sake of definition the origin is where the 2D mouse would be in 3D Space.

Ray BIEngine::CalcPickRay(long X, long Y)
    float px=0.0;
    float py=0.0;
    D3DVIEWPORT9 vp;
    D3DXMATRIX proj;
    Ray retRay;

    D3DXVECTOR3 vPickRayDir;
    D3DXVECTOR3 vPickRayOrig;	
    D3DXMATRIXA16 matProj;
    D3DXMATRIXA16 matView;
    lpDevice->GetTransform( D3DTS_PROJECTION, &matProj );
    lpDevice->GetTransform( D3DTS_VIEW, &matView );
    POINT Pos;
    GetCursorPos( &Pos);
    ScreenToClient( WindHandle, &Pos );

    // Compute the vector of the pick ray in screen space
    static float fInvWidth = 1.0f/WindWidth, fInvHeight= 1.0f/WindHeight;
    float fAspectRatio = float(WindWidth)/WindHeight;
    float fPhi,fBeta;
    fBeta = D3DXToRadian(60) * (  ( float(Pos.x) * fInvWidth ) - 0.5f);
    fPhi  = (D3DXToRadian( 60 )/ fAspectRatio) * ( -( float(Pos.y) * fInvHeight) + 0.5f);
    D3DXVECTOR3 v( FastCos( fPhi ) * FastSin( fBeta ), FastSin( fPhi ),	FastCos( fPhi ) * FastCos( fBeta ) );	

    // Get the inverse view matrix
    D3DXMatrixInverse( &m, NULL, &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;
    vPickRayOrig.x = m._41 + -10.0f * vPickRayDir.x ;
    vPickRayOrig.y = m._42 + -10.0f * vPickRayDir.y ;
    vPickRayOrig.z = m._43 + -10.0f * vPickRayDir.z ;

    retRay.Direction = vPickRayDir;
    retRay.Origin = vPickRayOrig;

    return retRay;
The second step is done right before checking intersections. Here is the code for that.
	D3DXVec3TransformCoord(&PickRay.Origin ,&PickRay.Origin ,&invMat);
	D3DXVec3TransformNormal(&PickRay.Direction ,&PickRay.Direction  ,&invMat);

Sample Output (Ray Casting values) These are the values of the Ray when I attempt to check for intersections. The Box values indicate where the Quad is on the the screen. This same thing repeats over and over again. Menu Background Ray Origin: -4.18026, 6.44458, -16.4026 Ray Direction: 0.418026, -0.644458, 0.640256 Menu Play Ray Origin: -2.18026, 9.44458, -16.4026 Ray Direction: 0.418026, -0.644458, 0.640256 Menu Multiplayer Ray Origin: -2.18026, 12.4446, -16.4026 Ray Direction: 0.418026, -0.644458, 0.640256 Menu Quit Ray Origin: -4.18026, 15.4446, -16.4026 Ray Direction: 0.418026, -0.644458, 0.640256 [edited by - IDirect3D9 on February 28, 2004 9:53:34 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!