OFPEC Forum

Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting General => Topic started by: Wolfrug on 29 Oct 2010, 21:08:52

Title: [SOLVED] Math help! Moving towards centre of circle
Post by: Wolfrug on 29 Oct 2010, 21:08:52
For you math wizards!

Scenario:

A person steps outside a circle, and is subsequently teleported back inside it (activated in a trigger's On Deactivation field, basically). This could be done simply by moving the unit, say, 5 meters in the direction of the centre of the circle (getPos TriggerName). What math wizardy would I need to perform to be able to do this rather simple operation? I suck at math, so all I basically need is the function, explanations will be useless :-[ (I bet it involves tan and sin and all those scary buggers).

Tried to search for this, but failed. So thanks!

Wolfrug out.
Title: Re: Math help! Moving towards centre of circle
Post by: laggy on 29 Oct 2010, 23:39:11
I suck at math as well  :-[

But, why not run a script on the unit measuring:

Code: [Select]
while {true} do
      {
      if (myUnit distance myCenter > 95) then {_spot = getPos myUnit};
      if (myUnit distance myCenter > 100) then {myUnit setPos _spot};
      sleep 1;
      }

Sorry if I got it completely wrong... It is late on a Friday night :P

I guess that could make the player/unit slowly "crawl" out of the wanted zone, if really persistant.
Title: Re: Math help! Moving towards centre of circle
Post by: JamesF1 on 30 Oct 2010, 00:23:49
It's too late in the day for me to do math (it doesn't come as naturally to me as I'd like ;)), but you could make use of CBA and BIS functions thus (if in doubt, leave the real math to people who can do it :D):
Code: [Select]
_initial = getPosATL player;
_trigger = getPosATL yourTriggerName;
_direction = [_initial, _trigger] call BIS_fnc_relativeDirTo;
_vector = [5, _direction, 0] call CBA_fnc_polar2vect;
_position = [_initial, _vector] call CBA_fnc_vectAdd;
player setPosATL _position;
player setDir _direction;
I've not tested that, but that should take them 5 metres towards the centre.  If not, then please do blame my late night attempts at comprehending questions and providing answers ;)
Title: Re: Math help! Moving towards centre of circle
Post by: Mr.Peanut on 30 Oct 2010, 00:35:49
Code: [Select]
_r = 5;
_xt = (getPos _trigger) select 0;
_yt = (getPos _trigger) select 1;
_xu = (getPos _unit) select 0;
_yu = (getPos _unit) select 1;
_dx = _xu - _xt;
_dy = _yu - _yt;
_dir = _dy atan2 _dx;
_dx = _dx - _r * cos _dir;
_dy = _dy - _r * sin _dir;
_unit setPos (_xt + _dx, _yt + _dy, 0);
Title: Re: Math help! Moving towards centre of circle
Post by: Wolfrug on 09 Nov 2010, 14:52:28
I forgot to say thank you to Mr. Peanut: thank you! That solved it, no problems whatsoever. Now all I'd need would be a command to setdir the player in the exact direction of the centre of the trigger, but that's really a secondary matter after all ;)

Thanks again everyone!

Wolfrug out.
Title: Re: [SOLVED] Math help! Moving towards centre of circle
Post by: bedges on 09 Nov 2010, 15:08:02
You may find that _dir in Peanut's example above is the angle you seek.