javascript - Getting the angle from a direction vector? -
i have simple function set angle vector. gets vector's current magnitude (length), calulates angle , converts angle radians degrees. apply angle x , y, lastly multiplying vector it's original magnitude.
this.setangle = function(degree){ var l = this.length(); //magnitude of vector var angle = degree*math.pi/180; //degress converted radians this.x=math.cos(angle); this.y=math.sin(angle); this.multiply(l); //original magnitude return; }
however unsure how obtain (get) angle vector. below attempt:
this.getangle = function(){ var angle = math.atan(this.y/this.x); //radians var degrees = angle/(180*math.pi); //degrees return math.floor(degrees); //round number, avoid decimal fragments }
this attempt doesn't return value except 0 or -1.
any suggestions?
edit:
correct method:
this.getangle = function(){ var angle = math.atan2(this.y, this.x); var degrees = 180*angle/math.pi; return (360+math.round(degrees))%360; }
this.getangle = function(){ var angle = math.atan2(this.y, this.x); //radians // need devide pi, , multiply 180: var degrees = 180*angle/math.pi; //degrees return (360+math.round(degrees))%360; //round number, avoid decimal fragments }
Comments
Post a Comment