2

I need to rotate and translate an object. I have a problem with that, I can only do one of them. I use this code:

RotateTransform3D myRotate = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), Convert.ToDouble(180)), new Point3D(0, 0, 0));
TranslateTransform3D myTranslate = new TranslateTransform3D(0, 0, 100);
ModelVisual3D device3D2 = new ModelVisual3D();
device3D2.Content = Display3d(MODEL_PATH2);
device3D2.Transform = myRotate;
device3D2.Transform = myTranslate;
viewPort3d.Children.Add(device3D2);

The problem is that it only does the last transform. I mean, if I apply "myRotate" the second one, it rotates the object, but it doesn't apply the "myTranslate" operation. I need to do both transforms.

I am using HelixToolkit too.

Imrik
  • 674
  • 2
  • 14
  • 32
  • I don't know anything about that framework, but it's clear the problem is that you are setting the `Transform` property, and then changing it. So by the time it comes to be used (via the viewport) then only the last one is used. There must be a way to either *combine* transform object, or create a collection. – musefan Oct 20 '15 at 14:58
  • yes, you are correct, thanks! but... any ideas how to improve that? :) – Imrik Oct 20 '15 at 15:01
  • Had a quick look, but couldn't see anything obvious. Personally I would probably play with Intellisense and look for classes/methods that look like they might help, then try them – musefan Oct 20 '15 at 15:04

3 Answers3

1

You have to mess with the Transform fields directly, instead of replacing the entire Transform, i.e. transform.position, transform.rotation, transform.localPosition, transform.localEulerAngles, transform.scale, transform.localScale, etc.

device3D2.Transform.rotation *= new Quaternion.AngleAxis(180f, new Vector3(0f, 0f, 1f));
device3D2.Transform.Translate(0, 0, 100);
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
1

You need to use a Transform3DGroup object. You then add the different transformations to it:

Transform3DGroup  myTransformer = new Transform3DGroup;  
RotateTransform3D myRotate = new RotateTransform3D(new AxisAngleRotation3D    (new Vector3D(0, 0, 1), Convert.ToDouble(180)), new Point3D(0, 0, 0));
TranslateTransform3D myTranslate = new TranslateTransform3D(0, 0, 100);
myTransformer.Children.Add(myRotate);
myTransformer.Children.Add(myTranslate);
ModelVisual3D device3D2 = new ModelVisual3D();
device3D2.Content = Display3d(MODEL_PATH2);
device3D2.Transform = myTransformer;
viewPort3d.Children.Add(device3D2);

Pay attention to the sequence in which you add the individual transformers.

sulzi
  • 31
  • 3
  • What if there are multiple transforms? Or if transform is controlled from keyboard by user and there will be a ton of objects sitting in collection – Ivan P. Jan 14 '21 at 09:24
0

I am using HelixToolkit as well.

Here I create model and translate it on scene:

            ModelVisual3D mdl = new ModelVisual3D();
            mdl.Content = getModel3D();
            if ((thecurrentBox.upperLeft.X != 0)||(thecurrentBox.bottomRight.Y!=0))  {
                Matrix3D mm = mdl.Transform.Value;
                mm.Translate(new Vector3D(-thecurrentBox.upperLeft.X, 0, -thecurrentBox.bottomRight.Y));
// you can do even more transformations here. 
//you can make mm as private field and transform it whenever you like
                mdl.Transform = new MatrixTransform3D(mm);
            }
// add mdl to Children of scene

I get a current value of transformation matrix, do ops on it then set up a transform on ModelVisual3D instance. Here is link to another example: https://github.com/wolfoerster/WFTools3D/blob/50cc33f9f9929d4651d0855c386d38e6861382b2/WFTools3D/Basics/Object3D.cs#L142

Ivan P.
  • 832
  • 2
  • 9
  • 26