Why do I have to run this code twice to rotate the 3d object?

Asked

Viewed 128 times

-1

My code’s a bit tricky, but I only got it the way it’s down.

Plus I have to run twice to be able to turn the object and I don’t know why.

I’d like to understand what’s happening 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);
  • 1

    Icarus, if I were to vote to close your question it would be as if it were not clear. You had not tagged Unity3d, but this has already been solved for you. Anyway, when you say that the object "rotates", what exactly you mean? Try to illustrate your scenario, perhaps with images (small!) of before and after the object being rotated or with some sketch of the axis and angle of the intended and realized turn. Or, better yet, provide a [mcve] so that someone interested in helping you can at least test your problem.

  • 1

    @Luizvieira I also felt inclined to vote to close. If I hadn’t tagged unity3d, she would have already gone into space. However, even as it is and having risked posting an answer, I still see it as a question that is on top of the "should be closed vs still can answer" line. Icarus, consider Luiz’s comment up there to improve your question and make it perfectly responsive for everyone. Even to write my answer, I had to guess and risk a few things, which is not a good sign.

  • @Victorstafusa I found her response very good (and I even voted for her). I just don’t know if it helps in fact the AP based on the comments you exchanged later. And the fault is that the question is not clear enough (although it is not totally bad). Icarus, improve the question if possible, consider accept the answer of Victor if she helped you, and remember that you can open new questions if you have other related questions. If you haven’t done it yet, do the [tour] and mainly read [Ask].

1 answer

4

Your question is not clear, but as I’ve handled Unity3d, I saw that’s what you’re talking about, not just C#.

Let’s explain the code, line by line:

var device3d = helixViewport3D.Children[0];

I don’t know exactly what the helixViewport3D, but the way it’s being used is something that contains one or more objects as a child. The first of these objects is the device3d, who I suppose is a GameObject.

var matrix = device3d.Transform.Value;

Each object has an associated matrix. 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 functioning of the matrix are difficult to understand, as they require an advanced knowledge in linear algebra.

However, the Unity3d API abstracts the gory mathematical details so that you can work with these matrices without having to go so deep into the details of the mathematics behind it.

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

That one Quaternion created is a set of values that represents a rotation at 90 degrees. Again, the mathematical foundation behind it is complicated, but it is abstracted. This rotation is applied to the matrix. Since the matrix represents the position of an object, along with its size and rotation, this operation will rotate the object by 90 degrees.

device3d.Transform = new MatrixTransform3D(matrix);

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

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

These two lines remove the element from the first position of the helixViewport3D (which is the device3d) and puts it there again. I believe it’s totally unnecessary and that in the end it doesn’t end up doing anything, but I’m not sure that.

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

  • In fact, it rotates 1 degree, and I put this Removeat and Add why when I updated the object by putting a "=" for example, in the view did not appear the spin, it is as if it had some function that updates and Removeat updates automatically :/

  • @I couldn’t find the builder of Quaternion you are using in the documentation: https://docs.unity3d.com/ScriptReference/Quaternion.html

  • https://msdn.microsoft.com/pt-br/library/ms558504(v=vs.110). aspx

  • 1

    @Icaarodantas Now I got really excited and confused. O Quaternion that I expected to see was not this. So I do not know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.