• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Area of Effect in a rectangle?

Status
Not open for further replies.
Level 37
Joined
Mar 6, 2006
Messages
9,240
I'm using that kind of thing in this spell: Static Fence

The main thing is this:
JASS:
// Returns a boolean that tells whether the point is inside a rectangle.
    // (px , py) = Point to check
    // (cx , cy) = lower left corner of the rect
    // (ax , ay) = upper left corner
    // (bx , by) = lower right corner
    function IsPointInRect takes real px , real py , real cx , real cy , real ax , real ay , real bx , real by returns boolean        
        local real dot1 = (px-cx)*(ax-cx) + (py-cy)*(ay-cy)
        local real dot2 = (ax-cx)*(ax-cx) + (ay-cy)*(ay-cy)
        local real dot3 = (px-cx)*(bx-cx) + (py-cy)*(by-cy)
        local real dot4 = (bx-cx)*(bx-cx) + (by-cy)*(by-cy)
        
        return dot1 >= 0 and dot1 <= dot2 and dot3 >= 0 and dot3 <= dot4
    endfunction
 
Level 16
Joined
Apr 4, 2011
Messages
995
I understand the general concept, but I'm not understanding how I use it. With the return, how do I use that return in an if/then/else statement?

JASS:
function IsPointInRect takes real px , real py , real cx , real cy , real ax , real ay , real bx , real by returns boolean

This part also doesn't make sense. The variables were never set, how do they tell whether the unit is inside it or not?
 
Those variables are called parameters, they are sent to the function when you call the function

JASS:
//I'll use the native function KillUnit as an example:
//this is what the function KillUnit looks like

native KillUnit takes unit whichUnit returns nothing

call KillUnit(GetTriggerUnit())

//on this case, KillUnit takes a unit parameter, so we give it a unit (via the GetTriggerUnit() function) when we call it...

also, the code Maker posted checks the point not the unit... meaning you should feed the function the point where the unit is located so that it can check if the point is within the range (which means the unit is inside the range too)...
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
This does not cover the unit's collision size, though. To count it in, you could find the closest point from a unit to the quadrangle, then use
constant native IsUnitInRangeXY takes unit whichUnit, real x, real y, real distance returns boolean
there with a distance of zero or if you have the collision size, search for intersections of circle and the quadrangle's sides. Of course, this only has do be done in case the unit's origin was not already within.
 
It all depends on the value you input when you call the function...

JASS:
//example

 // (px , py) = Point to check
    // (cx , cy) = lower left corner of the rect
    // (ax , ay) = upper left corner
    // (bx , by) = lower right corner

call IsPointInRect(0.00 , 0.00 , -300.0 , -200.0 , -300 , 200.0 , 300.0 , -200.0)
/*
here, the parameters cx,cy,ax,ay,bx,by defines the created box...

but don't we need to define the upper right corner too? 

or also, we can just check the upper right and the lower left...
*/
 
Level 16
Joined
Apr 4, 2011
Messages
995
And shockwave doesn't allow you to add the units to a unitgroup so you can do other stuff with them. I'm not talking about shockwave.

One last question about this. I don't understand, how I tell if the unit is inside the rectangle. Once the rectangle is defined, and we know that the units are inside the rectangle, how do I use that information, will it return a boolean to true, or will they be added to a specified unit group, or what?
 
Status
Not open for further replies.
Top