浏览代码

Merge pull request #9613 from Popov72/fix-anglebetweenvectors

Fix NaN values returned by GetAngleBetweenVectors
sebavan 4 年之前
父节点
当前提交
f5403fa114
共有 2 个文件被更改,包括 4 次插入2 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 3 2
      src/Maths/math.vector.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -74,6 +74,7 @@
 - Fix clip plane not reset to the rigth value when using mirrors ([Popov72](https://github.com/Popov72))
 - Fix lens flares not working in right handed system ([Popov72](https://github.com/Popov72))
 - Fix canvas not resized correctly in a multi-canvas scenario ([Popov72](https://github.com/Popov72))
+- Fix NaN values returned by `GetAngleBetweenVectors` when vectors are the same or directly opposite ([Popov72](https://github.com/Popov72))
 
 ## Breaking changes
 

+ 3 - 2
src/Maths/math.vector.ts

@@ -1431,12 +1431,13 @@ export class Vector3 {
         const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
         const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
         const dot: number = Vector3.Dot(v0, v1);
+        const angle = Math.acos(dot);
         const n = MathTmp.Vector3[3];
         Vector3.CrossToRef(v0, v1, n);
         if (Vector3.Dot(n, normal) > 0) {
-            return Math.acos(dot);
+            return isNaN(angle) ? 0 : angle;
         }
-        return -Math.acos(dot);
+        return isNaN(angle) ? -Math.PI : -Math.acos(dot);
     }
 
     /**