Snippet SetDistanceToUnit

Discussion in 'Systems and Snippets' started by Jesus4Lyf, Aug 7, 2009.

  1. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    SetDistanceToUnit​

    I think this is surprisingly useful.

    The "from" unit is the center of reference for setting the position of the "to" unit. The "dist" real is the distance to set between the units. So if you wanted a spell where a unit walks along pushing all units out of the way, you would, for each unit in "dist" range, call SetDistanceToUnit(pusher, pickedUnit, dist). :D
    JASS:
    function SetDistanceToUnit takes unit from, unit to, real dist returns nothing
        local real x=GetUnitX(to)-GetUnitX(from)
        local real y=GetUnitY(to)-GetUnitY(from)
        local real factor=dist/SquareRoot(x*x+y*y)
        call SetUnitX(to,GetUnitX(from)+x*factor)
        call SetUnitY(to,GetUnitY(from)+y*factor)
    endfunction

    Alternatively:
    JASS:
    function SetDistanceToXY takes real x, real y, unit to, real dist returns nothing
        local real tx=GetUnitX(to)-x
        local real ty=GetUnitY(to)-y
        local real factor=dist/SquareRoot(tx*tx+ty*ty)
        call SetUnitX(to,x+tx*factor)
        call SetUnitY(to,y+ty*factor)
    endfunction

    Hopefully this inspires some creativity.
     
  2. Renendaru

    Renendaru (Evol)ution is nothing without love.

    Ratings:
    +309 / 0 / -0
    Addz a demo.
     
  3. Tyrande_ma3x

    Tyrande_ma3x .

    Ratings:
    +91 / 0 / -0
    What about the angle?
     
  4. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    >Addz a demo.
    Bah. S'pose I will soon.

    >What about the angle?
    What angle? Who needs angles? ;)
     
  5. Tyrande_ma3x

    Tyrande_ma3x .

    Ratings:
    +91 / 0 / -0
    > What angle? Who needs angles?
    Well, when it's setting a unit to be in some distance from another, what would be the "target"`s angle? Would it just get moved north or south or at 211 degrees or some other value around the "caster"?
     
  6. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    In the correct direction. IE if it was at an angle of 70 degrees, it will remain at an angle of 70 degrees except further away.

    I did it with vectors, that's all. More efficient than angles. :)
     
  7. Renendaru

    Renendaru (Evol)ution is nothing without love.

    Ratings:
    +309 / 0 / -0
    Played around with it, made some spells that were fun to play with. Linked Soul and Magnet Shield. Linked Soul forced units to retain a certain distance or snap together being stunned. Magnet shield constantly pushes units too close and damages.
     
  8. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    Yay! :D

    Good stuff, Ren. :thup:
     
  9. Troll-Brain

    Troll-Brain You can change this now in User CP.

    Ratings:
    +85 / 0 / -0
    Are you sure about that, coz using Cos, Sin , and so one in jass2 seems as fast as read an array.
     
  10. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    Meh, I suppose it is probably around the same.

    Haven't benchmarked.
     
  11. Dr.Jack

    Dr.Jack That's Cap'n to you!

    Ratings:
    +103 / 0 / -0
    Approved!
    Thanks for contributing!
     
  12. SanKakU

    SanKakU Member

    Ratings:
    +21 / 0 / -0
    i've started to use it. but i had to add the following function:
    JASS:
    function getdis takes unit x, unit y returns real
    local real dx = GetUnitX(x) - GetUnitX(y)
    local real dy = GetUnitY(x) - GetUnitY(y)
    return SquareRoot(dx * dx + dy * dy)
    endfunction

    this was for the original function. i haven't written a complementary function for the XY function because i haven't used it yet.
    it would probably go something like this:
    JASS:
    function getdisr takes real x, real y, unit z returns real
    local real dx = x - GetUnitX(z)
    local real dy = y - GetUnitY(z)
    return SquareRoot(dx * dx + dy * dy)
    endfunction
     

Share This Page