Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF 3D - Positioning Visual3D elements in a 3D scene using nested Model3DGroup transforms?

Tags:

wpf

3d

I have a 3D scene where my 3D models are being loaded in the code behind from XAML files.

Each model is comprised of a tree of nested Model3DGroups each of which has various transformations applied to it to position and orient the next subcomponent of the model in the tree. This model is then used as the content of a ModelVisual3D so that it can be displayed to the screen.

I want to attach a child ModelVisual3D to a 'parent' ModelVisual3D. This child ModelVisual3D needs to use all of the nested transformations of the parent ModelVisual3D.Content to correctly position and orient itself in the virtual space. For example, the first ModelVisual3D is a robot arm which has various moveable joints and I want to attach a tool on the end of this arm--another ModelVisual3D. How can I access this composite transform from the parent ModelVisual3Ds content property to allow me to position the next ModelVisual3D correctly relative to its parent?

like image 248
Munro Avatar asked Jun 04 '10 10:06

Munro


1 Answers

As you have no doubt observed, when you group Model3Ds in a Model3DGroup the Transform properties of the children combine with those of the parent.

It sounds like you are asking how to compute the net transform down to a particular Model3D within a tree of Model3Ds that make up what you are calling your "model". To do this you need to know (or be able to scan and discover) the path from your root Model3DGroup down to the Model3D you want to find the transform for.

Once you have this path, all that is required is to combine the Transform properties at each level. To do this, simply construct a Transform3DGroup and add the individual transforms to it.

For example, if your robot arm has Model3D components named "UpperArm", "LowerArm", and "Hand", and you wanted to find out the position and angle of the hand you might do:

 var combined = new Transform3DGroup();
 combined.Children.Add(UpperArm.Transform);
 combined.Children.Add(LowerArm.Transform);
 combined.Children.Add(Hand.Transform);

Now you can find the (0,0,0) location on the hand as follows:

 combined.Transform(new Point3D(0,0,0));

Similarly you can find other points and use them to position your other ModelVisual3D.

like image 161
Ray Burns Avatar answered Nov 09 '22 06:11

Ray Burns