Jump to content
johnnyboy

[Release] JBOY Calculate Intermediate Position function

Recommended Posts

Here's yet another byproduct from Property of Mabunga mission:  JBOY_CALC_INTERMEDIATE_POS.sqf  (yes, I spared you from a video this time!)

 

Purpose:  Calculate position X meters from Position A in the direction towards Position B

 

Why:  I wanted the AI lead CSAT and NATO teams to execute different search patterns to find a pirate camp, so I put in markers 200 meters apart over the entire swamp area.  I made 5 different search patterns for the AI by ordering these markers differently in 5 different arrays.   One of the 5 search pattern arrays is randomly chosen for the AI team, and they move from marker to marker.

 

Problem:  If the markers are too far apart, AI navigation may make them choose to take a road or some other deviation, rather move straight to the marker.

 

Solution:  If distance to next marker is greater than 50 meters, then calculate an intermediate position 50 meters between current position and destination marker, and DoMove the units to the intermediate position.  AI would then go more directly toward next marker, because they were moving in 50 meter segments.  Note that I also randomly tweak the X,Y values of the calculated intermediate position so AI less robot-like in moving to next position.

//function name:  JBOY_CALC_INTERMEDIATE_POS.sqf
//Put this in init:   JOY_CALC_INTERMEDIATE_POS = compile preprocessFileLineNumbers "Scripts\JBOY_CALC_INTERMEDIATE_POS.sqf";
//Example call:   _newPos = [dude, destPosition, 20] call CALC_INTERMEDIATE_POS;  
// 
private ["_unit","_dest","_segmentDistance"];

_unit = _this select 0;    
_dest = _this select 1;  
_segmentDistance = _this select 2;

_segmentDest = _dest;

if ((_unit distance _dest) > _segmentDistance) then
{
    _compassDir = [_unit, _dest] call BIS_fnc_dirTo;
    _newX = (getPos _unit select 0) + (sin _compassDir * _segmentDistance);
    _newY = (getPos _unit select 1) + (cos _compassDir * _segmentDistance);

    _newPos = [_newX, _newY, 0];
    
    _segmentDest = [(_newPos select 0)+(5-random(10)), (_newPos select 1)+(5-random(10)), 0];
};

_segmentDest
  • Like 2

Share this post


Link to post
Share on other sites

That's a clever solution to the waypoint problem. But looking at the script, it seems you may not be aware of a couple of very handy commands.
Mainly getPos and getDir, and their alternative syntaxes which you could use like this:

if ((_unit distance _dest) > _segmentDistance) then
{
    _compassDir = _unit getDir _dest;
    _newPos = _unit getPos [_segmentDistance,_compassDir];
    _segmentDest = _newPos vectorAdd [5 - random 10, 5 - random 10,0];
};

Which one you use in this case doesn't really matter, but I'm sure you'd find these useful in some scripts.

  • Like 3

Share this post


Link to post
Share on other sites

@Greenfist:  Thanks dude!  I was definitely unaware of those handy commands.  They are now in my bag of tricks thanks to you!

Share this post


Link to post
Share on other sites

wow! is that alternative syntax stuff recent? never seen this before. thanks a lot for sharing!

 

also cool script jb!

Share this post


Link to post
Share on other sites

wow! is that alternative syntax stuff recent? never seen this before. thanks a lot for sharing!

Pretty recent, from 1.56 in February. They also added new object related commands getRelPos & getRelDir

  • Like 2

Share this post


Link to post
Share on other sites

Pretty recent, from 1.56 in February. They also added new object related commands getRelPos & getRelDir

 

damn!. thank you man. i'll have to go replace some stuff in my code right away lol.

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

×