Detecting units within a cone

bananaHUNT

You can change this now in User CP
Reaction score
55
I want to pick all units within a cone-shaped area in front of the caster.
This is to make a simple slash spell: You activate an ability, unit shows attack animation and slashes a small area in front of him for x damage (preferably his attack damage):

Code:
  ________
  \ area /
   \    /
    \  /
     \/
    unit

Now what would be the easiest way to do that?
 

Faust

You can change this now in User CP.
Reaction score
123
Gui would be messy and not very effective...
Make a trigger in your map.
Name it CONE.
Convert it to custom text.
Delete everything in it.
Copy this into it:

JASS:
library Cone
// whichGroup: the group the units will be added to
// xc, yc: the starting point, sort of like the center of the circle
// facing: the direction the "middle" of the sector goes to, in radians
// radius: the "length" of the sector
// angle: the maximum opening angle of the sector, in radians
// filter: the condition function to apply to the units in the sector.
function GroupEnumUnitsInSector takes group whichGroup,real xc,real yc,real facing,real radius,real angle,boolexpr filter returns nothing
    local group g = CreateGroup()
    local unit u
    local real x
    local real y
    local real x1
    local real y1
    local real x2
    local real y2

    call GroupEnumUnitsInRange(g, xc, yc, radius, filter)

    set angle = angle * 0.5
    set x1 = Cos(facing - angle)
    set y1 = Sin(facing - angle)
    set x2 = Cos(facing + angle)
    set y2 = Sin(facing + angle)

    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)

        set x = GetUnitX(u) - xc
        set y = GetUnitY(u) - yc
        if y * x1 >= x * y1 and y * x2 <= x * y2 then
            call GroupAddUnit(whichGroup, u)
        endif
    endloop

    call DestroyGroup(g)
    set g = null
endfunction

// point: the starting point, sort of like the center of the circle
// facing: the direction the "middle" of the sector goes to, in degrees
// radius: the "length" of the sector
// angle: the maximum opening angle of the sector, in degrees
// filter: the condition function to apply to the units in the sector. Pass null for all units
function GetUnitsInSector takes location point,real facing,real radius,real angle,boolexpr filter returns group
    local group g = CreateGroup()
    if filter == null then
        set filter = FILTER_True
    endif
    call GroupEnumUnitsInSector(g, GetLocationX(point), GetLocationY(point), facing * bj_DEGTORAD, radius, angle * bj_DEGTORAD, filter)
    return g
endfunction

endlibrary


function GroupEnumUnitsInSector takes group whichGroup,real xc,real yc,real facing,real radius,real angle,boolexpr filter returns nothing

You have to use this line. I know it's JASS and bawwwwwww bawwwwwww, but it's not that difficult, and makes everything easier.
whichGroup: the group the units will be stored in
xc: X coordinates of the point of the cone
yc: Y coordinates of the same
facing: the angle of your casting unit
radius: the maximum distance
angle: the opening angle of the cone
filter: leave it as "null" (without brackets) to get all units in the cone.

Use it as this (example):

Trigger:
  • Custom Script: call GroupEnumUnitsInSector(G, GetUnitX(udg_caster), GetUnitY(udg_caster), udg_angle, udg_distance, udg_degree * bj_DEGTORAD, null)

Credits to AceHart
 

vypur85

Hibernate
Reaction score
803
Code:
Event
Condition
Actions
 Set Point1 = (Position of (Triggering unit))
 Set Real1 = Facing of (Triggering unit)
 Set Unitgp = Pick every unit within 600.00 range of Point1 matching....
 Unit group - Pick every unit in Unitgp and do multiple actions -
  Loop - Actions
    Set Point2 = (Position of (Picked unit))
    Set Real2 = Angle between [B]Point1[/B] and [B]Point2[/B]

    If - Condition
      Real2 Less than 0.00
    Then - Actions
      Set Real2 = Real2 + 360.00
    Else - Actions

    If - Condition
      Real2 Less than (Real1 + 120)
      Real2 Greater than (Real1 - 120)
    Then - Actions
      ----This (Picked unit) is within the cone region----
    Else - Actions

Something like the above. Untested though. But should be workable unless I missed something.

Edit:
My bad, mine is in fact for a 'sector' instead of a 'perfect triangle', as drawn by you in the first post.


> preferably his attack damage

Not possible.
 

Vicboy

Ultra Cool Member
Reaction score
44
I was doing something like this two days ago. >.<

If it were GUI, it would consist of using Angle between two points.

Check the unit's distance

Then

See if it's angle is infront of the unit or 20/-20 degrees away.
 

bananaHUNT

You can change this now in User CP
Reaction score
55
Thanks :D I used your GUI way vypur and it totally works !

Btw are there any leaks to remove? This will be the basic attack so its going to be used like hundreds of times (.2 cd). It was something with udg_RemoveGroup right?
 

Rainther

I guess I should write something of value here...
Reaction score
61
I think it is
call GroupClear(grp)
call DestroyGroup(grp)
set grp = null

to remove it completly, with "grp" being the variable. Call them in Custom Scripts.
 

vypur85

Hibernate
Reaction score
803
Code:
Event
Condition
Actions
 Set Point1 = (Position of (Triggering unit))
 Set Real1 = Facing of (Triggering unit)
 Set Unitgp = Pick every unit within 600.00 range of Point1 matching....
 Unit group - Pick every unit in Unitgp and do multiple actions -
  Loop - Actions
    Set Point2 = (Position of (Picked unit))
    Set Real2 = Angle between Point1 and Point2

    If - Condition
      Real2 Less than 0.00
    Then - Actions
      Set Real2 = Real2 + 360.00
    Else - Actions

    If - Condition
      Real2 Less than (Real1 + 120)
      Real2 Greater than (Real1 - 120)
    Then - Actions
      ----This (Picked unit) is within the cone region----
    Else - Actions
    Custom script:  call RemoveLocation (udg_Point2)
 Custom script:  call RemoveLocation (udg_Point1)
 Custom script:  call DestroyGroup (udg_Unitgp)

Leakless version. Notice which is inside/outside the loop.


call GroupClear(grp)
call DestroyGroup(grp)
set grp = null
Seems more like JASS to me. Though not sure if it needs null.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top