Skip to content

Commit 7e32385

Browse files
authored
Vector2: Added angleTo()
1 parent de5ab57 commit 7e32385

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/math/Vector2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as MathUtils from './MathUtils.js';
2+
13
class Vector2 {
24

35
constructor( x = 0, y = 0 ) {
@@ -364,6 +366,20 @@ class Vector2 {
364366
return angle;
365367

366368
}
369+
370+
angleTo( v ) {
371+
372+
const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() )
373+
374+
if (denominator === 0) return Math.PI / 2;
375+
376+
const theta = this.dot(v) / denominator;
377+
378+
// clamp, to handle numerical problems
379+
380+
return Math.acos( MathUtils.clamp( theta, - 1, 1 ) );
381+
382+
}
367383

368384
distanceTo( v ) {
369385

0 commit comments

Comments
 (0)