Skip to content

Commit

Permalink
Merge pull request #12460 from sebavan/master
Browse files Browse the repository at this point in the history
Fix GetAngleBetweenVectors precision issue
  • Loading branch information
sebavan authored Apr 30, 2022
2 parents ff986b0 + 19593c5 commit 213714d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/dev/core/src/Maths/math.vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,10 @@ export class Vector3 {
public static GetAngleBetweenVectors(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): number {
const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
const dot: number = Vector3.Dot(v0, v1);
let dot: number = Vector3.Dot(v0, v1);
// Vectors are normalized so dot will be in [-1, 1] (aside precision issues enough to break the result which explains the below clamp)
dot = Scalar.Clamp(dot, -1, 1);

const angle = Math.acos(dot);
const n = MathTmp.Vector3[3];
Vector3.CrossToRef(v0, v1, n);
Expand Down

0 comments on commit 213714d

Please sign in to comment.