Snippet A few useful functions

The Undaddy

Creating with the power of rage
Reaction score
55
Well,not really something someone can't do himself/herself,but here are some functions I find very useful.

1.function AbsAngle takes real angle returns real

Sometimes,when calculating angles,you may get the angle of -138 or maybe 456,which sometimes messes up my calculations.And so this function returns the angle in range [0,360).The other thing I have noticed about angles is,that when you get a unit's facing angle (or was it angle between points?) you get the angle like [-180,180).So anyways,here it is:

JASS:
function AbsAngle takes real angle returns real
    loop
        exitwhen angle >= 0 and angle < 360
        if angle < 0 then
            set angle = angle + 360
        elseif angle >= 360 then
            set angle = angle - 360
        endif
    endloop
    return angle
endfunction


2.function AngleBetweenCoords takes real locAx,real locAy,real locBx,real locBy returns real

Basically blizzard's AngleBetweenPoints,but uses coordinates,as not to force you to create variables for some single thing to prevent leaks.

JASS:
function AngleBetweenCoords takes real locAx,real locAy,real locBx,real locBy returns real
    return bj_RADTODEG * Atan2(locBy - locAy, locBx - locAx)
endfunction


3.function DistanceBetweenCoords takes real locAx,real locAy,real locBx,real locBy returns real

Same as above ;)

JASS:
function DistanceBetweenCoords takes real locAx,real locAy,real locBx,real locBy returns real
    local real dx = locBx - locAx
    local real dy = locBy - locAy
    return SquareRoot(dx * dx + dy * dy)
endfunction


4.function GetReflectAngle takes unit target,unit projectile returns real

Well,some people might want one unit to bounce off another,so this here is the thing ;) - returns the new angle of movement for the bouncing off unit.The function is defined for 2 units,and if the projectile (or the unit that will bounce off) is moving towards its facing angle.
This will not make anything bounce off something that is randomly shaped,works correctly if the unit it bounces against is circular or has a circular shield around itself.

JASS:
function GetReflectAngle takes unit target,unit projectile returns real
    local real alpha = AbsAngle(GetUnitFacing(projectile))
    local real beta = AbsAngle(AngleBetweenCoords(GetUnitX(projectile),GetUnitY(projectile),GetUnitX(target),GetUnitY(target)))
    local real x =  AbsAngle(alpha - beta)
    local real y = AbsAngle(90 - x)
    local real gamma = alpha + 2*y
    return gamma
endfunction


If someone wants it predefined so it uses something else instead of units,just post.
Uses some of the functions above

5.function SetAnyUnitHeight takes unit u,real z,real rate returns nothing

Why add crow form yourself and save a function call?Let me do it for you ;).
Sets the flying height of any unit.

JASS:
function SetAnyUnitHeight takes unit u,real z,real rate returns nothing
    call UnitAddAbility(u,'Amrf')
    call UnitRemoveAbility(u,'Amrf')
    call SetUnitFlyHeight(u,z,rate)
endfunction


6.function GetUnitZ takes unit u returns real

Returns the Z of a unit.

JASS:
function GetUnitZ takes unit u returns real
    local location l = GetUnitLoc(u)
    local real z = GetLocationZ(l)
    if u != null then
        call RemoveLocation(l)
        set l = null
        return z + GetUnitFlyHeight(u)
    endif
    set l = null
    return 0.
endfunction


7.function SetUnitZ takes unit u,real z returns nothing

Sets the Z position of a unit.This is absolute from the (sea?) level (Z = 0).
Check out the --u17ra 1337 h4xx0rz t3s7 m4p-- below.

JASS:
function SetUnitZ takes unit u,real z returns nothing
    local location l = GetUnitLoc(u)
    if u != null then
        call UnitAddAbility(u,'Amrf')
        call UnitRemoveAbility(u,'Amrf')
        call SetUnitFlyHeight(u,z - GetLocationZ(l),0)
        call RemoveLocation(l)
    endif
    set l = null
endfunction


Method fails if cliff height > z
 

Attachments

  • SetUnitZ.w3x
    14.1 KB · Views: 184

Trollvottel

never aging title
Reaction score
262
GetReflectAngle would have been useful for me :( but now its to late and i did it my own way (look at the meathook spell)

anyway, this functions are useful, gj.

i think functions like Get/SetUnitZ or/and Polarprojections with alpha and beta angle could fit in this library
 

Flare

Stops copies me!
Reaction score
662
1) Hmmm, never really thought of getting Abs of an angle :p

2) There is Atan2BJ, which is almost the exact same thing as that, except some things switched around (and only takes 1 x and y value)
JASS:
function Atan2BJ takes real y, real x returns real
    return Atan2(y, x) * bj_RADTODEG
endfunction


3) Not much can be said here :p

4) Do you really need to get Abs of the angles? Anyway, you could simplify it to this (I've worked this out on paper, and I think it is correct, but can't guarantee since I haven't actually tested it in WC3 yet, AND it works with negative angles)
JASS:
function Reflect takes unit reflector, unit projectile returns real
    local real rang = GetUnitFacing (reflector) // Mirror angle
    local real pang = AngleBetweenCoords(GetUnitX(projectile),GetUnitY(projectile),GetUnitX(reflector),GetUnitY(reflector)) // Entrance angle
    local real dif = rang - pang //Mirror angle - Entrance angle
    return pang + 2*dif
endfunction


End result of Reflect function should be
170px-Reflection_angles.svg.png
 

The Undaddy

Creating with the power of rage
Reaction score
55
>>GetUnitFacing (reflector)

What angle is that? :p
As much as I tested,the reflector can be facing anywhere while the projectile comes,and the outcome should be the same.

>>Do you really need to get Abs of the angles?

local real x = AbsAngle(alpha - beta) - Here definitely

Otherwise,I put it just for caution.We still haven't gone trough anything about angles of movement at school,so I have to learn from what I test and sketch,and what I see in the map :(

@Trollvottel

Maybe I'll add some,but kind of later,I'm tired now :)

EDIT:
btw Nice sketch Flare,did you do that yourself just now ? :)
Whatever I try to do on coreldraw (or anywhere for that matter) turns out all f***** up
 

Trollvottel

never aging title
Reaction score
262
2) yes Atan2 takes only 2 arguments, too. the only difference to the Atan2BJ function is that it returns radians and not degrees.

4) your reflect function has nothing to do with undaddy's, because if you dont have a specific facing for a mirror but a unit which's angle is irrelevant, your method fails.
well, just to write another reflection method:

JASS:
            function reflect takes unit u1, unit u2 returns real
                set lx = GetUnitX(u1)
                set ly = GetUnitY(u1)
                set angle = GetUnitFacing(u1)
                set llx = GetUnitX(u2)
                set lly = GetUnitY(u2)
                set angle2 = Atan2(ly-lly, lx-llx) * bj_RADTODEG
                if ( angle2 > 45 and angle2 < 135 ) or ( angle2 > 225 and angle2 < 315 ) then
                    return  -angle
                else
                    return 180-angle
                endif
            endfunction


@undaddy:
the sketch is from wikipedia: http://upload.wikimedia.org/wikiped...on_angles.svg/170px-Reflection_angles.svg.png
 

Flare

Stops copies me!
Reaction score
662
What angle is that?
As much as I tested,the reflector can be facing anywhere while the projectile comes,and the outcome should be the same.

Reflector is the the unit which is reflecting the projectile :p

local real x = AbsAngle(alpha - beta) - Here definitely

I'll show you the notes I wrote up before posting :)

Reflector angle (RA) = 45 (-315)
Projectile angle (PA) = 90 (-270)

Formula = PA + 2*(RA - PA)

90 + 2*(45-90)
90 + 2*(-45)
90 - 90
0 = Exit angle

Now, taking -315 as the reflector angle
90 + 2*(-315-90)
90 + 2*(-405)
90 - 810
-720 (+360)
-360 (+360)
0 = Exit angle (again)

Exact same result, with positive and negative form of the angle.

Haven't tried it with negative entrance angle, but result should be the same.

btw Nice sketch Flare,did you do that yourself just now

<3 Wikipedia :D
 

The Undaddy

Creating with the power of rage
Reaction score
55
>>@undaddy:
the sketch is from wikipedia: http://upload.wikimedia.org/wikipedi...angles.svg.png

Thought it might be form somewhere,but hey,it doesn't matter really.

And can someone explain which X and Y does Atan2 take?I don't get it :D

>>Reflector is the the unit which is reflecting the projectile

Then your way works for maybe a wall-like unit? Or something like that,because it depends on the reflectors facing :).Mine is for bouncing off,lets say,trees,or like a barrier around a mage,or shiny balls ;)
 

Flare

Stops copies me!
Reaction score
662
Lets take 2 points (p1 and p2) and their coordinates (x1,y1 and x2,y2)

Atan2 takes (y2-y1), (x2-x1). Basically, the slope of a line, then returns the angle in radians :)

JASS:
call Atan2 (y2-y1, x2-x1)


Then your way works for maybe a wall-like unit? Or something like that,because it depends on the reflectors facing .Mine is for bouncing off,lets say,trees,or like a barrier around a mage,or shiny balls

Ye, my method implied a stationary object ^^ I can adjust it if you like? :p
 

Trollvottel

never aging title
Reaction score
262
Lets take 2 points (p1 and p2) and their coordinates (x1,y1 and x2,y2)

Atan2 takes (y2-y1), (x2-x1). Basically, the slope of a line, then returns the angle in radians :)

JASS:
call Atan2 (y2-y1, x2-x1)


and Atan2BJ calls this and multiplies it with 57.29577951 (180/Pi) so you have degrees.
 

The Undaddy

Creating with the power of rage
Reaction score
55
Lets take 2 points (p1 and p2) and their coordinates (x1,y1 and x2,y2)

Atan2 takes (y2-y1), (x2-x1). Basically, the slope of a line, then returns the angle in radians :)

JASS:
call Atan2 (y2-y1, x2-x1)

Well,then my function is just a call slower :p.Still,maybe useful for someone (like me) who doesn't know Atan2 even exists :eek:

And,Trollvottel,mind explaining what did you mean exactly by "Polarprojections with alpha and beta angle"?
 

Trollvottel

never aging title
Reaction score
262
@undaddy: well, i can copy paste you this functions.
you need them if you work with x,y,z

normally on polarprojection, you have only this alpha-angle, its the angle on the ground, so it would be:

JASS:
 newx = x + distance * Cos(alpha*0.017353292)


but now there is the beta-angle which is in the z direction. now your x cant be as long as it was before so now you have:

erde_skizze.jpg

JASS:
 newx = x + distance * Cos(alpha*0.017353292) * Cos(beta*0.017353292)



well your functions would be:

JASS:
   function PolarX takes real x, real distance, real alpha, real beta returns real
        return x + ( distance * Cos ( beta * DTR ) * Cos ( alpha * DTR ) )
    endfunction

    
    function PolarY takes real y, real distance, real alpha, real beta returns real
        return y + ( distance * Cos ( beta * DTR ) * Sin ( alpha * DTR ) )
    endfunction

    
    function PolarZ takes real z, real distance, real beta returns real
        return z + ( distance * Sin ( beta * DTR ) )
    endfunction


to get distance between z-coords, you use this:

JASS:
    function SquareDistance takes real x0, real y0, real z0, real x1, real y1, real z1 returns real
        local real x = x1 - x0
        local real y = y1 - y0
        local real z = z1 - z0
        return SquareRoot( ( x * x ) + ( y * y ) + ( z * z ) )
    endfunction


well, and for angle between points:

JASS:
function AngleBetweenPointsAlpha takes real x0, real y0, real z0, real x1, real y1, real z1 returns real
    local real x = x1 - x0
    local real y = y1 - y0
    return Atan2 ( y, x ) * RTD
endfunction

function AngleBetweenPointsBeta takes real x0, real y0, real z0, real x1, real y1, real z1 returns real
    local real x = x1 - x0
    local real y = y1 - y0
    local real z = z1 - z0
    return Atan2 ( z, SquareRoot ( ( x * x ) + ( y * y ) ) ) * RTD
endfunction
 

The Undaddy

Creating with the power of rage
Reaction score
55
@Trollvottel

WOAH someone will be doing some heavy shit stuff with that :D :thup: :thup:.Anyways,no,not this year,not going to happen :D.
But anyways,the Get/Set unit Z funcs I have done.
 

Trollvottel

never aging title
Reaction score
262
Nice. but how about using a global location which you move to the x-y coordinates instead of always creating and destroying new locations? it would be faster. and if you dont want to do it:
YOU FORGOT TO NULL THE LOCATION VARIABLES!
 

The Undaddy

Creating with the power of rage
Reaction score
55
>>YOU FORGOT TO NULL THE LOCATION VARIABLES!
Do I have to?:D
I don't use any sytems that require handles :(
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top