Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec2.rotate(degrees), Vec2.angle, Vec2.angleTo methods #5622

Merged
merged 9 commits into from
Sep 18, 2023
53 changes: 53 additions & 0 deletions src/core/math/vec2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { math } from './math.js';

/**
* A 2-dimensional vector.
*
Expand Down Expand Up @@ -407,6 +409,57 @@ class Vec2 {
return this;
}

/**
* Rotate a vector by Euler angle.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {number} degrees - The number to rotate by Euler angle.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Vec2} Self for chaining.
* @example
* const v = new pc.Vec2(0, 10);
*
* v.rotate(45); // rotates by 45 degrees
*
* // Outputs [7.071068.., 7.071068..]
* console.log("Vector after rotation is: " + v.toString());
*/
rotate(degrees) {
const angle = Math.atan2(this.x, this.y) + (degrees * math.DEG_TO_RAD);
const len = Math.sqrt(this.x * this.x + this.y * this.y);
this.x = Math.sin(angle) * len;
this.y = Math.cos(angle) * len;
return this;
}

/**
* Returns the Euler angle of the specified 2-dimensional vector.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns {number} The Euler angle of the specified 2-dimensional vector.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
* @example
* const v = new pc.Vec2(6, 0);
* const angle = v.angle();
* // Outputs 90..
* console.log("The angle of the vector is: " + angle);
*/
angle() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: should this be a function or a getter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it should be a function, to stay consistent with similar methods, such as: length()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good and fair point. But I'm wondering whether length, for example, should also be a getter these days. If we do decide to leverage getters in the math API, that's for another PR... 😄

return Math.atan2(this.x, this.y) * math.RAD_TO_DEG;
}

/**
* Returns the shortest Euler angle between two 2-dimensional vectors.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {Vec2} rhs - The 2-dimensional vector to calculate angle to.
* @returns {number} The Euler angle of the shortest angle between two 2-dimensional vectors.
Maksims marked this conversation as resolved.
Show resolved Hide resolved
* @example
* const a = new pc.Vec2(0, 10); // up
* const b = new pc.Vec2(1, -1); // down-right
* const angle = a.angleTo(b);
* // Outputs 135..
* console.log("The angle between vecotrs a and b: " + angle);
Maksims marked this conversation as resolved.
Show resolved Hide resolved
*/
angleTo(rhs) {
return Math.atan2(this.x * rhs.y + this.y * rhs.x, this.x * rhs.x + this.y * rhs.y) * math.RAD_TO_DEG;
}

/**
* Each element is set to the largest integer less than or equal to its value.
*
Expand Down