Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [Snippet] UnitOrientation  (Read 9954 times)

  • Rookie - level 1
  • *
  • Posts: 45
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 13
    • View Profile
[Snippet] UnitOrientation
« on: May 02, 2013, 03:00:14 AM »
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.
« Last Edit: May 31, 2013, 12:52:10 AM by Purgeandfire »

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 84
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
UnitOrientation
« Reply #1 on: May 14, 2013, 11:51:51 AM »
I like this.

Approved.

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 977
  • WC3 Models: 59
  • WC3 Tutorials: 13
  • WC3 Tools: 10
  • WC3 Maps: 12
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 6
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
UnitOrientation
« Reply #2 on: May 14, 2013, 11:58:12 AM »
Liked and rated :D Good job!!!

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

 

[Snippet] Group Units in line, with distance and optional spread-value

Started by rvonsonsnadtzBoard Codes & Snippets

Replies: 5
Views: 12107
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 21803
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 27672
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 9073
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7870
Last post January 09, 2012, 07:03:32 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!