2013-10-23 53 views
0

我在哪裏可以找到一個函數(最好是用PHP編寫的),即四元數旋轉轉換爲3D旋轉(輥距偏離),也解決了萬向鎖的問題(在萬向節鎖定的角度不返回NAN)。由於四元數輥距偏航

回答

0

這不是PHP,但是C++。應該很容易轉移到PHP。無效/萬向情況可以看出,其中任一rotateX,旋轉Y或rotateZ不assinged因爲該值會爲NaN。

 //Our Quaternion is defined by qx,qy,qz,qw 
     double qx; 
     double qy; 
     double qz; 
     double qw;   

     double rotateXa0 = 2.0*(qy*qz + qw*qx); 
     double rotateXa1 = qw*qw - qx*qx - qy*qy + qz*qz; 
     double rotateX = 0.0; 
     if (rotateXa0 != 0.0 && rotateXa1 != 0.0) 
      rotateX = atan2(rotateXa0, rotateXa1); 

     double rotateYa0 = -2.0*(qx*qz - qw*qy); 
     double rotateY = 0.0; 
     if(rotateYa0 >= 1.0) 
      rotateY = M_PI/2.0; 
     else if(rotateYa0 <= -1.0) 
      rotateY = -M_PI/2.0; 
     else rotateY = asin(rotateYa0); 

     double rotateZa0 = 2.0*(qx*qy + qw*qz); 
     double rotateZa1 = qw*qw + qx*qx - qy*qy - qz*qz; 
     double rotateZ = 0.0; 
     if (rotateZa0 != 0.0 && rotateZa1 != 0.0) 
      rotateZ = atan2(rotateZa0, rotateZa1);