Jump to content
Sign in to follow this  
iceman77

Random position inside trigger area

Recommended Posts

I need to spawn a vehicle randomly inside of a trigger area or marker area, either or.

Here's how iam creating the vehicle:

private ["_loc","_dir","_veh"];

_loc = _this select 0;
_dir = { _veh setDir getDir _loc};

_vehArray = ["A10","AH1Z","AV8B2"," AV8B"];
_veh = _vehArray select (floor(random(count _VehArray)));
_veh = _Veh createVehicle position _loc;
call _dir

As of now a random vehicle spawns on a invisible hpad, I would like the vehicle to spawn randomly inside a trigger area.How to?

Share this post


Link to post
Share on other sites

Thanks Kylania. Is there no other way to get 1 random position from a marker area or trigger, besides using Shukos scripts? His scripts are very nice, I can surely respect such great scripting. It just seems overkill for the seemingly simple task iam trying to accomplish.Unless getting 1 random pos from an area requires much more complex scripting then I thought it would.If I could, id like to avoid using the 17 scripts/functions to get 1 random pos, if you know what i mean.

regards

Share this post


Link to post
Share on other sites

If you use CBA:

_pos = getPos h1;
_areasize = 200;
_position = [_pos, _areasize] call CBA_fnc_randPos;  

_position will be randomly created in a 200m radius around the object h1.

Or...

_position = [(getPos h1 select 0) + ((random 200)+(random -200)), (getPos h1 select 1) + ((random 200)+(random -200)), 0];

  • Like 1

Share this post


Link to post
Share on other sites

You can try this also...

It's straight out of a mission I was working on so not pretty... but I'm just back from vacation so too stuffed to dick around with it right now.

Can be modified to do triggers as well. Right now only works with markers.

It's a function. I'm assuming here that you know about implementing functions... so no instructions on making it work!

And unless I'm missing something in the commands. Yes...it's more complicated than you might think. Different shaped markers/triggers with different rotations and dimensions make it a bit troublesome.

Having said that... the code here is more than you actually need, but it does more than you need :)

call with:-

//find 100 random points within the marker area

_locs = [_mkrnam,[100]] call fnc_getRandMkrLocs;

or...

//find points on a 10x10 grid within the marker area.... so 100 points

_locs = [_mkrnam,[10,10]] call fnc_getRandMkrLocs;

or...

//find 100 random points with a gradient of no more than 0.15

//that will fit a vehicle like wtank1

_locs = [_mkrnam,[100],wtank1,0.15] call fnc_getRandMkrLocs;

or...

//find points on a 10x10 grid within the marker area with a gradient of no

//more than 0.15 that will fit a vehicle like wtank1

_locs = [_mkrnam,[10,10],wtank1,0.15] call fnc_getRandMkrLocs;

fnc_getRandMkrLocs

private ["_mkr","_num","_obj","_grd","_mkrsiz","_mkrszx","_mkrszy","_mkrpos","_minx","_miny","_mkrdir",
	 "_locs","_flatpt","_colr","_i","_ranx","_rany","_mrkr","_m","_stepx","_stepy","_newx","_newy"];

//Findflat function
_fnFindFlat = {

private ["_pos2chec","_objsize","_maxigrad","_flatpt"];

_pos2chec = _this select 0;
_obj2plac = _this select 1;
_maxigrad = _this select 2;
//_pos2chec = getPos _obj2chec;
_flatpt = (_pos2chec) isflatempty [(sizeof typeof _obj2plac) / 2,0,_maxigrad,(sizeof typeof _obj2plac),0,false,_obj2plac];
//_flatpt = (_pos2chec) isflatempty [(_objsize) / 2,0,_maxigrad,(_objsize),0,false];
_flatpt

};

_mkr = _this select 0;	//marker name
_num = _this select 1;	//an array with either one parameter for random points...or two parametrs for grid points
_obj = if (count _this >2) then {_this select 2};	//the object to find size of area needed
_grd = if (count _this >3) then {_this select 3};	//the allowable gradient of the point checked.. 0-1

_mkrsiz = markersize _mkr;

_mkrszx = _mkrsiz select 0;
_mkrszy = _mkrsiz select 1;

_mkrpos = getmarkerpos _mkr;

_minx =  0 - (_mkrszx/2);
_miny =  0 - (_mkrszy/2);

_mkrdir    = (markerDir _mkr) * -1;
_mkrdir    = _mkrdir mod 360;

_locs = [];

_flatpt =[0,0,0];
_colr = "colorYELLOW";

//one parameter supplied...therefore do x random points
if ((count _num) ==1) then {

_cnt = _num select 0;

//for [{_i=0},{_i<_cnt},{_i=_i+1}] do {
for "_i" from 0 to (_cnt-1) do {

	//random x,y around zero
	_ranx = _minx + random _mkrszx;
	_rany = _miny + random _mkrszy;

	//rotate it
	_newx = 2 * ((_ranx * cos(_mkrdir)) - (_rany * sin(_mkrdir)));
	_newy = 2 * ((_ranx * sin(_mkrdir)) + (_rany * cos(_mkrdir)));

	//add it to map coords
	_newx = (_mkrpos select 0) + (_newx);
	_newy = (_mkrpos select 1) + (_newy);

	if (count _this >2) then {
		_flatpt = [[_newx,_newy],_obj,_grd] call _fnFindFlat;
		_colr = "colorRED";
	};

	if ((not surfaceiswater [_newx,_newy]) and (count _flatpt >0)) then {

		//_locs = _locs + [[_newx,_newy]];
		_locs set [count _locs,[_newx,_newy]];

		/*
		_mrkr = format ["mkr_%1_%2", diag_ticktime,_i];

		_m = createMarkerLocal [_mrkr, [_newx,_newy]];
		_m setMarkerShape "ICON";
		_m setMarkerColorLocal _colr;
		_m setMarkerTypeLocal "mil_dot";
		_m setMarkerSizeLocal [1, 1];
		_m setMarkerAlphaLocal 0.2;
		*/
	};

	//hint format ["_i: %1",_i];
};
};

//two parameters supplied...therefore do [stepx,stepy] grid
if ((count _num) ==2) then {

_stepx = (_mkrszx/(_num select 0));
_stepy = (_mkrszy/(_num select 1));

for [{_i=_minx},{_i<=(_minx+_mkrszx)},{_i=_i+_stepx}] do {

	for [{_j=_miny},{_j<=(_miny+_mkrszy)},{_j=_j+_stepy}] do {

		//rotate it
		_newx = 2 * ((_i * cos(_mkrdir)) - (_j * sin(_mkrdir)));
		_newy = 2 * ((_i * sin(_mkrdir)) + (_j * cos(_mkrdir)));

		//add it to map coords
		_newx = (_mkrpos select 0) + (_newx);
		_newy = (_mkrpos select 1) + (_newy);

		if (count _this >2) then {
			_flatpt = [[_newx,_newy],_obj,_grd] call _fnFindFlat;
			_colr = "colorRED";
		};

		if ((not surfaceiswater [_newx,_newy]) and (count _flatpt >0)) then {

			//_locs = _locs + [[_newx,_newy]];
			_locs set [count _locs,[_newx,_newy]];

			/*
			_mrkr = format ["mkr_%1", diag_ticktime];

			_m = createMarkerLocal [_mrkr, [_newx,_newy]];
			_m setMarkerShape "ICON";
			_m setMarkerColorLocal _colr;
			_m setMarkerTypeLocal "mil_dot";
			_m setMarkerSizeLocal [1, 1];
			_m setMarkerAlphaLocal 0.2;
			*/
		};

		//hint format ["_i,_j: %1,%2",_i,_j];

        };
};
};

_locs

Hope you get some joy out of it!

Edited by twirly
Clarity
  • Like 1

Share this post


Link to post
Share on other sites
Or...

_position = [(getPos h1 select 0) + ((random 200)+(random -200)), (getPos h1 select 1) + ((random 200)+(random -200)), 0];

[hairsplittin]

its not a real circle is it? :D

[/hairsplitting]

Share this post


Link to post
Share on other sites

@ Wiggum This isn't exactly what im looking for but it seems simpler using an object eh?I may use this for simplicity sakes.Thanks man.

@Twirly Thanks, gonna mess around with that a bit, pretty sweet!

Share this post


Link to post
Share on other sites

A trigger is also a object, so you could use:

_pos = getPos mytrigger;
_areasize = triggerArea mytrigger select 0;

Share this post


Link to post
Share on other sites
@Twirly Thanks, gonna mess around with that a bit, pretty sweet!

No worries mate.

One thing though.... I guess I should have named the function/script fnc_getLocsInMkr instead... as it also return points on a grid and not only random points.

I thought I'd just clear that up for other people reading this thread.

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  

×