Transformation of sreen coordinated to model coordinates in Silverlight using Kit3D

Jan 25, 2011 at 1:54 AM

I am trying to implement drag and drop functionality in Silverlight using kit3d. I need to change triangle's vertex coordinates when mouse is released. For this I need to convert screen coordinates into model coordinates. Reverse in there in kit3d. Can anyone help me with sample code to do the same?
Please note "HitTest" does not transform to model coordinates, you can see the ModelVisual3D.cs for the way model to screen transformation is done.

Thanks in advance.
Regards
Dushyant

Jan 25, 2011 at 9:18 AM

On MouseLeftButtonDown event,
// The point in screen
Point3D testPoint3D = new Point3D((float)currMousePos.X, (float)currMousePos.Y, 0);

// the orientation vector
Vector3D testDirection3D = new Vector3D((float)currMousePos.X, (float)currMousePos.Y, 10);

// pt and vec params
PointHitTestParams pointparams = new PointHitTestParams(currMousePos);

Kit3D.Windows.Media.VisualTreeHelper.HitTest(viewport3d, null, HTResult, pointparams);

The 3rd parameter of HitTest is a callback function with prototype: HitTestResultBehavior HTResult(HitTestResult res);
So
public HitTestResultBehavior HTResult(HitTestResult res)
{
    RayHitTestResult rayResult = res as RayHitTestResult;
    
    if(rayResult != null)
    {
        RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
        
        if(rayMeshResult != null)
        {
             hitgeometry = rayMeshResult.ModelHit as GeometryModel3D; // global GeometryModel3D var
             hitmesh = rayMeshResult.MeshHit as MeshGeometry3D; // global MeshGeometry3D var

             // whatever logic you want to hitted model
        }
    }

}


Jan 25, 2011 at 12:30 PM

agreed, I got the mesh. now I need to update mesh.positions by some value, (which is going to be the EndPt.X-startPat.X, similar for Y and Z), So I need to know the startPt and endpt w.r.t to model.

How to get startPt and endPt?

Below is the sample code for the condition I am talking

            Point3D pt1 = new Point3D(0, 0, 0);
            Point3D pt2 = new Point3D(0.5, 0, 0);
            Point3D pt3 = new Point3D(0, 0.5, 0);
            mesh.Positions.Add(pt1);
            mesh.Positions.Add(pt2);
            mesh.Positions.Add(pt3);
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);

suppose I am clicking mouse at the 2nd vertex (0.5, 0, 0), so my start point should be Point3D(0.5, 0, 0). How to get it?

 

Regards

Dushyant