将四元数拆分为轴旋转 [英] Split quaternion into axis rotations
问题描述
我有一个表示对象(黄色框和球体)方向的四元数.我想知道是否有可能将该四元数分解为其他四元数,从而使每个局部轴(X,Y和Z)旋转.
I have a quaternion representing the orientantion of an object (yellow box and sphere). I would like to know if it is possible to split that quaternion into other quaternions that give us the rotation of each local axis (X, Y and Z).
到目前为止,我一直在做Euler表示并使用它,但这并不是针对我的特定情况的正确解决方案:
What I have been doing until now is getting the Euler representation and work with it, but it is not the correct solution for my particular case:
给出两个点(蓝色框),我想限制对象的方向,以便即使四元数从该平面之外也不能指向灰色平面.
我想分割(分解)四元数,因为当我的对象达到平面的极限(例如,右侧)时,我想使其保持在该位置(在该组件中),然后在垂直组件中旋转我的对象,使用新的分裂四元数之一.
Given two points (blue boxes), I want to limit the orientation of my object so that it can't point out of the grey plane, even if my quaternion looks out of that plane.
I want to split (decompose) the quaternion, because when my object reachs the limit of the plane (for instance, the right), I want to make it stay there (in that component), and then rotate my object in the vertical component, using one of the new splitted quaternion.
我正在与Unity合作.
I am working with Unity.
我希望这是可以理解的问题:)
I hope it is understandable my problem :)
推荐答案
这里是一种仅获取y轴局部旋转的方法.可以修改此功能以获取x轴或z轴.
Here's a way to get the local rotation of just the y-axis. This function can be modified to get the x or z-axis.
/// <summary> isolate the y-Component of a rotation </summary>
private Quaternion yRotation(Quaternion q)
{
float theta = Mathf.Atan2(q.y, q.w);
// quaternion representing rotation about the y axis
return new Quaternion(0, Mathf.Sin(theta), 0, Mathf.Cos(theta));
}
您可以通过转换为Euler在Unity检查器中验证结果:
You can verify the result in the Unity inspector by converting to Euler:
public float yLocal;
void Update()
{
yLocal = yRotation(this.transform.rotation).eulerAngles.y;
}
这篇关于将四元数拆分为轴旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!