Skip to content

Commit

Permalink
Vec2.rotate(degrees), Vec2.angle, Vec2.angleTo methods (#5622)
Browse files Browse the repository at this point in the history
* add vec2.rotate and vec2.radians methods

* vec2 use degrees instead of radians

* prefer multiplication over division

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

* Update src/core/math/vec2.js

Co-authored-by: Will Eastcott <will@playcanvas.com>

---------

Co-authored-by: Will Eastcott <will@playcanvas.com>
  • Loading branch information
Maksims and willeastcott committed Sep 18, 2023
1 parent e1504d6 commit 13a2be2
Showing 1 changed file with 53 additions and 0 deletions.
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 an angle in degrees.
*
* @param {number} degrees - The number to degrees to rotate the vector by.
* @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 angle in degrees of the specified 2-dimensional vector.
*
* @returns {number} The angle in degrees of the specified 2-dimensional vector.
* @example
* const v = new pc.Vec2(6, 0);
* const angle = v.angle();
* // Outputs 90..
* console.log("The angle of the vector is: " + angle);
*/
angle() {
return Math.atan2(this.x, this.y) * math.RAD_TO_DEG;
}

/**
* Returns the shortest Euler angle between two 2-dimensional vectors.
*
* @param {Vec2} rhs - The 2-dimensional vector to calculate angle to.
* @returns {number} The shortest angle in degrees between two 2-dimensional vectors.
* @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 vectors a and b: " + angle);
*/
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

0 comments on commit 13a2be2

Please sign in to comment.