javascript – How do i make a path on a canvas (HTML5) move a a certain angle-ThrowExceptions

Exception or error:

I have a path that is a triangle and i am trying to move it forward a certain length at a specific angle,

this.x += length*Math.sin(this.angle); 
this.y -= -length*Math.cos(this.angle);

length is the amount of distance forward and angle is the angle at which the path is facing. When i run it, and the angle is 90 degrees it moves on the x correctly but changes the y.

image

How to solve:

I figured it out! I was not converting the angle value to a radian, witch is what the sin and cos functions take, here is my new code

this.x+=length*Math.sin(parseInt(this.angle)*Math.PI/180);
this.y-=length*Math.cos(parseInt(this.angle)*Math.PI/180);

Leave a Reply

Your email address will not be published. Required fields are marked *