Charles Petzold on writing books, reading books, and exercising the internal UTM

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

Animating Attached Properties

September 8, 2007
New York, N.Y.

A couple WPF programmers have puzzled about this subject recently so maybe next time somebody has the problem they'll find this blog entry.

When setting Storyboard.TargetProperty to an attached property (such as Canvas.Left or Canvas.Top), put the fully-qualified property name in parentheses:

Here's a functional example:


Hi charles.

I was wondering if you could throw some light on a similarily ubiquitous issue... 2D to 3D space... IE. mousePosition thru to objects in a viewport...

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 = screenMatrix.Transform(pointOnScreenAs3D);
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 = reverseTransform.Transform(pointOnScreen);
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?


— simon-john roberts, Sat, 22 Sep 2007 13:24:47 -0400 (EDT)

This is not a place to ask questions. This is a place for comments.

A better place for WPF programming questions is the MSDN Forum for WPF. If you have a question about something in one of my books, you can email me and I'll try to answer your question.

You are using 3DTools. I did not write 3DTools. Perhaps directing this question to the creators of 3DTools might be more appropriate.

I wrote a book entitled 3D Programming for Windows that has some sample code for converting between 2D and 3D coordinates, and a library that helps you do it. This book will make you smarter.

Charles Petzold, Sat, 22 Sep 2007 14:02:31 -0400 (EDT)

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold