Browse Source

Avoid NaN when normalizing a zero Quaternion

Returns this instead of NaN when calling the nomalize function on a zero quaternion.
Eole211 6 năm trước cách đây
mục cha
commit
62ffa346e1
1 tập tin đã thay đổi với 11 bổ sung5 xóa
  1. 11 5
      src/Maths/math.ts

+ 11 - 5
src/Maths/math.ts

@@ -3897,11 +3897,17 @@ export class Quaternion {
      * @returns the current updated quaternion
      */
     public normalize(): Quaternion {
-        var length = 1.0 / this.length();
-        this.x *= length;
-        this.y *= length;
-        this.z *= length;
-        this.w *= length;
+        var len = this.length();
+
+        if (len === 0) {
+            return this;
+        }
+
+        var inv = 1.0 / len;
+        this.x *= inv;
+        this.y *= inv;
+        this.z *= inv;
+        this.w *= inv;
         return this;
     }