Angelo S.

asked • 01/03/20

I'm at X,Y - target at X,Y - How calculate angle to face target?

Hello there!

  1. I am at 10, 25 (x,y)
  2. target at 5, -10 (x,y)
  3. I'm facing 0º

How can I calculate the correct angle to face the target?

About this question: I am building a script on javascritp, so I guess I really need to use vanilla maths here,

if I were coding/scrip in some 3D software it would be easy as use something like "transform,x,y"...

EDIT: Got it working, thanks for the Tutors! Here's the code I'm using:

function getAngleDeg(ax,ay,px,py)
var angel = {
x: ax,
y: ay
var player = {
x: px,
y: py
var x = player.x - angel.x;
var y = player.y - angel.y;
var radianAngle = Math.atan2(x, y);
var northUpAngle = radianAngle * 128 / Math.PI;
if (northUpAngle < 0) northUpAngle += 256;
return northUpAngle.toFixed(0);

ps: Instead of 380º the game reads that as 256units (probably because needs to be sent as Unsigned Integers of bits) and needed te result to be 0-256 (wihtout negative numbers)

2 Answers By Expert Tutors


Adam M. answered • 01/03/20

PhD Mathematician with a Wealth of Teaching Experience

Angelo S.

Lovely! Altho I faced hard time finding what each symbol meaning. That's my entirely fault for being so bad with this subject! Really thanks :D


Patrick B. answered • 01/03/20

Math and computer tutor/teacher

Adam M.

I don't think this is what the OP was after. Also, the denominator in your calculation of slope should be swapped.


Angelo S.

I felt like I could understand this really easily! Atlho, when I do inverse tangent(-7) in javascript wich is I guess Math.atan(-7) the result is 1.4288992721907328 Is there something missing in the "x =| inverse tangent(-7)| = 81.87 degrees" ?


Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.