Sign in to follow this  
helix

Mouse coords to world coords -- Attaching a 3d object to cursor

Recommended Posts

I know it's not a difficult problem but I'm stuck. I'm banging my head against the wall trying to figure this out. In my game, clicking on an object in the inventory will attach it to the mouse cursor so you can drop it somewhere else. Unfortunately the engine I'm using doesn't have a worldToScreen (and vice versa) function. The "picking" works (ie, I can select/deselect the objects in the inventory with no problem, but while my erroneous mouse following code runs as expected, it just doesn't look right). Can someone give me some psuedo-code or a description of what I need to do? I'll give any additional information that is needed. Thanks in advance!

Share this post


Link to post
Share on other sites
Ok, I figured this one out (thanks google):


D3DXVECTOR3 vPickRay;
vPickRay.x = (((2.0f * fScreenX) / fWindowWidth) - 1.0f) / m_ProjectionMatrix._11;
vPickRay.y = -(((2.0f * fScreenY) / fWindowHeight) - 1.0f) / m_ProjectionMatrix._22;
vPickRay.z = 1.0f;

D3DXMATRIXA16 mInvView;
D3DXMatrixInverse( &mInvView, NULL, &m_ViewMatrix );

// Transform the screen space pick ray into 3D space
D3DXVECTOR3 vRayDir;
vRayDir.x = (vPickRay.x * mInvView._11) + (vPickRay.y * mInvView._21) + (vPickRay.z * mInvView._31);
vRayDir.y = (vPickRay.x * mInvView._12) + (vPickRay.y * mInvView._22) + (vPickRay.z * mInvView._32);
vRayDir.z = (vPickRay.x * mInvView._13) + (vPickRay.y * mInvView._23) + (vPickRay.z * mInvView._33);

D3DXVECTOR3 vRayOrigin;
vRayOrigin.x = mInvView._41;
vRayOrigin.y = mInvView._42;
vRayOrigin.z = mInvView._43;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this