2

Given a vector v1=(0,0,1) and two vectors v2=(1,0,0) and v3=(-1,0,0) I would expect v1.angleTo(v2) and v1.angleTo(v3) to to return different results, i.e. 1/2 PI and 3/2 PI.

However, both return 1/2 PI:

var v1 = new THREE.Vector3(0, 0, 1);
var v2 = new THREE.Vector3(1, 0, 0);
var v3 = new THREE.Vector3(-1, 0, 0);

v1.angleTo(v2)
1.5707963267948966

v1.angleTo(v3)
1.5707963267948966

It seems that angleTo always returns the smaller angle, i.e. values between 0 and PI.

How can I get the expected value/behavior?

Tov Aqulic
  • 265
  • 1
  • 13
  • 1
    The results you get is what I would expect, 90 degrees for both angles. – 2pha Jun 13 '20 at 13:09
  • 1
    By default, an angle is always measured counterclockwisely, so I would expect one angle to be 90 degrees, the other to be 270 degrees. – Tov Aqulic Jun 13 '20 at 15:26

1 Answers1

2

angleTo always returns the smaller angle. See the implementation of angleTo in https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js.

If the angle should always be determined in one direction (i.e. either counterclockwisely or clockwisely), a simple solution for 2d vectors (or n-d vectors in a 2d plane parallel to a two-axes-plane of the coordinate system, as in the example given in the question) is:

var orientation = v1.x * v2.z - v1.z * v2.x;
if(orientation > 0) angle = 2*Math.PI - angle;
Tov Aqulic
  • 265
  • 1
  • 13
  • `Math.atan2(y, x)` could've been used to handle this particular case (See [this thread](https://stackoverflow.com/a/12011762/2228912)) – Hakim Jun 07 '21 at 09:44