Snippet Angle Between Points Fix

Reaction score
86
Hey, I'm not 100% sure if anyone has posted this kind of snippet, but here it is.

Uses:
- Basically if you want to decide if a unit is in front of another.

Why: Why can't you use normal Angle Between Points? Because, for one, it can return a negative angle! Ranges from 180- (-180).

Of course, you may think this is an easy fix, but, say, for example, the angle between points is 359, and the facing angle for the unit is 5. Say you wanted to decide if the point is in front, example, 120 degree field of vision. Then you would take the facing angle and add 60, and subtract 60, and see if the angle lies between. What happens? It doesn't work! Take a look

Ranges from 65 - (-55)... Now 359 is congruent to -1 which clearly lies within this range. BUT, it will not recognize it as so! (There is also another case, unit is in 1st quadrant, facing in 4th).

Due to these 2 cases, many people have become frustrated by this annoying problem.

Which is why I created this tiny function that acts as a fix.

Takes: real FacingAngle, real XoflocA, real YoflocA, real Xoflocb, real Yoflocb

Returns: real Angle

This function basically decides how it is suppose to convert the angle that the formula spits out!

Coded In Jass.

How to add to your map:

For Jass:
-I think you already know but, go to Trigger editor, click the map icon at the very top of list. Changes trigger frame, copy and paste the function into that frame. Set to a variable when you call it.

For GUI:
-Go to Trigger editor, click the map icon at the very top of list. Changes trigger frame, copy and paste the function into that frame.
-Create a new real variable.Example RealVar.(referred to as udg_RealVar)
-Need variables for the facing angle, and the two locations. (Unless you know a bit of Jass) Otherwise referred to as udg_Facing, udg_locA, and udg_locB respectively.
-Go to trigger and put in this text in a custom script:
JASS:

set udg_RealVar = AngleBetweenPointsFOV(udg_Facing,GetLocationX(udg_locA),GetLocationY(udg_locA),GetLocationX(udg_locB),GetLocationY(udg_locB))

And then use RealVar however you like!

How to use in Map:
-Reason its called AngleBetweenPointsFOV is because FOV stands for Field of Vision. It's specifically created for that.

-What to do with the real it returns: Well, let's see: FOV = 120 degrees

-Check if the real it returns is LESS than the facing of the unit + 60 (half the FOV)
-Check if the real it returns is GREATER than the facing of the unit - 60 (half the FOV)
If both these conditions are true, then the point is in front of the units 120 degree FOV.

- How else? You can make a backstab ability (as vypur85 said), and make it such that:
if the angle is GREATER than Facing of the unit + 135
if the angle is LESS than the Facing of the unit - 135
If both of these conditions are true that means the point is behind the unit in a 90 FOV.

-Very confusing isn't it? Well, use your imagination, because that's how it works.

Advantages:
-As many function calls as regular AngleBetweenPoints.
-Helps with the Field of Vision



Limitations:
Yes, there are a few pit falls with this small snippet. The only reason that I did not fix this, is because it would decrease the options of what you can do with this. Why? Well, it would involve making a system, that also takes the Field of Vision and then simply returns a boolean stating if it's in that area or not. To me, that is pretty lame... Which is why I have left it as such a small Snippet.

Now onto the ACTUAL limitations: Pretty much, if you try and compare it by adding values over 90 to the facing value, then, there is a chance for a bug. Two, if you add a value over 180, then it will bring a chance for another bug.
Other than that, it's lightweight, efficient, and pretty customizable.

Also, added a GUI version that shouldn't have any limitations. But. it takes the Field of Vision into account, like I said before, and decreases customization.
Here's the code:
JASS:

function AngleBetweenPointsFOV takes real Facing, real XoflocA, real YoflocA, real XoflocB, real YoflocB returns real
    local real Angle = bj_RADTODEG * Atan2(YoflocB - YoflocA, XoflocB - XoflocA)
    if (Angle < 0) and (Facing > 90) then
        set Angle = Angle + 360
    endif
    if Angle < 90 and (Facing > 180) then
        set Angle = Angle + 360
    endif
    return Angle
endfunction

Older Version: (Takes locations)
The difference is this one takes locations instead of reals:

JASS:
function AngleBetweenPointsFOV takes real Facing, location locA, location locB returns real
    local real Angle = bj_RADTODEG * Atan2(GetLocationY(locB) - GetLocationY(locA), GetLocationX(locB) - GetLocationX(locA))
    if (Angle < 0) and (Facing > 90) then
        set Angle = Angle + 360
    endif
    if Angle < 90 and (Facing > 180) then
        set Angle = Angle + 360
    endif
    return Angle
endfunction
Here I have attached two versions of the map. The "test2" one is the Jass version, and the other one is the GUI Version.
 

Attachments

  • test2.w3x
    16.9 KB · Views: 260
  • Unit Facing Side V2.w3x
    20.7 KB · Views: 230

vypur85

Hibernate
Reaction score
803
You should actually give some example like Backstab or Damage Reduction at different angle etc etc. Probably you could also include GUI, to make it easier for all to understand. And the way you paragraph your snippet seems a little bit messy. Not sure why, but it really seem not neat to me :p. Probably you should justify the sentences here and there, add some bullets here and there, or bold/italise something here and there...
 

Kenny

Back for now.
Reaction score
202
Seems like this will come in handy for some people. I have already come up with a few ways to use it. As vypur85 said, maybe bold your subheadings or make them a different colour, it will give your presentation a better structure :). Also, it may be an idea to make another one of these snippets that uses reals instead of locations. Most JASS'ers out there prefer to stay away from locations, and will probably accept the two extra arguments if it means they do not have to use local locations. For example, if you are finding the angle between two units, you can either create then remove then null two locations, or use the GetUnitX(unit) and GetUnitY(unit) and not have to null them. That way if you have two, you are providing "the greatest good for the greatest number of people".

Anway +rep for the concept, as i said before this will definately come in handy for people who like "backstab" type spells. And i have seen many threads asking about how they are done.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
You sure the math used there is actually correct?


How is this going to help with, for example, me facing 1 degree and this returning 359 instead of the "usual" -1?
 
Reaction score
86
It does return -1. This checks for that specific case. If the facing angle is 1 then it will leave the angle alone because if you were to take the facing angle of the unit and subtract from it it would be negative and the angle would fall within the range.

Similarly, if the facing angle of the unit was 359, then it would add 360 to it.

So, simply, what it does is it takes the angle between the points and makes it comparable to the facing angle supplied.

So pretty much, in a nutshell, if the unit is facing the 3rd or 4th quadrant, and the angle is less than 90, then it will add 360 to it so it would be comparable with the facing example, which is, as an example, 359. So if the angle was 5 then it would return 365 in this case.
 

vypur85

Hibernate
Reaction score
803
I don't understand. Why not just use the condition if the angle between points is less than 0, then add 360. Isn't it easier?

Code:
Set Angle = Angle between Point1 and Point2
If then else
 Condition
  Angle Less than 0
 Then
  Set Angle = Angle + 360
 Else

Or is this what you do in your JASS trigger. Sorry, I'm a JASS-noob.
 
Reaction score
86
Kind of~ It depends on the facing angle of the unit. If the facing angle is 1 and the angle is -1, these two angles are only 2 degrees away right? But, if I just added 360 to it all the time, then it would become 1 and 359.
 
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