question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vector3.angleTo with vec on (0,0,0) returns NaN

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:29 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Mugen87commented, Sep 20, 2019

@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:

angleTo: function ( v ) {

	var denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );

	if ( denominator === 0 ) return 0;

	var theta = this.dot( v ) / denominator;

	// clamp, to handle numerical problems

	return Math.acos( _Math.clamp( theta, - 1, 1 ) );

}
2reactions
saschandroidcommented, Jan 29, 2020

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”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found