四元数角 [英] quaternion to angle

查看:104
本文介绍了四元数角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以这是我怎么做的吧:

 浮法xrot = 0;
浮yrot = 0;
浮zrot = 0;

四元数Q =新四元数()fromRotationMatrix(player.model.getRotation())。
如果(q.getW()→1){
    q.normalizeLocal();
}

浮动角=(浮点)(2 * Math.acos(q.getW()));
双S =的Math.sqrt(1- q.getW()* q.getW());

//测试,以避免被零除,s是总是由于开方正
如果//收盘于轴线并不重要为零,则方向
如果(S< 0.001){
    //如果重要的是轴归然后更换其中x = 1; Y = Z = 0;
    xrot = q.getXf();
    yrot = q.getYf();
    zrot = q.getZf();
    // Z = q.getZ();
} 其他 {
    xrot =(浮点)(q.getXf()/秒); //规范化轴
    yrot =(浮点)(q.getYf()/秒);
    zrot =(浮点)(q.getZf()/秒);
}
 

但它似乎没有工作,当我试图把它投入使用:

  player.model.addTranslation(xrot * player.speed,0,zrot * player.speed);
 

AddTranslation需要3个数字由比许多空间(X,Y,Z)移动我的模型,但母鸡我给它上面的数字是不动​​的模型,它已被旋转的方向(在XZ平面上)

为什么不是这方面的工作?

编辑:新的code,虽然它是约45度走了

  Vector3类型的举动=新Vector3类型();
此举= player.model.getRotation()applyPost(新Vector3类型(1,0,0),移动)。
move.multiplyLocal(-player.speed);
player.model.addTranslation(移动);
 

解决方案

xrot yrot zrot 定义的四元数指定的旋转轴。我不认为你想使用它们在你的 addTranslation()打电话......在一般情况下,不会有任何与运动方向。

我的意思是:你的3-D对象 - 比方说,争论的缘故,它的一架飞机 - 将产生一定的$ P $运动pferred方向在其原来的坐标 系统。因此,如果原始取向具有质量在原点的中心,和 螺旋桨沿+ X轴的某处,飞机要在+ X方向飞行。

现在你介绍一些坐标变换,旋转飞机到一些其他的方向。该旋转由旋转矩阵描述,或等价地,通过一个 四元数。哪种方式并不飞机要在旋转后动?

您可以找到 通过采取单位矢量中的+ X方向:(1.0,0.0,0.0),然后应用 旋转矩阵或四元数到向量将它转变成新坐标 系统。 (然后缩放它的速度,因为你在做以上)的X,Y和Z组件 变换,缩放矢量给沿每个轴所需的增量运动。这一转化载体一般的没有的将是四元数的旋转轴,并且我认为这可能是你的问题。

Alright, so this is how I am doing it:

float xrot = 0;
float yrot = 0;
float zrot = 0;

Quaternion q = new Quaternion().fromRotationMatrix(player.model.getRotation());
if (q.getW() > 1) {
    q.normalizeLocal();
}

float angle = (float) (2 * Math.acos(q.getW()));
double s = Math.sqrt(1-q.getW()*q.getW());

// test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
if (s < 0.001) {
    // if it is important that axis is normalised then replace with x=1; y=z=0;
    xrot = q.getXf(); 
    yrot = q.getYf();
    zrot = q.getZf();
    // z = q.getZ();
} else {
    xrot = (float) (q.getXf() / s); // normalise axis
    yrot = (float) (q.getYf() / s);
    zrot = (float) (q.getZf() / s);
}

But it doesn't seem to work when I try to put it into use:

player.model.addTranslation(xrot * player.speed, 0, zrot * player.speed);

AddTranslation takes 3 numbers to move my model by than many spaces (x, y, z), but hen I give it the numbers above it doesn't move the model in the direction it has been rotated (on the XZ plane)

Why isn't this working?

Edit: new code, though it's about 45 degrees off now.

Vector3 move = new Vector3();
move = player.model.getRotation().applyPost(new Vector3(1,0,0), move);
move.multiplyLocal(-player.speed);
player.model.addTranslation(move);

解决方案

xrot, yrot, and zrot define the axis of the rotation specified by the quaternion. I don't think you want to be using them in your addTranslation() call...in general, that won't have anything to do with the direction of motion.

What I mean by that is: your 3-D object -- let's say for the sake of argument that it's an airplane -- will have a certain preferred direction of motion in its original coordinate system. So if the original orientation has the center of mass at the origin, and the propeller somewhere along the +X axis, the plane wants to fly in the +X direction.

Now you introduce some coordinate transformation that rotates the airplane into some other orientation. That rotation is described by a rotation matrix, or equivalently, by a quaternion. Which way does the plane want to move after the rotation?

You could find that by taking a unit vector in the +X direction: (1.0, 0.0, 0.0), then applying the rotation matrix or quaternion to that vector to transform it into the new coordinate system. (Then scale it by the speed, as you're doing above.) The X, Y, and Z components of the transformed, scaled vector give the desired incremental motion along each axis. That transformed vector is generally not going to be the rotation axis of the quaternion, and I think that's probably your problem.

这篇关于四元数角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆