Jump to content
  • Advertisement
Sign in to follow this  

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

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

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):

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;

D3DXMatrixInverse( &mInvView, NULL, &m_ViewMatrix );

// Transform the screen space pick ray into 3D space
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
Sign in to follow this  

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!