Jump to content
arma_max

How can I list all units that are in the player's FOV?

Recommended Posts

Hi,

 

I want to list all units/vehicles that are in the player's field of view FOV, no matter if the player has actually already detected them or not and no matter how far away they are or if they are even visible at all. The goal is to draw a small box around every unit as some kind of cheating help 🙂

 

I guess I could just iterate over all units on the battlefield and use getObjectFOV and the angle between the player and every unit to determine if they are in that FOV or not but is there something more handy and less performance-killing?

 

Max

Share this post


Link to post
Share on other sites

From the wiki

 

”Returns the target pointed at by the player (usually with cross-hairs). The target has to be known to the player to some degree (knowsAbout > 0). If target is completely unknown, command returns objNull.”

 

That is exactly not what I want. It should also list units the player does not know about. 
 

Max

Share this post


Link to post
Share on other sites

I know that this is most probably not what you are looking for but still, this is the most generic approach I could think of (still, it most probably is quite CPU intense).

 

You have to go through the units that are of interest to you. It is strongly recommended to do so for units that have a certain distance from the player in order to avoid going through all the units on the map). For example, you can use the nearestObejcts command suggested by Spriterfight to get all the units that are 500 m away from the player.

 

Then you would have to check the angle between the vector pointing to the direction the player is looking and the units of interest. An example snippet could look like the following

// Get the units that are 500 metres or closer to the unit
private _closeUnits = player nearObjects["Man", 500];

// Get the FOV to use it further down (to avoid calling the command in loops)
private _playerFOV = (getObjectFOV player)/2; // Divide by two to get the angle from the centre of viewpoint to the edge of the FOV

// Initialise empty array to hold visible units
private _visibleUnits = [];

// Search through the units
{
	// Get the angle between the eyes of the player and the unit
	private _toUnit = (eyePos player) vectorFromTo (getPos _x);

	// Calculate the angle between the direction the player is looking at and the unit
	private _angle = (eyeDirection player) vectorCos _toUnit; // Get the cosine of the angle
	_angle = acos(_angle); // Get the angle (in radians)

	// Check that the angle is less than or equal to FOV/2
	if(_angle <= _playerFOV) then {
		_visibleUnits pushBack _x; // Add the unit to the array
	};
} forEach _closeUnits;

Now, there's a lot of issues here... First of all, I am not sure what exactly the command getObjectFOV returns, as there is no clear notion to me what the FOV is. As far as I understand the concept of FOV (at least in the way it is defined in ArmA), there's a vertical FOV and a horizontal FOV, which you can get from the command getResolution. Which one the command returns is unknown to me.

 

Furthermore, with the provided snippet you cannot differentiate between friends and foes. For this, assuming that the only friendly side is WEST you could do something like

// Get the units that are 500 metres or closer to the unit
private _closeUnits = player nearObjects["Man", 500];

// Get the FOV to use it further down (to avoid calling the command in loops)
private _playerFOV = (getObjectFOV player)/2; // Divide by two to get the angle from the centre of viewpoint to the edge of the FOV

// Initialise empty array to hold visible units
private _visibleUnits = [];

// Search through the units
{
	// Get the angle between the eyes of the player and the unit
	private _toUnit = (eyePos player) vectorFromTo (getPos _x);

	// Calculate the angle between the direction the player is looking at and the unit
	private _angle = (eyeDirection player) vectorCos _toUnit; // Get the cosine of the angle
	_angle = acos(_angle); // Get the angle (in radians)

	// Check that the angle is less than or equal to FOV/2 and the unit is not WEST
	if(_angle <= _playerFOV && !{(side _x) isEqualTo west}) then { // ADDED ADDITIONAL TEST
		_visibleUnits pushBack _x; // Add the unit to the array
	};
} forEach _closeUnits;

This way you could also have more arrays to hold each faction's units and create rectangles of different colors for each side.

 

Please note that the provided scripts are NOT tested, so please spend some time to test before assuming they work as intended.

 

I do hope this helps in some way. If it doesn't and/or you need further assistance please don't hesitate to ask :).

  • Like 3

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

×