• 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] Get units in sector (Cone)

Status
Not open for further replies.
Level 36
Joined
Jul 1, 2007
Messages
6,677
Hi, does anyone have/know how to make a script which works similarly to this idea?

Takes a point, radius, angle offset, and angle of sector

Returns a group containing all units within that sector, OR checks whether a given point/unit is within the sector and returns a boolean

Basically I'm trying to make a cone-shaped area of effect ability
 
Level 37
Joined
Mar 6, 2006
Messages
9,243

  • Cone Angle
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • -------- -------------------------------------------------- --------
      • -------- Set unit variables --------
      • -------- -------------------------------------------------- --------
      • Set u1 = Paladin 0003 <gen>
      • Set u2 = Blood Mage 0000 <gen>
      • -------- -------------------------------------------------- --------
      • -------- Set location variables --------
      • -------- -------------------------------------------------- --------
      • Set p1 = (Position of u1)
      • Set p2 = (Position of u2)
      • -------- -------------------------------------------------- --------
      • Set r1 = (Facing of u1)
      • -------- -------------------------------------------------- --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Acos((Cos((r1 - (Angle from p1 to p2)))))) Less than 45.00
        • Then - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: in
        • Else - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: out
      • -------- -------------------------------------------------- --------
      • -------- Clear leaks --------
      • -------- -------------------------------------------------- --------
      • Custom script: call RemoveLocation(udg_p1)
      • Custom script: call RemoveLocation(udg_p2)
      • Custom script: set udg_p1 = null
      • Custom script: set udg_p2 = null

The width of the cone is 90°, this checks 45° to left and right.

Just modify this. Pick all units in range, filter out unwanted ones. Inside the loop, set p2 = position of picked unit.
 
Level 36
Joined
Jul 1, 2007
Messages
6,677
So... something like this?

Code:
public function IsInSector takes unit a, unit b, real theta returns boolean
    local real xdiff = GetUnitX(b)-GetUnitX(a)
    local real ydiff = GetUnitY(b)-GetUnitY(a)
    
    if (Acos(Cos(GetUnitFacing(a) - Atan2(ydiff, xdiff))) <= theta) then
        return true
    endif
    return false
endfunction
 
Level 37
Joined
Mar 6, 2006
Messages
9,243
Oh, you're using vjass. In that case
JASS:
public function isInCone takes real x, real y, real angle, unit u returns boolean
    return Cos(angle-Atan2(GetUnitY(u)-y, GetUnitX(u)-x)) > 0.8
endfunction

The angle you compare to ranges from -1 to 1.

EDIT: You can use this if there's trouble finding a proper angle to use:
JASS:
return Acos(Cos(angle-Atan2(GetUnitY(u)-y, GetUnitX(u)-x))) < 45*bj_DEGTORAD
 
Last edited:
You should check this out, its a compilation of all useful simple geometrical checks:
http://www.hiveworkshop.com/forums/1838224-post336.html


Original thread: http://www.hiveworkshop.com/forums/graveyard-418/ispointinrectangle-180617/
JASS:
library IsPointInRectangle
    //determins if point P is in rectangle ABCD
    function IsPointInRectangle takes real ax, real ay, real bx, real by, real cx, real cy, real dx, real dy, real px, real py returns boolean
        local real cross0 = (py-ay)*(bx-ax)-(px-ax)*(by-ay)
        local real cross1 = (py-cy)*(ax-cx)-(px-cx)*(ay-cy)
        local real cross4 = (py-dy)*(ax-dx)-(px-dx)*(ay-dy)
        
        return ((cross0*cross1 >= 0) and (((py-by)*(cx-bx)-(px-bx)*(cy-by))*cross1 >= 0)) or ((cross0*cross4 >= 0) and (((py-by)*(dx-bx)-(px-bx)*(dy-by))*cross4 >= 0))
    endfunction
endlibrary


Original thread: http://www.hiveworkshop.com/forums/graveyard-418/snippet-pointintriangle-180407/
JASS:
library IsPointInTriangle

//check to see if a point P is in triangle ABC
function IsPointInTriangle takes real ax, real ay, real bx, real by, real cx, real cy, real px, real py returns boolean
    local real cross0 = (py-ay)*(bx-ax)-(px-ax)*(by-ay)
    local real cross1 = (py-cy)*(ax-cx)-(px-cx)*(ay-cy)
    return (cross0*cross1 >= 0) and (((py-by)*(cx-bx)-(px-bx)*(cy-by))*cross1 >= 0)
endfunction

endlibrary
 
Status
Not open for further replies.
Top