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
If your coordinates are (xMe, yMe) and their coordinates are (xThem, yThem), then you can use the formula:
arctan((yMe-yThem)/(xThem-xMe))
Normally it'd be arctan((yThem-yMe)/(xThem-xMe))
, but in this case the sign of the y-axis is reversed.
To convert the result from radians to degrees multiply by 180/pi.
So in JavaScript, this would look like: Math.atan((yThem-yMe)/(xThem-xMe))*180/Math.PI
atan gives a value between -pi/2 and pi/2 (that is, between -90 and 90 degrees). But you can look at what quadrant your (xThem - xMe, yMe - yThem) vector is in and adjust accordingly.