You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[p5.js 2.0+ Bug Report]: Vector angleBetween() doesn't work for vectors with more than three dimensions #8926
Use angleBetween to compute the angle between them
Note that it is incorrect (although no errors were reported)
Snippet:
functionsetup(){letv1=createVector(1,0,0,0);letv2=createVector(0,0,0,1);lettheta=v1.angleBetween(v2);// The correct angle between v1 and v2 is PI/2, but theta is 0print(theta);}
Discussion
The angleBetween() method currently uses cross() to compute the angle. This was fine in p5.js version 1 since vectors were always 3D. But version 2 vectors can have any number of dimensions, so angleBetween() needs to use a more general method. I recommend using the dot product divided by the product of the vector lengths:
Hi everyone, this is reserved for the CodeDay microinternship. If it is not completed by them, then it can be opened to volunteers. But for now, please do not make PRs or volunteer - thanks for understanding!
The solution should generalize angleBetween() to N dimensions:
add unit tests checking behavior for beyond 3d dimensionalities, as well as for operations between vectors of mismatched dimensions
when performing binary operation on vectors of different dimensions, use smallest vector priority: check the implementation of add, sub, div, mult, rem and their behavior on mismatched sized vectors
The documentation states "The angles returned are signed, which means that v1.angleBetween(v2) === -v2.angleBetween(v1)." The general dot product method will not do this.
I think a signed angle only makes sense for 2D vectors: it is positive if the second vector is positioned clockwise from the first. There are probably p5 sketches that depend on this, so this 2D behavior needs to be preserved.
The case for 3D is less clear. A common practice is to take the sign relative to the normal of the plane formed by the two vectors. But angleBetween() doesn't currently work this way. For example, the following code will print positive PI/2 for both directions:
I don't think a signed angle makes any sense for more than three dimensions.
My thought is to return a signed angle for 2D vectors and an unsigned angle for any other dimension. But please discuss! And be sure to update the documentation to match whatever is implemented.
I'll reproduce the behavior using vectors with more than three dimensions, inspect the angleBetween() implementation, and add regression tests to identify where the calculation fails. I'll check the expected behavior for different vector dimensions and make sure the fix preserves existing 2D and 3D behavior.
Could you please confirm whether this issue is available and assign it to me if appropriate?
Activity
ksen0 commentedon Jun 17, 2026
Hi everyone, this is reserved for the CodeDay microinternship. If it is not completed by them, then it can be opened to volunteers. But for now, please do not make PRs or volunteer - thanks for understanding!
The solution should generalize
angleBetween()to N dimensions:sidwellr commentedon Jun 23, 2026
The documentation states "The angles returned are signed, which means that
v1.angleBetween(v2) === -v2.angleBetween(v1)." The general dot product method will not do this.I think a signed angle only makes sense for 2D vectors: it is positive if the second vector is positioned clockwise from the first. There are probably p5 sketches that depend on this, so this 2D behavior needs to be preserved.
The case for 3D is less clear. A common practice is to take the sign relative to the normal of the plane formed by the two vectors. But
angleBetween()doesn't currently work this way. For example, the following code will print positive PI/2 for both directions:I don't think a signed angle makes any sense for more than three dimensions.
My thought is to return a signed angle for 2D vectors and an unsigned angle for any other dimension. But please discuss! And be sure to update the documentation to match whatever is implemented.
VEERAJ-mark27 commentedon Oct 22, 2026
Hi, I'd like to contribute to this issue.
I'll reproduce the behavior using vectors with more than three dimensions, inspect the
angleBetween()implementation, and add regression tests to identify where the calculation fails. I'll check the expected behavior for different vector dimensions and make sure the fix preserves existing 2D and 3D behavior.Could you please confirm whether this issue is available and assign it to me if appropriate?