2D co-ordinates to 3D space - eh?

Topics: Developer Forum, User Forum
Sep 22, 2007 at 3:32 PM
I have tried the TransformTo2DAncestor methods using the "Hit" models under the mouse...

and just the plain old GetViewMatrix to extracate the Matrix3D for my camera.

also I tried the GetProjectionMatrix(camera, viewportRatio)...

But ended up with very small values? like 0.00021, etc, etc can these be right???

So I then wrote a new (very simple) stand-alone test app to reduce the possibilty of extraneous stupidities on my part being the culprit.
I put one small sphere in the 0,0,0 spot and radialGradientBrush shaded it.

added a camera at 0,0,-1 with a look of 0,0,1.

and tried two very simple functions:

private Point3D calculatePointInWorld(Point pointOnScreen, Camera camera)


Matrix3D screenMatrix = MathUtils.GetViewMatrix(camera);

Point3D PointInWorld = new Point3D();


Point3D pointOnScreenAs3D = new

Point3D(pointOnScreen.X, pointOnScreen.Y, 0);

PointInWorld =


return PointInWorld;


but this appeared to do nothing as the matrix is 1 to 1 seemingly.

Doh! perhaps i should have gone for the projectionMatrix...

Then i tried the ubiquitous...

public Point3D Convert2DPoint(Point pointToConvert, ModelVisual3D myVisual)


bool TranformationSuccess = false;

Viewport3DVisual viewport;

Matrix3D screenTransform =

MathUtils.TryTransformTo2DAncestor(myVisual, out viewport, out TranformationSuccess);

Point3D pointInWorld = new Point3D();

if (screenTransform.HasInverse)


Matrix3D reverseTransform = screenTransform;


Point3D pointOnScreen = new

Point3D(pointToConvert.X, pointToConvert.Y, 0);

pointInWorld =


return pointInWorld;


TranformationSuccess = false;

return pointInWorld;


but neither of these provided the expected results...

like (4,3,7) or ( 4.13, 3.23, 7) etc...

ho hum.

Am i being stupid here?

Sep 23, 2007 at 5:43 AM
Ok, i got some numbers that make sense.



heres the code...

public Point3D Get2Dto3D(Point pointToConvert, Viewport3D vp3D)
bool success;
Point3D pointInWorld = new Point3D();

Point3D pointOnScreen = new Point3D(pointToConvert.X, pointToConvert.Y, 0);

Viewport3DVisual vp3Dv = VisualTreeHelper.GetParent(vp3D.Children0) as Viewport3DVisual;
Matrix3D screenTransform = MathUtils.TryWorldToViewportTransform(vp3Dv, out success);

if (screenTransform.HasInverse)

pointInWorld = screenTransform.Transform(pointOnScreen);

return pointInWorld;
success = false;
return pointInWorld;


I put the camera at 0 0 0 and looked 0 0 -1

which in the center of my window gave 0 0 -1 which is negative nearPlane...

at the center of your Viewport3D the mouse (x,y,0) should map to (0,0, camera.NearPlane)

when I looked 0 0 1 I got the positive near plane but thats not the direction I ordinarily look...

so I swapped the camera to Position in the negative and look in the positive but then the trackball was all reversed (duh!).

So my simple question is this - here is a camera location (not preferred but simple)

why is it that I only get the right values for the x and y locations of my apps objects when the near plane distance is set to exactly the Z value of the position?

and as my room axis' around 0,0,0 am i screwed... IE should I be putting it all in the positive or something...

with a

<PerspectiveCamera x:Name="Free_Camera"

an on a 4x 4y zy sixed room the function returns great values

-2 to 2 for -x to +x edges,

15 for the Z,

0 to +4 for the Y,

but obviously I can only see the back half of my room!

but If I set the camera initially to:

<PerspectiveCamera x:Name="Free_Camera"

FieldOfView="30" />

I get values for if I was testing for a depth of 31.3 ish? (positionZ - NearPlaneDistance and skewed a bit Im guessing);

So actually my other questions are:

a) I am assuming the initial location of the camera needs to be factored into the Matrix3D transform somehow. is that right and how?

b) how does one set the depth being tested, I tried messing with the 2D x,y,0 zero value but didnt get the expected linear results.

Aug 21, 2008 at 11:29 PM

What does this code mean?

Viewport3DVisual vp3Dv = VisualTreeHelper.GetParent(vp3D.Children0) as Viewport3DVisual;

Is the 0 after Children a typo?  What is the correction?