Skip to content

Conversation

@puxiao
Copy link
Contributor

@puxiao puxiao commented Jul 29, 2021

Related issue: #null

Description

Vector2 added .angleTo(), just like Vector3.angleTo().

@WestLangley
Copy link
Collaborator

const x = new THREE.Vector2( 1, 0 );
const y = new THREE.Vector2( 0, - 1 );

console.log( y.angle() ); // 4.71238898038469

console.log( x.angleTo( y ) ); // -1.5707963267948966

@puxiao
Copy link
Contributor Author

puxiao commented Jul 29, 2021

const x = new THREE.Vector2( 1, 0 );
const y = new THREE.Vector2( 0, - 1 );

console.log( y.angle() ); // 4.71238898038469

console.log( x.angleTo( y ) ); // -1.5707963267948966

Is this the result you want?

angleTo( v ) {

	let angle = Math.atan2( this.cross( v ), this.dot( v ) );
		
	if( angle < 0 ){
		angle += 2 * Math.PI;
	}
		
	return angle;

}

@WestLangley
Copy link
Collaborator

Your first implementation returned a positive result -- presumably the smallest of the two angles. That is the result I would expect in three.js.

The second implementation returns a signed result. That method likely requires a different name and proper documentation.

What is your use case for needing this method?

@puxiao
Copy link
Contributor Author

puxiao commented Jul 29, 2021

I am studying linear algebra recently.
By chance, I found that Vector3 has .angleTo() but Vector2 does not.
Emm....
In fact, I don't have a case where this method needs to be used.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 29, 2021

We usually do not enhance the API without a compelling use case. Just because Vector3 has a method does not mean it is required in Vector2 or Vector4.

@WestLangley
Copy link
Collaborator

I don't have a case where this method needs to be used.

That is what I expected. There has been no demand for this in three.js. This is a 3D library.

Also, there are many use cases: clockwise, counter-clockwise, smallest-angle, [0-2pi], [-pi, pi], signed angle, unsigned angle.

//

One option is to do what you did initially, and add the method that returns the shortest angle, unsigned.

The other option is to do nothing since there has been no demand for it.

@puxiao
Copy link
Contributor Author

puxiao commented Jul 29, 2021

Okay, I get it, thanks.

I will close this PR.

@puxiao puxiao closed this Jul 29, 2021
@jespertheend
Copy link
Contributor

I'm working on controls for a vehicle that only moves on a flat plane, and Vector2.angleTo() would have been useful here to be honest. I was hoping to keep the direction vector of the vehicle 2d to avoid it from drifting upwards.

@mrdoob
Copy link
Owner

mrdoob commented Mar 15, 2023

I think the method is so tiny that is probably worth adding it 👍

@mrdoob
Copy link
Owner

mrdoob commented Mar 15, 2023

Unfortunately I can't reopen the PR...

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 15, 2023

As mentioned in #22209 (comment) the method can be implemented differently and not every implementation fits all use cases. I agree with @WestLangley that Vector2.angleTo() should adhere the same policy as Vector3.angleTo().

@jespertheend Would the method still helpful to you if it returns the shortest unsinged angle between two vectors? I could imagine a signed angle is more useful for what you want to implement...

@jespertheend
Copy link
Contributor

Yeah I think a signed angle would probably be more useful.

I also frequently run across this case with 3d vectors:

const up = new Vector3(0, 1, 0);
let angle = direction.angleTo(up);
const cross = direction.cross(up);
if (cross.z < 0) {
	angle *= -1;
}

From a quick search in my current (4 week) three.js project I ran across this 7 times.
In comparison, this project used angleTo only 10 times. So the majority of those were followed by steps to make it unsigned.
Doing it with 3d vectors needs an extra measure axis though, in this case the up vector.

In Renda I have these four functions:
Vec2.angleTo()
https://github.com/rendajs/Renda/blob/16eb03603466b27ecfebef30c619c2f752a22582/src/math/Vec2.js#L400-L405
Vec2.clockwiseAngleTo()
https://github.com/rendajs/Renda/blob/16eb03603466b27ecfebef30c619c2f752a22582/src/math/Vec2.js#L362-L367
Vec3.angleTo()
https://github.com/rendajs/Renda/blob/16eb03603466b27ecfebef30c619c2f752a22582/src/math/Vec3.js#L467-L472
Vec3.clockwiseAngleTo()
https://github.com/rendajs/Renda/blob/16eb03603466b27ecfebef30c619c2f752a22582/src/math/Vec3.js#L513-L521

I personally think all of these could be useful, but ultimately it's a trade-off of how much you want to include.

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 15, 2023

Considering #22209 (comment) and your implementations, Vector2.angleTo() should look like so: 7e32385. This version was also suggested by @WestLangley in #22209 (comment).

@mrdoob If you are fine with this commit, I'll restore the PR based on that version.

@WestLangley
Copy link
Collaborator

hmmm...

  • I expect if we add an "unsigned" Vector2 version, someone will then want a "signed" Vector2 version.

  • As per the docs, Vector3.angleTo() computes the angle "between" the two vectors. Maybe it should be named Vector3.angleBetween()?

There are many options for implementing this. Very little demand.

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 15, 2023

I expect if we add an "unsigned" Vector2 version, someone will then want a "signed" Vector2 version.

Yes, this will only be a matter of time. But still I feel it's okay to add Vector2.angleTo(). Just a personal preference and I'm not insisting on it 😇 .

As per the docs, Vector3.angleTo() computes the angle "between" the two vectors. Maybe it should be named Vector3.angleBetween()?

Lately, I frequently get complains that three.js changes its API too often and these complains are to a certain degree justified. We should be a bit more strict in that regard and only renaming things when it's absolutely necessary. TBH, I don't see this need in context if Vector3.angleTo().

@mrdoob
Copy link
Owner

mrdoob commented Mar 16, 2023

@Mugen87

@mrdoob If you are fine with this commit, I'll restore the PR based on that version.

Sounds good!

@mrdoob
Copy link
Owner

mrdoob commented Mar 16, 2023

@WestLangley

  • I expect if we add an "unsigned" Vector2 version, someone will then want a "signed" Vector2 version.

Let's cross that bridge when we come to it 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants