title: 3D Hit Testing in WPF
body: <p>I'm writing a WPF application that displays terrain in 3D.</p>
  
  <p>When I perform hit testing, the wrong 3D point is returned (not the point I clicked on).</p>
  
  <p>I tried highlighting the triangle that was hit (by creating a new mesh, taking the coordinates from the <code>RayMeshGeometry3DHitTestResult</code> object). I see that the wrong triangle gets hit (a triangle is highlighted, but it is not under the cursor).</p>
  
  <p>I'm using a perspective camera with field of view of 60, and the near and far planes are of 3 and 35000 respectively.</p>
  
  <p>Any idea why it might happen and what I can do to solve it?</p>
  
  <p>Let me know if you need any more data.</p>
  
  <p>Edit: This is the code I use to perform the hit testing:</p>
  
  <pre><code>private void m_viewport3d_MouseDown(object sender, MouseButtonEventArgs e)
  {
      Point mousePos = e.GetPosition(m_viewport3d);
      PointHitTestParameters hitParams = new PointHitTestParameters(mousePos);
      HitTestResult result = VisualTreeHelper.HitTest(m_viewport3d, mousePos);
      RayMeshGeometry3DHitTestResult rayMeshResult = result as RayMeshGeometry3DHitTestResult;
      if (rayMeshResult != null)
      {
          MeshGeometry3D mesh = new MeshGeometry3D();
          mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex1]);
          mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex2]);
          mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex3]);
          mesh.TriangleIndices.Add(0);
          mesh.TriangleIndices.Add(1);
          mesh.TriangleIndices.Add(2);
          GeometryModel3D marker = new GeometryModel3D(mesh, new DiffuseMaterial(Brushes.Blue));
          //...add marker to the scene...
      }
  }
  </code></pre>