| 01-02-2006, 04:13 PM | #1 |
can anyone help me out with a trigger that basically says random point X,Y which is atleast #distance away from mutliple units. |
| 01-02-2006, 05:34 PM | #2 |
Ouch... I guess... looping moving a location around untill it matches conditions is the best way. |
| 01-02-2006, 05:41 PM | #3 |
This one needs to be solved mathematically: The distance between two points a and b is the square root of (dx² + dy²). dx is the difference xb - xa and dy is the difference yb - ya. dx = xb - xa dy = yb - ya Your minimum distance is the radius r. So we get the formula (r must be greater than or equal to the distance between your point a and a random point b): sqrt(dx² + dy²) >= r Lets suppose there is only a minimum distance (as you specified in your post), you can start of with any x or any y (that fits onto your map). Then you need to determine a random y or x (missing coordinate). However, now you need to restrict the range it can be chosen from. Lets assume we picked a random x and now we want a y so that our new point is at least r away. So we'll take the formula again: sqrt(dx² + dy²) >= r Lets get rid of the square root: (dx² + dy²) >= r² Now we substract dx² (we already have chosen xb so we actually know its value already): dy² >= (r² - dx²) Now we know the minimum value for dy². Ok and now we will substitute dy with the original definition of it (look above): dy² = (yb - ya)² Lets get rid of the power of 2: dy = yb - ya Now we can calculate a minimum value for |yb| (absolute of yb). Since we had powers of 2, it will only be the positive side of it. We can randomize the sign to get the full coordinate spectrum. Ok, so how to do it in jass? What confuses me a bit is multiple units. To have this work, you need one fixed point to start with. I know no easy solution for more than one points (other than trying to find a point which fits the condition for all points). First you need to get the coordinates to start with. GetUnitX and GetUnitY would be the way to go. You can store these values in variables although it might not be necessary. For the sake of simplicity I'll use a lot of variables here. Code:
local unit u = (whatever you need to get your unit)
local real xa = GetUnitX(u)
local real ya = GetUnitY(u)
local real r = (some value here) // your minimum distance
local real xb = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea), GetRectMaxX(bj_mapInitialPlayableArea)) // xb can be anything on the map
local real dx = xb - xa
local real dy = SquareRoot(Pow(r, 2) - Pow(dx,2))
local real yb = dy + ya
//to have negative y values too, we randomize the sign
if (GetRandomInt(0, 1) == 1) then
set yb = -yb
endif
//now you have a random point(xb,yb), you can use it as you wishThe above code is more of a snippet. You can create a function out of it or embed it into a trigger. You don't need to define so many locals, I did that only to improve readability. Maybe the code needs adaption for multiple units, which could be kinda difficult. Edit: iNfraNe's way would be a lot more easier to code. However, it has unknown execution time. It can be faster, it can be slower. Additionally it might not really be random. |
