Thread: How to spawn within min and max distance?

1. How to spawn within min and max distance?

I've been using the following code to randomly spawn things within a certain range:

This works great, except for the fact that I'd also like a minimum distance, not just a maximum. My problem is that I have dyscalculia (dyslexia with numbers), and I am NO GOOD at mathematical equations. I was wondering if someone could show me how to modify this to also include a minimum distance.

2. Do your math in polar coordinates, then use CBA functions to switch then to cartesian for you.

dir = random 360;
range = min + random ( max - min );

then convert and use that.

3. I thank you for the help, but to be honest I'm not really sure what you mean.

EDIT: Well, since I'm so terrible at math, I found a work around. I check to see if the random location chosen is further away from the player than the minimum distance, if not then it picks another random location until it finds one at the proper range. Does it work? Yes. Is it terribly inefficient? Yes. But it's only once at the beginning of the mission so I don't think it will matter much.

4. Here is how I do it, using Domination as base, located in x_scripts\x_createsecondary.sqf:
Code:
```_poss = [];
while {count _poss == 0} do {
_poss = [_target_array2 select 0, _target_array2 select 2] call XfGetRanPointCircleBig;
switch (d_params_LocationDifficulty) do {
//no check needed for case 0.
case 1: {if ((_poss distance [((_target_array2 select 0) select 0), ((_target_array2 select 0) select 1)] < (_target_array2 select 2)*0.4)) then {_poss = []}};
case 2: {if ((_poss distance [((_target_array2 select 0) select 0), ((_target_array2 select 0) select 1)] < (_target_array2 select 2)*0.8)) then {_poss = []}};
};
sleep 0.04;
};```
And the function (located in x_scripts\x_funcs\x_functions2.sqf):
Code:
```// get a random point inside a circle for bigger objects
// parameters:
// center position, radius of the circle
// example: _random_point = [position trigger1, 200] call XfGetRanPointCircleBig;
XfGetRanPointCircleBig = {
private ["_center", "_radius", "_center_x", "_center_y", "_ret_val", "_co", "_angle", "_x1", "_y1", "_nobs", "_helper"];
_center = _this select 0;_radius = _this select 1;
_center_x = _center select 0;_center_y = _center select 1;
_ret_val = [];_co = 0;
while {count _ret_val == 0 && _co < 50} do {
_angle = floor (random 360);
_x1 = _center_x - ((random _radius) * sin _angle);
_y1 = _center_y - ((random _radius) * cos _angle);
if (!(surfaceiswater [_x1, _y1])) then {
_nobs = [_x1,_y1,0] nearObjects ["Static",20];
if (count _nobs == 0) then {
if (!(surfaceIsWater [position _helper select 0, position _helper select 1])) then {
_slope = [position _helper, 5] call XfGetSlope;
if (_slope < 0.5 && !(isOnRoad (position _helper))) then {
_ret_val = [position _helper select 0,position _helper select 1,0];
};
};
deleteVehicle _helper;
};
};
if (count _ret_val == 0) then {
_co = _co + 1;
};
};
_ret_val
};```
_target_array2 select 0 contains the position.
_target_array2 select 2 contains the radius.
_target_array2 select 1 contains the location, but is not used in these scripts.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•