浏览代码

added test/fix for singularity when converted quaternions to eulers

Adam Bowman 8 年之前
父节点
当前提交
5923d925f4
共有 1 个文件被更改,包括 17 次插入4 次删除
  1. 17 4
      src/Math/babylon.math.ts

+ 17 - 4
src/Math/babylon.math.ts

@@ -1935,13 +1935,26 @@
             var sqx = qx * qx;
             var sqy = qy * qy;
 
-            result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
-            result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
-            result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
+            var zAxisY = qy*qz - qx*qw;
+            var limit = .4999999;
+
+            if(zAxisY < -limit){
+                result.y = 2 * Math.atan2(qy,qw);
+                result.x = Math.PI/2;
+                result.z = 0;
+            }else if(zAxisY > limit){
+                result.y = 2 * Math.atan2(qy,qw);
+                result.x = -Math.PI/2;
+                result.z = 0;
+            }else{
+                result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
+                result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
+                result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
+            }
 
             return this;
             
-        };
+        }
 
         public toRotationMatrix(result: Matrix): Quaternion {
             var xx = this.x * this.x;