我在这个问题上花了几个星期,似乎找不到合适的解决方案,需要一些建议。
我正在使用 LWJGL/Java 创建一个 Camera 类,并使用四元数来处理方位(偏航)、俯仰和滚动旋转。我希望这台相机能够处理 3D 空间中的所有 6 度运动并滚动。轴承、俯仰和滚动都是四元数。我将它们乘以“变化”四元数,并从中创建一个平移矩阵。我把它放在一个浮点缓冲区中,并将模型视图矩阵乘以包含旋转矩阵的缓冲区。
我可以让轴承和俯仰旋转正常工作,但是当我实施滚动时,我遇到了问题。主要是,围绕 Z 轴旋转(滚动)似乎不起作用。当我“滚动”相机时,它似乎围绕全局 Z 轴而不是本地相机方向轴滚动。根据我乘以四元数的顺序,我通常可以让 3 个中的 2 个工作,但我不能让它们一起工作。
由于它们都是独立工作的,我假设我的定向方法有问题,我将它们组合并构建一个旋转矩阵。我在粘贴整个类时遇到问题,所以这里是与轮换相关的方法和声明:
private final static float DEGTORAD = (float)(Math.PI/180);
//Eye - position of the camera in the 3D world.
private Vector3f eye;
//Camera axis vectors, calculated each time reorient() is called.
//Initialized to global x, y, and z axis initially.
private Vector3f up;
private Vector3f right;
private Vector3f direction;
//Angles of rotation (in degrees)
private float pitchAngle;
private float bearingAngle;
private float rollAngle;
private Quaternion pitch;
private Quaternion bearing;
private Quaternion roll;
private FloatBuffer viewMatrixBuffer = BufferUtils.createFloatBuffer(16);
private Quaternion currentOrientation;
...
/**
* Change the bearing (yaw)
* @param bearing delta in degrees
*/
public void bearing(float bearingDelta){
bearingAngle += bearingDelta;
if(bearingAngle > 360){
bearingAngle -= 360;
}else if(bearingAngle < 0){
bearingAngle += 360;
}
bearing.setFromAxisAngle(new Vector4f(0f, 1f, 0f, bearingAngle * DEGTORAD));
bearing.normalise();
}
/**
* Change the pitch
* @param pitch delta in degrees
*/
public void pitch(float pitchDelta){
pitchAngle += pitchDelta;
if(pitchAngle > 360){
pitchAngle -= 360;
}else if(pitchAngle < 0){
pitchAngle += 360;
}
pitch.setFromAxisAngle(new Vector4f(1f, 0f, 0f, pitchAngle * DEGTORAD));
pitch.normalise();
}
/**
* @param initialRoll
*/
public void roll(float initialRoll) {
rollAngle += initialRoll;
if(rollAngle > 360){
rollAngle -= 360;
}else if(rollAngle < 0){
rollAngle += 360;
}
roll.setFromAxisAngle(new Vector4f(0, 0, 1, rollAngle * DEGTORAD));
roll.normalise();
}
/**
* Change direction to focus on a certain point in the world
* @param eye
*/
public void lookThrough(){
reorient();
GL11.glMultMatrix(viewMatrixBuffer);
}
public void reorient(){
//Multiply in order: bearing, pitch, roll. Non-commutative!
Quaternion change = new Quaternion();
Quaternion.mul(bearing, pitch, change);
Quaternion.mul(roll, change, change);
// orient the camera...
Matrix4f rotationMatrix = getRotationMatrix(change);
//Get the looking direction
direction.x = rotationMatrix.m20;
direction.y = rotationMatrix.m21;
direction.z = rotationMatrix.m22;
//Set the position
rotationMatrix.m30 = eye.x;
rotationMatrix.m31 = eye.y;
rotationMatrix.m32 = eye.z;
rotationMatrix.m33 = 1;
rotationMatrix.invert();
rotationMatrix.store(viewMatrixBuffer);
viewMatrixBuffer.rewind();
Vector3f.cross(new Vector3f(0,1,0), direction, null).normalise(right);
Vector3f.cross(right, direction, null).normalise(up);
}
Vector3f、Quaternion 和 Matrix4f 都是 LWJGL 类,不是定制的。
所以我的问题是,给定代表轴承、俯仰和滚动的 3 个四元数,我如何修改 ModelView 矩阵以准确表示这些旋转?
编辑:我觉得这非常接近。请参阅 RiverC 评论中的要点链接。在旋转了这么多度之后,视图在滚动时恢复正常之前跳了很多。它的要点在那里,但它仍然略微偏离。