Jump to content
Sign in to follow this  
FederalRazer89

Trying to get a position from arrays of nearestObjects

Recommended Posts

As the title suggest i am trying to get some position from arrays of the command "nearestObjects". I want the "nearestObjects" to look for a certain amount of house, so i can identify a village or town with the script and then use the average of all the listed house in the array. That way the position i am after will not be heavily affected by one or two houses caught in the calclated radius.
 

_Center  = getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition");
_mapsize = (_Center select 0)*2;

_RandomStartPos      = [_Center, (_mapsize*0.77), (_mapsize*0.78), 3, 1, 20, 0] call BIS_fnc_findSafePos;
sleep 1;

player moveInCargo [_heli,5];


_enemySpawnPos = [_RandomStartPos, (_mapsize*0.7), _mapsize*0.71, 3, 0, 20, 0] call BIS_fnc_findSafePos;
createMarker ["marker2",_enemySpawnPos];
"marker2" setMarkerShape "ELLIPSE";
"marker2" setMarkerSize [500,500];
"marker2" setMarkerText "Enemy resupply line";
"marker2" setMarkerAlpha 0.5;
 
_findhouses   = nearestObjects [_enemySpawnPos, "house"", (_mapsize*0.2)];        //small problems on this line
_divide       = count _findhouses; 
_fortifiedpos = _findhouses/_divide;                                                          //The bigger problem

createMarker ["marker3",_fortifiedpos];
"marker3" setMarkerShape "ELLIPSE";
"marker3" setMarkerSize [500,500];
"marker3" setMarkerText "Enemy resupply line";
"marker3" setMarkerAlpha 0.5;


I not really sure how should proceed, because i dont think the line "_fortifiedpos" will work, the problem above prevents it from execute. "nearestObjects" contains arrays of the houses and the houses also have arrays, if there isnt a command that would merge the arrays.
If anyone have any input that would be nice, arrays are some a bit new to me so this is kind of over my head.

Share this post


Link to post
Share on other sites

Hmm didnt think about that, but wont that depend on the map creator having to create those locations? Because i dont know too much about mapmaking in arma i am not sure if i can depend on those locations. But i going to try it on some maps and see how it goes.

 

Share this post


Link to post
Share on other sites

When you have that array of houses, you need to iterate through it to build an average position as a new array.  Each position is a 3-element array, so your new array will also be 3-elements: consider each element as its own unique 1-dimensional position (x, y, and z). 
 

private ["_meanPosition", "_length", "_component"];
_meanPosition = [];
_length = count _findhouses;
for "_i" from 0 to 2 do {
  _component = 0;
  for "_j" from 0 to (_length - 1) do {
    _component = _component + (((position (_findhouses select _j)) select _i) / _length);
  };
  _meanPosition pushBack _component;
};

This is the same as adding all the x's and dividing by the total, then adding all the y's and dividing by the total, and finally adding all the z's and dividing the total, to yield three position averages (The average 3D position).  In this algorithm division is done at each step instead of at the end to avoid accumulating really large sums that could cause problems.

Here is a function I have been using, which you can paste in init.sqf or somewhere like that:
 

fnc_vector_mean = {
	/* Find the mean vector of a list of vectors */
	_vectors = _this;
	private ["_alen", "_component", "_mean"];
	_mean = [];
	_alen = count _vectors;
	for "_i" from 0 to 2 do {
		_component = 0;
		for "_j" from 0 to (_alen - 1) do {
			_component = _component +
				(((_vectors select _j) select _i) / _alen);
		};
		_mean pushBack _component;
	};
	_mean
};

That is only for 3D vectors, so the name is not very apt, but it would not be complicated to extend it (but you don't need to in this case).

  • Like 2

Share this post


Link to post
Share on other sites

Thanks dwringer

Going to try it when i downloaded arma 3 on the laptop, hope it can run the game or else i will have to wait untill get home.


 

Share this post


Link to post
Share on other sites

It worked, and i added a waypoint to the nearest house. Thanks for all the help.

The added line:

_officer2      = createGroup east;
_Enemyofficer2 = "O_officer_F" createUnit [_meanPosition,_officer2];
_selecthouse = nearestTerrainObjects [_officer2 ,["BUILDING", "HOUSE"],750,true];

wp_ = _officer2 addWaypoint [(_selecthouse select 0),0];

Going to tweak the paramaters a bit more now.

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  

×