diff --git a/src/math/Vector3.js b/src/math/Vector3.js index e83347e73f169665f315c153cb110fe553c264ce..d1be1894edd6bcdbde50b61c8072d911d3bd95cf 100644 --- a/src/math/Vector3.js +++ b/src/math/Vector3.js @@ -564,7 +564,17 @@ THREE.extend( THREE.Vector3.prototype, { angleTo: function ( v ) { - return Math.acos( Math.min ( 1, this.dot( v ) / this.length() / v.length() ) ); + // clamp, to handle numerical problems + + function clamp( x ) { + + return Math.min( Math.max( x, -1 ), 1 ); + + } + + var theta = this.dot( v ) / ( this.length() * v.length() ); + + return Math.acos( clamp ( theta ) ); }, diff --git a/test/unit/math/Vector3.js b/test/unit/math/Vector3.js index 176a2dab219f88725b71272f9d7ebe219e39b3da..2aa2370edde6f45df2804b64ed5d7ee822628602 100644 --- a/test/unit/math/Vector3.js +++ b/test/unit/math/Vector3.js @@ -292,8 +292,10 @@ test( "reflect", function() { test( "angleTo", function() { var a = new THREE.Vector3( 0, -0.18851655680720186, 0.9820700116639124 ); + var b = new THREE.Vector3( 0, 0.18851655680720186, -0.9820700116639124 ); - equal( a.angleTo( a ), 0 ); + equal( a.angleTo( a ), 0 ); + equal( a.angleTo( b ), Math.PI ); var x = new THREE.Vector3( 1, 0, 0 ); var y = new THREE.Vector3( 0, 1, 0 );