[Snippet] UnitOrientation

Scripts

13 years
edited 8 years
This solves the ever-so-annoying problem of unit orientation--determining how one unit is facing, or oriented, in relation to another. This snippet provides the functionality to:
- Determine if a unit is behind a unit
- Determine if a unit is facing the behind of a unit
- Determine if a unit is facing a unit

All with an angle margin (margin of error, sort of) to determine what is considered "true" or "false" for those functions. View the image and the documentation to get a better understanding of how the angle margin works. Here is the code:
Code (jass) Select
1
library UnitOrientation /* v.1.0.0.0
2
********************************************************************
3
*
4
*   This snippet will determine one unit's orientation towards 
5
*   another unit. This is useful for three purposes:
6
*       - Determine whether a unit is facing a unit
7
*       - Determine whether a unit is facing the back of a unit
8
*       - Determine whether a unit is behind a unit
9
*
10
*   There are angle margins to determine how flexible the function
11
*   is. Higher angle margins will result in more angles returning
12
*   as true for that function. The angle margin is that angle left
13
*   of the unit and that angle right of the unit, creating a conal
14
*   angle of 2 * angleMargin. 
15
*
16
********************************************************************
17
*   
18
*   Functions
19
*   
20
*       function IsUnitBehindUnit takes unit behind, unit inFront, real angleMargin returns boolean
21
*           
22
*           - Checks if the unit "behind" is behind the unit "inFront". 
23
*           - The angle margin is the conal angle that is considered
24
*             to be "behind" a unit. The input should be DEGREES.
25
*
26
*       function IsUnitFacingUnit takes unit theFacer, unit toFace, real angleMargin returns boolean
27
*
28
*           - Determines whether the unit "theFacer" is facing "toFace". 
29
*           - The angle margin is the same as in "IsUnitBehindUnit".
30
*
31
*       function IsUnitFacingUnitBehind takes unit behind, unit inFront, real angleMargin returns boolean
32
*       
33
*           - Determines whether the unit "behind" is behind the unit "inFront"
34
*             and checks if the unit "behind" is facing the back of "inFront". 
35
*             A great example would be a backstab spell, where you are required
36
*             to be behind the target and facing the unit. 
37
*           - The angle margin is the same as in "IsUnitBehindUnit".
38
*  
39
*******************************************************************/
40
    
41
    function IsUnitBehindUnit takes unit behind, unit inFront, real angleMargin returns boolean
42
        local real angle = ModuloReal(Atan2(GetUnitY(behind) - GetUnitY(inFront), GetUnitX(behind) - GetUnitX(inFront)) * bj_RADTODEG, 360)
43
        local real difference = GetUnitFacing(inFront) - angle
44
        if difference < 0 then
45
            set difference = -difference
46
        endif
47
        return difference > (180 - angleMargin) and difference < (180 + angleMargin)
48
    endfunction
49
    
50
    function IsUnitFacingUnitBehind takes unit behind, unit inFront, real angleMargin returns boolean
51
        local real difference = GetUnitFacing(inFront) - GetUnitFacing(behind)
52
        if difference < 0 then
53
            set difference = -difference
54
        endif
55
        return difference < angleMargin and IsUnitBehindUnit(inFront, behind, angleMargin)
56
    endfunction
57
    
58
    function IsUnitFacingUnit takes unit theFacer, unit toFace, real angleMargin returns boolean
59
        local real angle = ModuloReal(Atan2(GetUnitY(theFacer) - GetUnitY(toFace), GetUnitX(theFacer) - GetUnitX(toFace)) * bj_RADTODEG, 360)
60
        local real difference = GetUnitFacing(theFacer) - angle
61
        if difference < 0 then
62
            set difference = -difference
63
        endif
64
        return difference > (180 - angleMargin) and difference < (180 + angleMargin)
65
    endfunction
66
67
endlibrary




Let me know if there are any functions you want me to add. If you still don't understand the purpose of this, think about the conditions for a backstab spell. You have to be (A) behind the target, (B) facing the target. This snippet will allow you to check those easily.
13 years
I like this.

Approved.
13 years
Liked and rated :D Good job!!!