Angle of the attacker

Maxodors

New member
Joined
Mar 18, 2022
Messages
2
I would really appreciate if someone could help me with the formula to find an angle of attacker (this is for a game). Every unit has x, y coordinates and facing angle. Facing angle is where the unit is looking. Left is 180, Up is 90, Right is 0/360 and Down is 270. As an output of the formula, I need an angle between 0 and 360 from where the unit got attacked. Please, I need as simplified solution as possible (low on difficult computations).
 
I would really appreciate if someone could help me with the formula to find an angle of attacker (this is for a game). Every unit has x, y coordinates and facing angle. Facing angle is where the unit is looking. Left is 180, Up is 90, Right is 0/360 and Down is 270. As an output of the formula, I need an angle between 0 and 360 from where the unit got attacked. Please, I need as simplified solution as possible (low on difficult computations).
Please share your work for this problem!
 
I would really appreciate if someone could help me with the formula to find an angle of attacker (this is for a game). Every unit has x, y coordinates and facing angle. Facing angle is where the unit is looking. Left is 180, Up is 90, Right is 0/360 and Down is 270. As an output of the formula, I need an angle between 0 and 360 from where the unit got attacked. Please, I need as simplified solution as possible (low on difficult computations).
Hi Maxodors,

Welcome to freeMATHhelp.

When you say: "low on difficult computations" do you mean that you are not very 'mathematically gifted'?

At first glance (if I understand what you are attempting to do correctly), what you are asking for doesn't appear to be a particularly "difficult
computation".

What do you know about converting Cartesian (x, y) Coordinates into Polar Coordinates? That's a fairly simple process and I suspect it may provide all the answers you need.

Just Google "Polar Coordinates", have a read up on them and come back to show us what, if any, progress you have made. We will offer further guidance, if necessary, once we have seen some input from you. We don't just offer worked solutions here but are happy to help those who are willing to help themselves. :):thumbup:

PS: When I Googled it, the very first result I got (
here) provided everything you should need to know in a format that should make it easy for you to use but feel free to check out the other sites thrown up by the search.
Let us know how you get on, huh?
 
By low on computation, I mean that the solution should have the least complex functions possible (this is because it takes more processing power for the game).

Here is my attempt. I am not sure if it is correct and if it is the simplest way to do it.

W = angle of the attack from the perspective of the receiver
F = tan-1((y2-y1)/(x2-x1))

If x2>x1 AND y2>y1 then
W = F
Endif

If x2>x1 AND y2 < y1 then
W = 360 - F
Endif

If x2<x1 AND y2 > Y1 then
W= 180 - F
Endif

If x2 < x1 AND y2< y1 then
W = 180 + F
Endif
 
By low on computation, I mean that the solution should have the least complex functions possible (this is because it takes more processing power for the game).

Here is my attempt. I am not sure if it is correct and if it is the simplest way to do it.

W = angle of the attack from the perspective of the receiver
F = tan-1((y2-y1)/(x2-x1))

If x2>x1 AND y2>y1 then
W = F
Endif

If x2>x1 AND y2 < y1 then
W = 360 - F
Endif

If x2<x1 AND y2 > Y1 then
W= 180 - F
Endif

If x2 < x1 AND y2< y1 then
W = 180 + F
Endif
That's very good, and seems correct, well done!

I have two suggestions for possible improvement:-
- Many computer languages have a function called "atan2(ydiff, xdiff)" which will directly return the number that you need (although it will probably be in radians, but this can obviously be turned into degrees with a single multiplication )
- Some languages (c, c++) have different functions depending on the floating point accuracy. Instead of "double" you could try switching to "float" and using atan2f, sinf, cosf, etc instead of atan2, sin, cos, ...

But this seems unlikely to be a speed bottleneck on a modern computer unless you have many thousands of attackers. I recommend that you double check which part of your code is actually taking a long time to execute.
 
By low on computation, I mean that the solution should have the least complex functions possible (this is because it takes more processing power for the game).

Here is my attempt. I am not sure if it is correct and if it is the simplest way to do it.

W = angle of the attack from the perspective of the receiver
F = tan-1((y2-y1)/(x2-x1))
Good to see you've made an effort.

However, I'm concerned there may be a slight flaw in your approach.

If the "attack" comes from directly above (or below) ie: the angle is 90°/270° then you may be attempting division by zero and thence crash your device.

My initial thoughts on the matter were based on the "receiver" being situated at the Origin (since you didn't give us much to go on) and hence my suggestion you look into Polar Coordinates.

My recommendation would then have been:-

IF x=0 AND y=0 THEN "Attacker" is directly in front of you.

IF x=0 THEN IF y>0 THEN θ=90° ELSE θ=270°
IF x>0 THEN IF y≥0 THEN θ=[tan-1 (
\(\displaystyle \frac {y}{x}\))]° ELSE θ=[360+tan-1 (\(\displaystyle \frac {y}{x}\))]°

IF x<0 THEN θ=[180+tan-1 (\(\displaystyle \frac {y}{x}\))]°

As I say, this is based on the "receiver" being at the Origin and the x & y values are the coordinates of the "attackers".
You appear to have the capabilities to modify that to your particular needs if desired (if "receiver" is not at the Origin) but
Cubist raises a very good point (at Post #5) about the 'atan2' function that may be available in your programming language; that may well be worth exploring further.
 
Last edited:
By low on computation, I mean that the solution should have the least complex functions possible (this is because it takes more processing power for the game).

Here is my attempt. I am not sure if it is correct and if it is the simplest way to do it.
Hi again Maxodors,

Interested to know what you finally went for.

Can you let us know, please? (For the sake of 'completeness' :))

PS: Having read through the thread again, it occurred to me that a simple, quick calculation might be added at the beginning of the code I suggested to account for the 'receiver' NOT being at the Origin. Just add this as the first line:-

LET x = (xa - xr); LET y = ( ya - yr)
where (xr, yr) are the coordinates of the 'receiver' and (xa, ya) those of the 'attacker'.

Let us know what you plumped for, eh?
 
Top