How to get maya like rotation?

I am trying to achieve the same rotational effect as maya in my project. I have some knowledge about quaternions and the trackball example.

Unfortunately, I still can’t bow my head to the concept of using quaternions to get the desired effect.

Basically, I am still getting the same problem as before with a trackball. By turning the object upside down, and then try to turn right, the object will rotate to the left. Well, actually his camera rotates around the focus point in the opposite direction.

The problem is that I use the screen coordinates and the trackball to get the old / new vectors and get the rotation angle from these two vectors. I will always have the wrong axis of rotation this way.

How do I solve this problem?

+4
source share
2 answers

I don’t know Maya, so I can only guess that its rotation looks like this: if you rotate left-right, it seems natural. Then, if you turn the object up 180 degrees, then turn left-right again, it still feels natural.

If you are familiar with the concept of using a matrix to perform transformations (for example, rotate, scale, and translate), then the quaternion is one and the same concept, but it only allows rotation, so you can use it to limit your transformation to just rotation. In practice, you can use either a matrix or a quaternion to do the same.

What you need to do is remember the current state of the quaternion for the object, then when the next rotation frame happens, multiply the new rotation by the old quaternion (in that order) to give you the next quaternion of the frame. This ensures that no matter what orientation the object is in, the next rotation of the frame will be applied from the viewer's point of view. This is in contrast to some naive rotation, in which you simply say: "the user scrolls up / down, therefore, changes the rotation of the X axis", which leads to a revolution.

Remember that, like matrices, quaternions must be multiplied in reverse order for the actions to actually apply, so I said to multiply the new operation with the existing quaternion.

To finish with an example. Let's say a user performs 2 actions:

  • In frame 1, the user rotates the object 180 degrees around the X axis (up / down rotation).
  • In frame 2, the user rotates the object 90 degrees around the Y axis (left / right rotation).

Suppose the object has a quaternion Q. Each frame, you will reset the object to its default coordinates and apply the quaternion Q to rotate it. Now you can initialize it with an identity quaternion, but let's just say that the initial quaternion is called Q0.

  • In frame 1, create a new quaternion R1, which is the “180-degree rotation along the X axis” quaternion (you can find the math to calculate such a quaternion). First, multiply the new operation by the existing quaternion: Q1 = R1 * Q0.
  • In frame 2, create a new quaternion R2, which is a “90 degree rotation around the Y axis.” First multiply the new operation by the existing quaternion: Q2 = R2 * Q1.

In frame 1, you will use Q1 to display the object, and in frame 2 you will use Q2. You can simply continue to apply any subsequent user actions to the quaternion, and it will always be rotated in the preview frame.

+2
source

I think you have problems with a changing coordinate system.

Suppose you want to rotate an object in the X axis, then in the Y axis, and then move it and scale it. Thus, you must multiply the maxtrix transformation (at the beginning it is equal to the itentity matrix) by the rotation matrix (first by X, then by Y), then by the translation matrix and finally by the scaling matrix. So, when your current matrix is ​​multiplied by the resulting matrix, your coordinate systems change.

To avoid this problem, you can use 2 methods:

1) to copy your result matrix as a product of all previous matrices.

2) use the stack, where in the upper part there will be a matrix, which is equal to the product of all matrices in the lower part of this matrix (in the stack).

PS I'm not sure if this helps you. I have never used quaternions in my projects.

+1
source

All Articles