Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 四元数到角_Java_Algorithm_Math_Geometry_Trigonometry - Fatal编程技术网

Java 四元数到角

Java 四元数到角,java,algorithm,math,geometry,trigonometry,Java,Algorithm,Math,Geometry,Trigonometry,好的,我就是这样做的: 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 =

好的,我就是这样做的:

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);
}
AddTranslation需要3个数字来移动我的模型,移动的距离超过许多空间(x、y、z),但当我给它上面的数字时,它不会沿着旋转的方向移动模型(在XZ平面上)

为什么这不起作用

编辑:新代码,尽管现在大约偏离45度

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
zrot
定义四元数指定的旋转轴。我认为您不想在
addTranslation()
调用中使用它们……一般来说,这与运动方向无关

我的意思是:你的三维物体——为了论证它是一架飞机——在它的原始坐标中会有一个特定的运动方向 系统。如果原始方向的质心在原点,那么 螺旋桨沿着+X轴的某个地方,飞机想要朝+X方向飞行

现在引入一些坐标变换,将飞机旋转到其他方向。该旋转由旋转矩阵描述,或等效地由旋转矩阵描述 四元数。旋转后,平面要向哪个方向移动

你可以找到 通过在+X方向上取一个单位向量:(1.0,0.0,0.0),然后应用 将旋转矩阵或四元数转换为该向量,以将其转换为新坐标 系统。(然后按速度进行缩放,如上所述。)X、Y和Z分量
对于变换后的缩放向量,沿每个轴提供所需的增量运动。转换后的向量通常不会成为四元数的旋转轴,我想这可能是你的问题。

谢谢,但我如何将矩阵/四元数应用于向量?对于3x3旋转矩阵M和列向量V,旋转向量U是由矩阵乘积U=MV给出的。好的,这很好,但它大约偏离-45度。有办法解决吗?嗯,让它变成90度。如果不知道你们的输入、预期输出和观察到的输出是什么,很难说更多。好的,我让它工作了。其他一些数学题被取消了。^^;非常感谢你的帮助。
Vector3 move = new Vector3();
move = player.model.getRotation().applyPost(new Vector3(1,0,0), move);
move.multiplyLocal(-player.speed);
player.model.addTranslation(move);