Jump to content
  • Advertisement
Sign in to follow this  

Ray casting Picking

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

Ive converted some code from opengl and need someone to look over the code to see if there are any mistakes. Im in Directx 11 with a LH coordinate system.


GetPickRay( float sx, float sy, float fov, float width, float height )
    float d = 1.0f/IvTan(fov*kPI/360.0f);
    float aspect = width/height;
    Vector3 viewPoint( 2.0f*aspect*sx/width - aspect, -2.0f*sy/height + 1.0f, d );

    viewPoint = mViewToWorldMatrix.TransformPoint( viewPoint );

    return viewPoint - mEyePoint;



OnMouseDown(WPARAM btnState, int x, int y)
	mLastMousePos.x = x;
	mLastMousePos.y = y;

	Vector3 ray = GetPickRay( (float) x, (float) y, 45.0f,(float) mClientWidth,(float)mClientHeight );

        // compute intersection with z=0 plane
        float t = -mEyePoint.z/ray.z;
        mClick = mEyePoint + t*ray;
		moveIT = XMMatrixTranslation(mClick.x,mClick.y,mClick.z);


Vector3 mUp(0.0f,1.0f,0.0f);
 Vector3 view = Vector3(0.0f,0.0f,0.0f) - mEyePoint;
    Vector3 right;
    Vector3 viewUp;
    right =  view.Cross( mUp );
    viewUp =  right.Cross( view );
  Matrix33 rotate;
//almost similar to a view matrix accept no negating eye point and dotting with uvw
rotate.SetRows( right, viewUp, view );
    // set translation (eye position)
    mViewToWorldMatrix(0,3) = mEyePoint.x;
    mViewToWorldMatrix(1,3) = mEyePoint.y;
    mViewToWorldMatrix(2,3) = mEyePoint.z;



Edited by terrysworkstations
correction to title

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!