- Joined
- Mar 6, 2006
- Messages
- 9,243
I've seen this problem many times. People ask how they can get the difference between two angles.
I've not seen an easy solution. Maybe my search abilities just fail
Well, here's one.
It works for any angle, for example the difference between 350 and 10 returns 20, and difference between -170 and 170 returns 20.
You can easily modify it for different purposes, like detecting whether a unit is facing another, withing certain tolerance:
The key is this:
Below are examples how to use it.
If there are bugs, let me know. I'll be using this in a map of my own.
Note that angles > 180 are converted to -180...0 range, so the displayed text may lead you wrong, the difference should be correct.
I've not seen an easy solution. Maybe my search abilities just fail
It works for any angle, for example the difference between 350 and 10 returns 20, and difference between -170 and 170 returns 20.
You can easily modify it for different purposes, like detecting whether a unit is facing another, withing certain tolerance:

The key is this:
JASS:
Acos( Cos(r1) * Cos(r2) + Sin(r1) * Sin(r2) ) * bj_RADTODEG
Below are examples how to use it.
JASS:
library Attack initializer InitTrig_Attack_JASS
private function getAngleDifference takes real r1 , real r2 returns real
return Acos( Cos(r1) * Cos(r2) + Sin(r1) * Sin(r2) ) * bj_RADTODEG
endfunction
private function Action takes nothing returns nothing
local unit u = GetTriggerUnit()
local unit v = GetOrderTargetUnit()
local real r1 = GetUnitX(u)
local real r2 = GetUnitY(u)
local real r3 = GetUnitX(v)
local real r4 = GetUnitY(v)
// Angle from "caster" to target
local real r5 = Atan2( r4 - r2 , r3 - r1 )
local real r6 = GetUnitFacing(u) * bj_DEGTORAD
local real r7
set r7 = getAngleDifference( r5 , r6 )
call DisplayTextToPlayer( Player(0) , 0 , 0 , "(JASS) The angle difference between " + R2S(bj_RADTODEG * r5) + " and " + R2S(bj_RADTODEG * r6) + " is " + R2S(r7) + " degrees." )
set u = null
set v = null
endfunction
private function InitTrig_Attack_JASS takes nothing returns nothing
local trigger t1 = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( t1, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
call TriggerAddAction( t1, function Action )
set t1 = null
endfunction
endlibrary
-
Attack GUI
-
Events
-
Unit - A unit Is issued an order targeting an object
-
-
Conditions
-
Actions
-
-------- -------------------------------------------------- --------
-
-------- Set unit variables --------
-
-------- -------------------------------------------------- --------
-
Set u1 = (Triggering unit)
-
Set u2 = (Target unit of issued order)
-
-------- -------------------------------------------------- --------
-
-------- Set location variables --------
-
-------- -------------------------------------------------- --------
-
Set p1 = (Position of u1)
-
Set p2 = (Position of u2)
-
-------- -------------------------------------------------- --------
-
-------- Get angle from "caster" to target --------
-
-------- -------------------------------------------------- --------
-
Set r1 = (Angle from p1 to p2)
-
-------- -------------------------------------------------- --------
-
-------- Get caster facing --------
-
-------- -------------------------------------------------- --------
-
Set r2 = (Facing of u1)
-
-------- -------------------------------------------------- --------
-
-------- Get angle difference --------
-
-------- -------------------------------------------------- --------
-
Set r3 = (Acos((((Cos(r1)) x (Cos(r2))) + ((Sin(r1)) x (Sin(r2))))))
-
-------- -------------------------------------------------- --------
-
-------- Print the angle --------
-
-------- -------------------------------------------------- --------
-
Game - Display to Player Group - Player 1 (Red) for 10.00 seconds the text: (Difference between + ((String(r1)) + ( and + ((String(r2)) + ( is + ((String(r3)) + degrees.))))))
-
-------- -------------------------------------------------- --------
-
-------- Clear leaks --------
-
-------- -------------------------------------------------- --------
-
Custom script: call RemoveLocation(udg_p1)
-
Custom script: call RemoveLocation(udg_p2)
-
-------- -------------------------------------------------- --------
-
-
If there are bugs, let me know. I'll be using this in a map of my own.
Note that angles > 180 are converted to -180...0 range, so the displayed text may lead you wrong, the difference should be correct.
Attachments
Last edited: