maxkueng / victor

A JavaScript 2D vector class with methods for common vector operations

Home Page:http://victorjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

".rotate(angle)" and ".rotateBy(rotation)", their feature have been swapped!

724Cheers opened this issue · comments

//theoretically, console should print (0, 100)
var vec = new Victor(0, 100);
vec.rotate(Math.PI / 2);
console.log(vec);
//(-100, 0)
//theoretically, console should print (-100, 0)
var vec = new Victor(0, 100);
vec.rotateBy(Math.PI / 2);
console.log(vec);
//(0, -100), rotate by this.angle() + PI / 2

solution:

Victor.prototype.rotateBy = function (rotation) {
    var nx = (this.x * Math.cos(rotation)) - (this.y * Math.sin(rotation));
    var ny = (this.x * Math.sin(rotation)) + (this.y * Math.cos(rotation));

    this.x = nx;
    this.y = ny;

    return this;
};
Victor.prototype.rotate = function (angle) {
    var rotation = angle - this.angle();

    return this.rotateBy(rotation);
};

I confirm the erratic behaviour of native rotate and rotateBy.

I confirm the fixes of 724Cheers work, thank you 724Cheers.

rotateTo also needs a fix.

@maxkueng If I made a PR, could it be merged? Seems like there hasn't been a lot of activity lately with this repo...