如何获得像玛雅人一样的旋转?
我试图在我的项目中实现与 Maya 相同的旋转效果。 我对四元数和轨迹球示例有一些了解。
不幸的是,我仍然无法理解使用四元数来获得所需效果的概念。
基本上我仍然遇到与之前使用 3d 轨迹球相同的问题。将对象上下翻转后,然后尝试向右旋转,对象将向左旋转。好吧,实际上是我的相机围绕焦点以相反的方向旋转。
问题是我正在使用屏幕坐标 &轨迹球获取旧/新向量并从这两个向量获取旋转角度。这样我总是会得到错误的旋转轴。
我应该如何解决这个问题?
I am trying to achieve the same rotational effect like Maya in my project.
I have some knowledge on quaternions and the trackball example.
Unfortunately I am still unable to wrap my head around the concept of using the quaternions to get the desired effect.
Basically I am still getting the same issue I had before with the 3d trackball. After flipping the object upside down, and then trying to rotate to the right, the object will rotate to the left. Well actually its my camera rotating around the focus point in the opposite direction.
The problem is that I am using the screen coordinates & trackball to get the old / new vectors and getting the angle of rotation from those two vectors. I will always get the wrong axis of rotation this way.
How should I go about solving this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不懂Maya,所以我只能猜测它的旋转是这样的:如果你左右旋转,感觉很自然。然后如果你把物体上下旋转180度,然后再左右旋转,感觉还是很自然。
如果您熟悉使用矩阵进行变换(如旋转、缩放和平移)的概念,那么四元数就是相同的概念,但它只允许旋转,因此您可能需要使用它来将变换限制为轮换。在实践中,您可以使用矩阵或四元数来完成相同的操作。
您需要做的是记住对象当前的四元数状态,然后当下一帧旋转发生时,将新旋转与旧四元数相乘(按顺序) )给你下一帧的四元数。这将确保无论对象处于什么方向,下一帧的旋转都将从观看者的角度应用。这与一些简单的旋转相反,您只需说“用户正在向上/向下滚动,因此改变对象的 X 轴旋转”,这会导致翻转。
请记住,与矩阵一样,四元数需要以实际应用的操作的相反顺序相乘,这就是为什么我说将新操作乘以现有四元数。
最后用一个例子来结束。假设用户将执行 2 个操作:
假设该对象有一个四元数 Q。每一帧,您都会将对象重置为其默认坐标并应用四元数 Q 来旋转它。现在您可以使用恒等四元数对其进行初始化,但我们假设初始四元数称为 Q0。
在第 1 帧上,您将使用 Q1 来显示对象,在第 2 帧上,您将使用 Q2。您只需继续对四元数应用任何后续用户操作,它就会始终在查看器的参考系中旋转。
I don't know Maya so I can only guess that its rotation is like this: if you rotate left-right, it feels natural. Then if you rotate the object up-down 180 degrees, then rotate left-right again, it still feels natural.
If you are familiar with the concept of using a matrix to do transformations (like rotate, scale and translate), well a quaternion is just the same concept but it only allows rotations, so you might want to use it to constrain your transforms to just rotations. In practice, you can use either a matrix or a quaternion to do the same thing.
What you need to do is remember the current quaternion state for the object, then when the next frame of rotation occurs, multiply the new rotation with the old quaternion (in that order) to give you the next frame's quaternion. That will ensure that no matter what orientation the object is in, the next frame's rotation will be applied from the viewer's viewpoint. This is as opposed to some naive rotation where you just say "user is scrolling up/down, therefore alter the object's X-axis rotation", which causes that flipping.
Remember, like matrices, quaternions need to be multiplied in reverse order that the actions are actually applied, which is why I said to multiply the new operation by the existing quaternion.
To finish with an example. Let's say the user is going to perform 2 actions:
Lets say the object has a quaternion Q. Every frame, you will reset the object to its default coordinates and apply the quaternion Q to rotate it. Now you might initialise it with the identity quaternion, but let's just say the initial quaternion is called Q0.
On frame 1 you will use Q1 to display the object, and on frame 2 you will use Q2. You can simply keep applying any subsequent user actions to the quaternion and it will always be rotated in the viewer's frame of reference.
我认为您在更改坐标系时遇到问题。
假设您要在 X 轴上旋转对象,然后在 Y 轴上旋转对象,然后移动它并缩放。因此,您应该将变换 maxtrix(一开始等于实体矩阵)乘以旋转矩阵(首先乘以 X,然后乘以 Y),然后乘以平移矩阵,最后乘以缩放矩阵。因此,当当前矩阵乘以结果矩阵时,坐标系会发生变化。
为了避免这个问题,您可以使用两种方法:
1)将结果矩阵累积为所有先前矩阵的乘积。
2)使用堆栈,其中顶部是矩阵,它等于该矩阵底部(堆栈中)的所有矩阵的乘积。
PS我不确定它是否对你有帮助。我从未在我的项目中使用过四元数。
I think you have problems with changing coordinate system.
Suppose, you want to rotate object in X Axis, then in Y Axis, and then move it and scale. So, you should multiply your transformation maxtrix (at the beginning it equals to itentity matrix) to the rotation matrix (firstly to X, then to Y), then to translation matrix and at the end to scaling matrix. So, when your current matrix multiplies to the resulting matrix, your coordinate systems changes.
To avoid this problem you can use 2 methods:
1) to accumulate your resultig matrix as product of all previous matrices.
2) to use stack, where in the top will be the matrix, which equals to product of all matrices in the bottom of this matrix (in the stack).
P.S. I'm not sure, that it helps you. I never used quaternions in my projects.