Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[Snippet] UnitOrientation No New Posts Codes & Snippets

Started by
Purgeandfire

0 Members and 1 Guest are viewing this topic.

[Snippet] UnitOrientation
on: May 02, 2013, 03:00:14 AM
Category: Geometry, Units
Language: vJASS

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
  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.     function IsUnitBehindUnit takes unit behind, unit inFront, real angleMargin returns boolean
  41.         local real angle = ModuloReal(Atan2(GetUnitY(behind) - GetUnitY(inFront), GetUnitX(behind) - GetUnitX(inFront)) * bj_RADTODEG, 360)
  42.         local real difference = GetUnitFacing(inFront) - angle
  43.         if difference < 0 then
  44.             set difference = -difference
  45.         endif
  46.         return difference > (180 - angleMargin) and difference < (180 + angleMargin)
  47.     endfunction
  48.    
  49.     function IsUnitFacingUnitBehind takes unit behind, unit inFront, real angleMargin returns boolean
  50.         local real difference = GetUnitFacing(inFront) - GetUnitFacing(behind)
  51.         if difference < 0 then
  52.             set difference = -difference
  53.         endif
  54.         return difference < angleMargin and IsUnitBehindUnit(inFront, behind, angleMargin)
  55.     endfunction
  56.    
  57.     function IsUnitFacingUnit takes unit theFacer, unit toFace, real angleMargin returns boolean
  58.         local real angle = ModuloReal(Atan2(GetUnitY(theFacer) - GetUnitY(toFace), GetUnitX(theFacer) - GetUnitX(toFace)) * bj_RADTODEG, 360)
  59.         local real difference = GetUnitFacing(theFacer) - angle
  60.         if difference < 0 then
  61.             set difference = -difference
  62.         endif
  63.         return difference > (180 - angleMargin) and difference < (180 + angleMargin)
  64.     endfunction
  65.  
  66. 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.
« Last Edit: December 19, 2017, 07:51:30 PM by moyack »



UnitOrientation
Reply #1 on: May 14, 2013, 11:51:51 AM

I like this.

Approved.



UnitOrientation
Reply #2 on: May 14, 2013, 11:58:12 AM

Liked and rated :D Good job!!!


 

Started by PitzerMike

Replies: 0
Views: 1638
Codes & Snippets

Started by Purgeandfire

Replies: 0
Views: 1714
Codes & Snippets

Started by Bribe

Replies: 0
Views: 1968
Codes & Snippets

Started by moyack

Replies: 0
Views: 9880
Codes & Snippets

Started by Magtheridon96

Replies: 1
Views: 8662
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...