title: Broken Picking Coordinates Directx11
body: <p>I have for a fair while now been trying to implement a click-based model insertion into my game editor but have run into numerous problems. The most recent is an offset in the placement of the model. So far I have got this code together:</p>
  
  <pre><code>    D3DXVECTOR3 vPickRayDir;
      D3DXVECTOR3 vPickRayOrig;
  
      float width = (float)d3d-&gt;scd.BufferDesc.Width;
      float height = (float)d3d-&gt;scd.BufferDesc.Height;
  
      const D3DXMATRIX* pmatProj = &amp;d3d-&gt;matProjection;
  
      POINT ptCursor;
      GetCursorPos(&amp;ptCursor);
      ScreenToClient(&amp;ptCursor);
  
      D3DXVECTOR3 v;
      v.x = (((2.0f * ptCursor.x) / (d3d-&gt;viewport.Width)) - 1.0f) / pmatProj-&gt;_11;
      v.y = -(((2.0f * ptCursor.y) / (d3d-&gt;viewport.Height)) - 1.0f) / pmatProj-&gt;_22;
      v.z = -1.0f;
  
      const D3DXMATRIX matView = d3d-&gt;matView;
      const D3DXMATRIX _matWorld = matWorld;
  
      D3DXMATRIX matRotateX, matRotateY, matRotateZ, matTranslate;
  
      D3DXMatrixTranslation(&amp;matTranslate, 0, 0, 0);
      D3DXMatrixRotationX(&amp;matRotateX, 0);
      D3DXMatrixRotationY(&amp;matRotateY, 0);
      D3DXMatrixRotationZ(&amp;matRotateZ, 0);
  
      matWorld = (matRotateX * matRotateY * matRotateZ) * matTranslate;
  
      D3DXMATRIX mWorldView = matWorld * matView;
      D3DXMATRIX m;
      D3DXMatrixInverse(&amp;m, NULL, &amp;mWorldView);
  
      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;
      vPickRayOrig.y = m._42;
      vPickRayOrig.z = m._43;
  
      ModelInstance mi(GetDocument()-&gt;GetModel(m_wndModels.m_nodeTree.selectID));
  
      mi.Translation.at(0).x = (1 * vPickRayDir.x + vPickRayOrig.x);
      mi.Translation.at(0).y = (1 * vPickRayDir.y + vPickRayOrig.y);
      mi.Translation.at(0).z = (1 * vPickRayDir.z + vPickRayOrig.z);
      mi.Translation.at(0).z *= -1;
  </code></pre>
  
  <p>Why is there an offset and how do I remove it?</p>
  
  <p>P.S. <a href="https://drive.google.com/file/d/0B31ZpbBZ5QAMY1N3ODNfc05HejA/view?usp=sharing" rel="nofollow">Here's a link the complied program</a>. Click open and open the enclosed fbx file. Once opened, double click the model in the model list and click on the view space to insert.</p>