[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
library UnitOrientation /* v.1.0.0.0
********************************************************************
*
*   This snippet will determine one unit's orientation towards
*   another unit. This is useful for three purposes:
*       - Determine whether a unit is facing a unit
*       - Determine whether a unit is facing the back of a unit
*       - Determine whether a unit is behind a unit
*
*   There are angle margins to determine how flexible the function
*   is. Higher angle margins will result in more angles returning
*   as true for that function. The angle margin is that angle left
*   of the unit and that angle right of the unit, creating a conal
*   angle of 2 * angleMargin.
*
********************************************************************
*   
*   Functions
*   
*       function IsUnitBehindUnit takes unit behind, unit inFront, real angleMargin returns boolean
*           
*           - Checks if the unit "behind" is behind the unit "inFront".
*           - The angle margin is the conal angle that is considered
*             to be "behind" a unit. The input should be DEGREES.
*
*       function IsUnitFacingUnit takes unit theFacer, unit toFace, real angleMargin returns boolean
*
*           - Determines whether the unit "theFacer" is facing "toFace".
*           - The angle margin is the same as in "IsUnitBehindUnit".
*
*       function IsUnitFacingUnitBehind takes unit behind, unit inFront, real angleMargin returns boolean
*       
*           - Determines whether the unit "behind" is behind the unit "inFront"
*             and checks if the unit "behind" is facing the back of "inFront".
*             A great example would be a backstab spell, where you are required
*             to be behind the target and facing the unit.
*           - The angle margin is the same as in "IsUnitBehindUnit".

*******************************************************************/
   
    function IsUnitBehindUnit takes unit behind, unit inFront, real angleMargin returns boolean
        local real angle = ModuloReal(Atan2(GetUnitY(behind) - GetUnitY(inFront), GetUnitX(behind) - GetUnitX(inFront)) * bj_RADTODEG, 360)
        local real difference = GetUnitFacing(inFront) - angle
        if difference < 0 then
            set difference = -difference
        endif
        return difference > (180 - angleMargin) and difference < (180 + angleMargin)
    endfunction
   
    function IsUnitFacingUnitBehind takes unit behind, unit inFront, real angleMargin returns boolean
        local real difference = GetUnitFacing(inFront) - GetUnitFacing(behind)
        if difference < 0 then
            set difference = -difference
        endif
        return difference < angleMargin and IsUnitBehindUnit(inFront, behind, angleMargin)
    endfunction
   
    function IsUnitFacingUnit takes unit theFacer, unit toFace, real angleMargin returns boolean
        local real angle = ModuloReal(Atan2(GetUnitY(theFacer) - GetUnitY(toFace), GetUnitX(theFacer) - GetUnitX(toFace)) * bj_RADTODEG, 360)
        local real difference = GetUnitFacing(theFacer) - angle
        if difference < 0 then
            set difference = -difference
        endif
        return difference > (180 - angleMargin) and difference < (180 + angleMargin)
    endfunction

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!!!