|
|
发表于 2005-12-26 12:19:00
|
显示全部楼层
Re:困惑的问题,从矩阵到分量
唯一估计够戗。但结果应该差不多。
贴个quat转xyz的。
BOOL QuatToEuler_(ZQuat* quat, ZVector3* euler){
/// Local Variables ///////////////////////////////////////////////////////////
float matrix[3][3];
float cx,sx,x;
float cy,sy,y,yr;
float cz,sz,z;
///////////////////////////////////////////////////////////////////////////////
// CONVERT QUATERNION TO MATRIX - I DON'T REALLY NEED ALL OF IT
matrix[0][0] = 1.0f - (2.0f * quat->y * quat->y) - (2.0f * quat->z * quat->z);
// matrix[0][1] = (2.0f * quat->x * quat->y) - (2.0f * quat->w * quat->z);
// matrix[0][2] = (2.0f * quat->x * quat->z) + (2.0f * quat->w * quat->y);
matrix[1][0] = (2.0f * quat->x * quat->y) + (2.0f * quat->w * quat->z);
// matrix[1][1] = 1.0f - (2.0f * quat->x * quat->x) - (2.0f * quat->z * quat->z);
// matrix[1][2] = (2.0f * quat->y * quat->z) - (2.0f * quat->w * quat->x);
matrix[2][0] = (2.0f * quat->x * quat->z) - (2.0f * quat->w * quat->y);
matrix[2][1] = (2.0f * quat->y * quat->z) + (2.0f * quat->w * quat->x);
matrix[2][2] = 1.0f - (2.0f * quat->x * quat->x) - (2.0f * quat->y * quat->y);
sy = -matrix[2][0];
cy = sqrt(1 - (sy * sy));
yr = (float)atan2(sy,cy);
euler->y = yr;////(yr * 180.0f) / (float)D3DX_PI;
// AVOID DIVIDE BY ZERO ERROR ONLY WHERE Y= +-90 or +-270
// NOT CHECKING cy BECAUSE OF PRECISION ERRORS
if (sy != 1.0f && sy != -1.0f)
{
cx = matrix[2][2] / cy;
sx = matrix[2][1] / cy;
euler->x = (float)atan2(sx,cx);///((float)atan2(sx,cx) * 180.0f) / (float)D3DX_PI; // RAD TO DEG
cz = matrix[0][0] / cy;
sz = matrix[1][0] / cy;
euler->z = (float)atan2(sz,cz);///((float)atan2(sz,cz) * 180.0f) / (float)D3DX_PI; // RAD TO DEG
}
else
{
// SINCE Cos(Y) IS 0, I AM SCREWED. ADOPT THE STANDARD Z = 0
// I THINK THERE IS A WAY TO FIX THIS BUT I AM NOT SURE. EULERS SUCK
// NEED SOME MORE OF THE MATRIX TERMS NOW
matrix[1][1] = 1.0f - (2.0f * quat->x * quat->x) - (2.0f * quat->z * quat->z);
matrix[1][2] = (2.0f * quat->y * quat->z) - (2.0f * quat->w * quat->x);
cx = matrix[1][1];
sx = -matrix[1][2];
euler->x = (float)atan2(sx,cx);////((float)atan2(sx,cx) * 180.0f) / (float)D3DX_PI; // RAD TO DEG
cz = 1.0f;
sz = 0.0f;
euler->z = (float)atan2(sz,cz);///((float)atan2(sz,cz) * 180.0f) / (float)D3DX_PI; // RAD TO DEG
}
return TRUE;
}
|
|