Jump to content
Sign in to follow this  
Kovah85

How to spawn within min and max distance?

Recommended Posts

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

_marker = [(getMarkerPos _spawn select 0)-_rad1*sin(random 359),(getMarkerPos _spawn select 1)-_rad1*cos(random 359)];

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Kovah85

Share this post


Link to post
Share on other sites

Here is how I do it, using Domination as base, located in x_scripts\x_createsecondary.sqf:

_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):

// 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 {
			_helper = "RoadCone" createVehicleLocal [_x1,_y1,0];
			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.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×