Calculating degrees between 2 points with inverse Y axis

前端 未结 4 1235
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 17:45

I\'m creating a simple 2D game in javascript/canvas. I need to figure out the angle of a certain object relative to my position.

So: say I\'m at (10,10) and the object i

4条回答
  •  [愿得一人]
    2021-01-31 18:32

    In HTML5 canvas the origin is the top left corner hence the y axis grows from top to bottom. So regardless of wherever you are on the unit circle to calculate the angle of point A to the center(C) you should in fact do like

    angle = Math.atan2(Cy-Ay,Ax-Cx)

    and you will get your result in the range of [-π, π].

    I have no idea why they haven't made the canvas origin the bottom left corner.

提交回复
热议问题