Why do I need to run this code twice to rotate the 3d object? [closed]

-1

My code is a gambiarra, but I only managed it the way it is below.

In addition I have to run twice to be able to rotate the object and I do not know why.

I would like to understand what happens here.

var device3d = helixViewport3D.Children[0];
var matrix = device3d.Transform.Value;
matrix.Rotate(new Quaternion(new Vector3D(1, 0, 0), 1));
device3d.Transform = new MatrixTransform3D(matrix);
helixViewport3D.Children.RemoveAt(0);
helixViewport3D.Children.Add(device3d);
    
asked by anonymous 06.01.2017 / 23:38

1 answer

4

Your question is unclear, but as I've already moved with Unity3D, I've seen that's what you're talking about, not just C #.

Let's explain the code, line by line:

var device3d = helixViewport3D.Children[0];

I'm not exactly sure what helixViewport3D is, but the way it's being used is something that contains one or more objects as a child. The first of these objects is device3d , which I suppose is a GameObject .

var matrix = device3d.Transform.Value;

Each object has an associated array. This matrix contains data about the position of the object in the virtual world, as well as its size and orientation. The details of the mathematical operation of the matrix are difficult to understand, since they require advanced knowledge in linear algebra.

However, the Unity3D API abstracts the bloody mathematical details so you can work with those arrays without having to go deeper into the details of the mathematics behind it.

matrix.Rotate(new Quaternion(new Vector3D(1, 0, 0), 1));

This created% is a set of values representing a 90-degree rotation. Again, the mathematical foundation behind is complicated, but it is abstracted. This rotation is applied to the matrix. Since the matrix represents the position of some object, along with its size and rotation, this operation will rotate the object by 90 degrees.

device3d.Transform = new MatrixTransform3D(matrix);

This then applies the new array to the object, which results in its rotation by 90 degrees.

helixViewport3D.Children.RemoveAt(0);
helixViewport3D.Children.Add(device3d);

These two rows remove the element from the first position of Quaternion (which is helixViewport3D ) and put it there again. I think it's totally unnecessary and that in the end it does not end up doing anything, but I'm not sure.

As a result of these operations, the object will be rotated by 90 degrees. By applying this twice, the rotation will be 180 degrees.

    
07.01.2017 / 07:26