• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] If unit is facing unit condition.

Status
Not open for further replies.
Well given that IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin) returns a boolean, you can just put it inside some JASS if statements.
  • Custom script: if (IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin)) then
  • -------- do stuff --------
  • Custom script: endif
Alternatively, if you want to avoid using a lot of custom script, you can store it into a global boolean variable.
  • Custom script: set udg_SomeBoolean = IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin)
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • SomeBoolean Equal to True
    • Then - Actions
      • -------- unit is facing unit; do stuff--------
    • Else - Actions
      • -------- unit is not facing unit --------
 
Well given that IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin) returns a boolean, you can just put it inside some JASS if statements.
  • Custom script: if (IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin)) then
  • -------- do stuff --------
  • Custom script: endif
Alternatively, if you want to avoid using a lot of custom script, you can store it into a global boolean variable.
  • Custom script: set udg_SomeBoolean = IsUnitFacingUnit(DeezUnit, DatUnit, angleMargin)
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • SomeBoolean Equal to True
    • Then - Actions
      • -------- unit is facing unit; do stuff--------
    • Else - Actions
      • -------- unit is not facing unit --------

My trigger doesn't actually need a lot of custom scripts or an "Else" action because it only checks if DeezUnit is facing DatUnit. If DeezUnit is not facing DatUnit however,nothing will be triggered. So no "Else" action means less lines.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,289
Since a unit might never perfectly face another unit a tolerance is needed. One can use the properties of cosine for this as it is symmetric around 0 degrees and aliases so simulates +/- tolerances well.

Say one wants to test that unit A is facing unit B within +/- X degrees...
First get the angle from A to B. Then subtract the facing of A from it. This gives an alias of the delta angle that B is at with respect to the current facing of A. The condition uses this delta angle to test if it is within the +/- angle tolerance of X. This is done by putting the delta angle through cosine and then checking if the result is greater than or equal to the cosine of X. If this test passes then B is within the specified tolerance of X from the facing of A.

(cosine((angle from A to B) - (facing of A))) greater than or equal to (cosine(X))

The (cosine(X)) term can be pre-computed as a constant for speed, hence only 1 cosine and 1 arctangent (2 parameter geometric version) are needed. In normal computing cosine is reasonably expensive to compute so a modular angle test is normally faster. This is the opposite with JASS (what GUI compiles to and WC3 runs) where the expense of cosine is masked by the JASS virtual machine overhead and is significantly less than emulating modular real. Hence this is the fastest approach for JASS, but probably is not in real programming languages.
 
Last edited:
Status
Not open for further replies.
Top