四元数转角
好吧,这就是我正在做的事情:
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);
}
但是当我尝试使用它时,它似乎不起作用:
player.model.addTranslation(xrot * player.speed, 0, zrot * player.speed);
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xrot
、yrot
和zrot
定义由四元数指定的旋转轴。我认为您不想在addTranslation()
调用中使用它们......一般来说,这与运动方向没有任何关系。我的意思是:你的 3D 对象——为了论证起见,假设它是一架飞机——在其原始坐标中将有一个特定的首选运动方向
系统。因此,如果原始方向的质心位于原点,并且
螺旋桨沿 +X 轴某处,飞机想要沿 +X 方向飞行。
现在您引入一些坐标变换,将飞机旋转到其他方向。该旋转由旋转矩阵描述,或者等效地由
四元数。旋转后飞机要向哪个方向移动?
你可以找到
通过在 +X 方向取一个单位向量:(1.0, 0.0, 0.0),然后应用
旋转矩阵或四元数到该向量以将其转换为新坐标
系统。 (然后按速度缩放它,就像上面所做的那样。)X、Y 和 Z 分量
变换后的缩放矢量给出了沿每个轴所需的增量运动。转换后的向量通常不会将成为四元数的旋转轴,我认为这可能是您的问题。
xrot
,yrot
, andzrot
define the axis of the rotation specified by the quaternion. I don't think you want to be using them in youraddTranslation()
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.