Snippet IsUnitLeft

Flare

Stops copies me!
Reaction score
662
The first (somewhat) useful thing I have created in JASS :D Basically, this determines whether a unit is left or right of a given line by returning a boolean (i.e. true or false, if you don't know what a boolean is).

Requires NewGen

To import
Code:
Create a trigger called IsUnitLeft
Open the Edit menu -> Click Convert to Custom Text
Delete everything within the converted trigger
Copy the function's script from either a) the script shown in the post or b) the script within World Editor (shouldn't make a difference which way you do it)
Use the function for something ^^

(all of the other functions within the library are just used as part of the IsUnitLeft function)
JASS:
library IsUnitLeft

 
//Gets the slope of a line, and returns the angle of the line, in radians.

    function Slope takes real x1, real y1, real x2, real y2 returns real
        local real angle
        set angle = Atan2 (y2-y1, x2-x1)
        return angle
    endfunction
    
    function MidpointX takes real x1, real x2 returns real
        return (x1+x2)/2
    endfunction
    
    function MidpointY takes real y1, real y2 returns real
        return (y1+y2)/2
    endfunction

    function ExtendPerpLeft takes real x1, real y1, real x2, real y2 returns location
        local real sx = MidpointX (x1, x2)
        local real sy = MidpointY (y1, y2)
        local real angle = Slope (x1, y1, x2, y2)
        local real perpangle = angle + Deg2Rad (90)
        local real ex = sx + Cos (perpangle)
        local real ey = sy + Sin (perpangle)
        return Location (ex, ey)
    endfunction

//Determines whether a unit is left or right of a line.
//Direction of the line (from point A to point B, rather than point B to point A for example) does count.
//As the left/right side of the line would be reversed if the start and end points were reversed. 

    function IsUnitLeft takes location p1, location p2, unit u returns boolean

//This part determines the slope of the line, and extends it perpendicularly to the left at the midpoint.
//This gives us a point to determine whether a location on the left of the line is greater/less than the the equation of the line
//Once that has been determined, the function moves on to check the unit's position.

        local real x1 = GetLocationX (p1)
        local real y1 = GetLocationY (p1)
        local real x2 = GetLocationX (p2)
        local real y2 = GetLocationY (p2)
        local location perppt = ExtendPerpLeft (x1, y1, x2, y2)
        local real ux = GetUnitX (u)
        local real uy = GetUnitY (u)
        local real px = GetLocationX (perppt)
        local real py = GetLocationY (perppt)
        local real m = (y2-y1)/(x2-x1)
        local real comparisonval = (-m*x1) + y1
        local boolean greaterthan

//This part determines whether the coordinates of a line, when substituted into the equation are greater or less than the known value (comparisonval)

        if ((-m*px) + py) >= comparisonval then
            set greaterthan = true
            else
            set greaterthan = false
        endif

//This substitutes the unit's XY coordinates of the taken unit, and checks if it gives the same result as the perpendicular point
//If it gives the same result as the perpendicular point, the unit is on the left.
//Else the unit is on the right.

        call RemoveLocation (perppt)
        set perppt = null

        if (greaterthan == true and ((-m*ux) + uy) >= comparisonval) or (greaterthan == false and ((-m*ux) + uy) <= comparisonval) then
            return true
            else
            return false
        endif

    endfunction
endlibrary


Simply put, just assign a boolean variable like so
JASS:
set booleanvar = IsUnitLeft (point1, point2, myunit)


For GUI users with little JASS experience:
IMO, if you use GUI and have very little experience but need to use this function for anything, you need 4 variables (1 boolean, 2 points or a point array, and a unit). Then, just add a custom script like this:
Code:
Custom script:   set udg_boolean = IsUnitLeft (udg_StartPt, udg_EndPt, udg_unitvar)

Then just do a boolean comparison to check whether the boolean is true or false like in this trigger
(CentrePt was set at Map Init, it's just the Centre of Playable Map Area and the lightning effects are just used to indicate the line used)
Code:
Test
    Events
        Player - Player 1 (Red) skips a cinematic sequence
    Conditions
    Actions
        Set real = (Random angle)
        Set EndPt = (CentrePt offset by 700.00 towards real degrees)
        Set StartPt = (CentrePt offset by 700.00 towards (real + 180.00) degrees)
        Set PerpPt = (CentrePt offset by 500.00 towards (real + 90.00) degrees)
        Floating Text - Change the position of floatingtext[1] to StartPt with Z offset 0.00
        Floating Text - Change the position of floatingtext[2] to EndPt with Z offset 0.00
        Floating Text - Change the position of floatingtext[3] to PerpPt with Z offset 0.00
        Lightning - Move lightning[1] to source CentrePt and target EndPt
        Lightning - Move lightning[2] to source CentrePt and target StartPt
        Lightning - Move lightning[3] to source CentrePt and target PerpPt
        Custom script:   set udg_boolean = IsUnitLeft (udg_StartPt, udg_EndPt, udg_unitvar)
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                boolean Equal to True
            Then - Actions
                Game - Display to (All players) the text: Unit is left of the...
            Else - Actions
                Game - Display to (All players) the text: Unit is right of th...
        Custom script:   call RemoveLocation (udg_StartPt)
        Custom script:   call RemoveLocation (udg_EndPt)
        Custom script:   call RemoveLocation (udg_PerpPt)

Hopefully, people will find this uesful :)

Info on test map: Press ESC to generate a new line. Blue lightning effect determines the line, red lightning indicates which side is the left (if the line is going right -> left for example which can be a pain to figure out mentally :p)

If you find any leaks, or any bugs (does it return an incorrect value?), let me know :D
 

Attachments

  • IsUnitLeft sample.w3x
    19.1 KB · Views: 239

Trollvottel

never aging title
Reaction score
262
if its a straight line from left to right. what'd be left and what right?(up and down?)

and, most jass users dont use points, so could you make another function with just coordinates?
 

Flare

Stops copies me!
Reaction score
662
oh sure. now i see... ^^ sry for the stupid question

np, I've asked alot of stupid questions myself.

Also, does anyone think there is a need for an IsUnitRight function? Just out of curiosity (can't see why it would be needed, unless someone feels it's absolutely necessary for some odd reason when they can just check for a return false :p)
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top