诚然,这就是我是如何这样做的:
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);
添加字数为3个,将我的模型移至多个空间(x、 y、z),但当我给它提供上述数字时,该模型并没有朝它轮值的方向移动(关于XZ飞机)。
为什么不这样做?
Edit: 新的法典,虽然现在有大约45度。
Vector3 move = new Vector3();
move = player.model.getRotation().applyPost(new Vector3(1,0,0), move);
move.multiplyLocal(-player.speed);
player.model.addTranslation(move);