vector3.angleTo with vec on (0,0,0) returns NaN
See original GitHub issueI noticed that vector3.angleTo() returns NaN when either the vector on which it’s called or the target vector is at the origin (0,0,0).
let vec1 = new THREE.Vector3( -2, 2, 0 );
let vec2 = new THREE.Vector3( 0, 0, 0 );
console.log( vec2.angleTo(vec1) ); // NaN
console.log( vec1.angleTo(vec2) ); // NaN
Three.js version : r108 Browser : Chrome OS : Windows
Issue Analytics
- State:
- Created 4 years ago
- Comments:29 (8 by maintainers)
Top Results From Across the Web
Angle Function occasionally returns NaN - Stack Overflow
I have this angle function I'm using for a Unity project, but it occasionally returns NaN, and I ...
Read more >Vector3#angleTo – three.js docs
Returns the angle between this vector and vector v in radians. # .ceil () : this. The x, y and z components of...
Read more >Get it straight - Using Angle in Unity - YouTube
Let's straightening up how to use angles in Unity. In this episode I will show how to make the Vector2.Angle (same for Vector3...
Read more >3D Vectors: Angles to the axes (A-level Maths) - YouTube
How to calculate the angle between a 3D vector and each of the coordinate axes. Also includes animations (courtesy of GeoGebra) to allow...
Read more >How to Calculate Angles in Unity - A Unity Math Tutorial
NaN / NaN. Back. Skip navigation. Search. Search. Search with your voice. Sign in. How to Calculate Angles in Unity - A Unity...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@felixmariotto You are right, this behavior is unfortunate. I suggest the same what Unity does in Vector3.Angle.
Vector3.angleTo()
would look like so then:As the user has to deal with valid inputs anways (test for non-zero vectors), wouldn’t it be better to just return π/2 radians (90°) instead of a console.error? Returning π/2 seems for me to be mathematical correct … Wikipedia: “In Euclidean space, two vectors are orthogonal if and only if their dot product is zero, i.e. they make an angle of 90° (π/2 radians), or one of the vectors is zero.” (https://en.wikipedia.org/wiki/Orthogonality). Wikibooks: “the angle between the zero vector and any other vector is defined to be a right angle” (https://en.wikibooks.org/wiki/Linear_Algebra/Length_and_Angle_Measures). Returning (mathematically wrong) NaN (and conole.error) because the user should not try to calculate the angle to/from a zero-vector doesn’t look like a good solution for me.
Users would then only have to check for zero-vectors if the returned angle is exactly π/2 and only if this is relevant later on.
By the way: I use “angleTo” while calculating weighted (angle and surface) vertex normals. That worked perfectly fine because I had to test the angle to be in a certain range anyways. After updating three.js I got thousands of console.errors, so I now have to test every (calculated) vector to be non-zero before using “angleTo”.