Jump to content
Sign in to follow this  
Luft08

AI move direction on road

Recommended Posts

I wish to place a vehicle on a road and send it to a random road location far  away. I can get the direction of the road via a function:

getRoadDir = {
	params["_roadSeg"];
	private _info = getRoadInfo _roadSeg;
	private _dir = (_info select 6) getDir (_info select 7);
	_dir
};

My problem is that I need the vehicle to always immediately start moving toward its destination without an initial turn around.  The "road direction" I want is either what is returned by the function or 180 degrees different and I don't know how to determine that. 

Share this post


Link to post
Share on other sites

Hi there,

it was a little bit tricky, but I figured it out!

 

The script does what you want, but watch out:

The best direction != always the shortest road to go!

 

How to set it up:

 

Description.ext  

class CfgFunctions
{
	class JF
	{
		class Scripts
		{
			class SetOrientation {file = "SetOrientation.sqf"};	//or whatever path you like
		};
	};
};

 

SetOrientation.sqf

params ["_object", "_targetPos"];
private ["_road", "_dirRoad", "_pos", "_relDir"];

_pos = getPos _object;

if (isOnRoad _pos) then
{
	_road = roadAt _pos;
	_dirRoad = getDir _road;
	_relDir = _road getRelDir _targetPos;

	if (_relDir > 180) then {_relDir = 360 - _relDir}; 	
	if (_relDir < 90) then {_object setDir _dirRoad} else {_object setDir (_dirRoad - 180)};	
	
	true;
} else { false };

How to call it:

 

[object, targetPos] call JF_fnc_SetOrientation;

 

Examples:

  • [this, getPos (myTarget1)] call JF_fnc_SetOrientation;
  • [this,  [100, 200, 0]] call JF_fnc_SetOrientation;
  • [myVehicle1,  [100, 200, 0]] call JF_fnc_SetOrientation;
  • _doesItWork = [this, getPos (myTarget1)] call JF_fnc_SetOrientation;

 

"Features":

  • object has (!!!) to be on a road
  • wheter the script was able to set the direction or not, it returns true or false
  • Like 1

Share this post


Link to post
Share on other sites
6 hours ago, Smart Games said:

Hi there,

it was a little bit tricky, but I figured it out!

 

The script does what you want, but watch out:

The best direction != always the shortest road to go!

 

How to set it up:

 

Description.ext  


class CfgFunctions
{
	class JF
	{
		class Scripts
		{
			class SetOrientation {file = "SetOrientation.sqf"};	//or whatever path you like
		};
	};
};

 

SetOrientation.sqf


params ["_object", "_targetPos"];
private ["_road", "_dirRoad", "_pos", "_relDir"];

_pos = getPos _object;

if (isOnRoad _pos) then
{
	_road = roadAt _pos;
	_dirRoad = getDir _road;
	_relDir = _road getRelDir _targetPos;

	if (_relDir > 180) then {_relDir = 360 - _relDir}; 	
	if (_relDir < 90) then {_object setDir _dirRoad} else {_object setDir (_dirRoad - 180)};	
	
	true;
} else { false };

 

Thanks for taking a look at this but wouldn't the AI controlled vehicle still turn around if say the road connected to the target segment curved around and went the opposite direction? 

 

What I'm doing is spawning a convoy one vehicle at a time at the same spawn spot and I need each vehicle to move out of the way before the next vehicle spawns and causes massive destruction.  If a vehicle ever takes the time to turn around bad things happen.  Since both the starting location and the ending location are random I can never be sure what the AI will do.

 

I think there is some way to query Arma as to the AI path that will be taken but I don't know to get that information.

 

I'm looking at the Bohemia "calculatePath" function but I don't understand how to use it.

Share this post


Link to post
Share on other sites
10 minutes ago, Luft08 said:

What I'm doing is spawning a convoy one vehicle at a time at the same spawn spot and I need each vehicle to move out of the way before the next vehicle spawns and causes massive destruction. 

Just wait until the position is clear before you spawn the next vehicle. 

Share this post


Link to post
Share on other sites

 

My current convoy function looks like this:

if(!isServer) exitWith {};

params["_leadVehicleType", "_endVehicleType", "_vehicleTypeArray", "_convoyStartPos", "_convoyEndPos", "_vehicleCount", "_startDir", "_side", "_maxLeadSpeed", "_vehicleSeparation", "_vehicleStorageArray"];

// create lead escort vehicle
private _vehArray = [_convoyStartPos, _startDir, _leadVehicleType, _side] call BIS_fnc_spawnVehicle;
_vehicleStorageArray pushBack _vehArray;

private _veh = _vehArray select 0;
private _leader = effectiveCommander _veh;
private _group = _vehArray select 2;

_group setFormation "COLUMN";
_veh limitSpeed _maxLeadSpeed;

private _wp = _group addWaypoint [_convoyEndPos, 0];
_wp setWaypointBehaviour "SAFE";
_wp setWaypointType "MOVE";

// Create convoy vehicles
for "_x" from 1 to _vehicleCount do {
	private _result = _veh distance _convoyStartPos > 20;
	sleep 10;
	private _vehType = selectRandom _vehicleTypeArray;
	private _vehArray = [_convoyStartPos, _startDir, _vehType, _side] call BIS_fnc_spawnVehicle;
	_vehicleStorageArray pushBack _vehArray;
	private _veh = _vehArray select 0;
	_veh limitSpeed _maxLeadSpeed + 5;
	{[_x] joinSilent _leader}forEach _vehArray # 1;
};

sleep 10;

_vehArray = [_convoyStartPos, _startDir, _endVehicleType, _side] call BIS_fnc_spawnVehicle;
_vehicleStorageArray pushBack _vehArray;

_veh = _vehArray select 0;
_veh limitSpeed _maxLeadSpeed + 5;

	
{[_x] joinSilent _leader}forEach _vehArray # 1;

{
	private _veh = _x select 0;
	_veh setConvoySeparation _vehicleSeparation;
} forEach _vehicleStorageArray;

If you want to test it create a new mission using the Altis map and launch the convoy using something like:

if(!isServer) exitWith {};

activeVehicleArray = [];
private _vehicleTypeArray = ["B_Truck_01_ammo_F", "B_Truck_01_box_F", "B_Truck_01_fuel_F", "B_Truck_01_medical_F", "B_Truck_01_Repair_F", "B_Truck_01_covered_F"];
// private _startEscortType = "B_MRAP_01_gmg_F";
private _startEscortType = "B_APC_Wheeled_01_cannon_F";
private _endEscortType = "B_APC_Wheeled_01_cannon_F"; 
// private _endEscortType = "B_MRAP_01_gmg_F";
private _convoyStartPos = [3626.53,13169.2];
private _convoyEndPos = [14637.4,16771.7];
private _convoyVehicleCount = 8;
private _convoyStartDir = 11.689;
private _side = west;
private _maxLeadSpeed = 15;
private _vehicleSeparation = 90;
[_startEscortType, _endEscortType, _vehicleTypeArray, _convoyStartPos, _convoyEndPos, _convoyVehicleCount, _convoyStartDir, _side, _maxLeadSpeed, _vehicleSeparation] execVM "launchConvoy.sqf";

I'm currently looking into the calculatePath function to determine which direction to point the vehicles. I think that would be a better way to go than waiting for each vehicle to turn around and move out of the way.

Share this post


Link to post
Share on other sites
1 minute ago, Luft08 said:

I'm currently looking into the calculatePath function to determine which direction to point the vehicles. I think that would be a better way to go than waiting for each vehicle to turn around and move out of the way.

As i told you, ask me if you need any more help.

Good Evening!

Share this post


Link to post
Share on other sites
7 minutes ago, Smart Games said:

As i told you, ask me if you need any more help.

Good Evening!

Well, thanks for your reply. 

 

Good Morning (where I am) 🙂

  • Haha 1

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  

×