Rotate a vector by given degrees (errors when value over 90)

Posted by Ivan on Game Development See other posts from Game Development or by Ivan
Published on 2012-09-08T12:47:39Z Indexed on 2012/09/08 15:50 UTC
Read the original article Hit count: 319

Filed under:
|
|

I created a function to rotate a vector by a given number of degrees. It seems to work fine when given values in the range -90 to +90. Beyond this, the amount of rotation decreases, i.e., I think objects are rotating the same amount for 80 and 100 degrees.

I think this diagram might be a clue to my problem, but I don't quite understand what it's showing. Must I use a different trig function depending on the radians value? The programming examples I've been able to find look similar to mine (not varying the trig functions).

Vector2D.prototype.rotate = function(angleDegrees) {  
  var radians = angleDegrees * (Math.PI / 180);

  var ca = Math.cos(radians);
  var sa = Math.sin(radians);

  var rx = this.x*ca - this.y*sa;
  var ry = this.x*sa + this.y*ca;

  this.x = rx;
  this.y = ry;  
};

© Game Development or respective owner

Related posts about math

Related posts about JavaScript