这是我不久前为 3 级火箭发射查看器编写的一些代码。我从http://www.euclideanspace.com/maths/geometry/rotations获得了大部分信息
注意:偏航、俯仰和滚动可能会根据您设置坐标系的方式而改变
// Assuming the angles are in radians.
double p = curPitch * Math.PI/180.0 / 2.0;
double y = curYaw * Math.PI/180.0 / 2.0;
double r = curRoll * Math.PI/180.0 / 2.0;
double sinp = Math.sin(p);
double siny = Math.sin(y);
double sinr = Math.sin(r);
double cosp = Math.cos(p);
double cosy = Math.cos(y);
double cosr = Math.cos(r);
Vector3 axis = new Vector3();
//here's the important part: how you get your quaternion vector!
axis.x = sinr * cosp * cosy - cosr * sinp * siny;
axis.y = cosr * sinp * cosy + sinr * cosp * siny;
axis.z = cosr * cosp * siny - sinr * sinp * cosy;
//now normalize the vector in case we want to use it again later
axis = Vector3.normalizeVector(axis);
orientation[1] = axis.x;
orientation[2] = axis.y;
orientation[3] = axis.z;
//w is omega: the angle to rotate about the quaternion
double w = cosr * cosp * cosy + sinr * sinp * siny;
w = Math.acos(w) * 2.0;
orientation[0] = w;
gl.glPushMatrix();
//translate object first, then rotate it.
gl.glTranslated(curDisplacement[0] + saveDisplacement[0], -curDisplacement[1] + saveDisplacement[2], curDisplacement[2] + saveDisplacement[1]);
//this order might be messed up because I screwed up my coordinate system, but the idea is still there
gl.glRotated(orientation[0]*180/Math.PI, orientation[2]*180/Math.PI, orientation[3]*180/Math.PI, orientation[1]*180/Math.PI);
//place your objects
gl.glPopMatrix();
希望这可以帮助!