PDA

View Full Version : Trigonometry question

KaRRiLLioN
Apr 23 2003, 02:00
Trig used to be my strong point, but when it comes to scripting I have a hard time visualizing the code.

I&#39;m trying to take a chopper that is flying, and when it gets within a certain range of an invisible helo pad, land precisely on it. I&#39;m using SetVelocity right now, and I can make it land pretty well, but not perfectly on buildings, which is what I want. It&#39;s a littlebird BTW. What I&#39;ll need to do is guide it in whichever direction it needs to be in to land right on the spot.

I know how to use Setvelocity and trigonometry to make the helo go faster/slower in the direction that it is already traveling in, just not how to calculate whether to go left or right to get right on a target.

Got any ideas?

Apr 23 2003, 02:27
Code snippet from torpedo guidance script. The principle for your problem should be the same.

_tPos = position of the target
_pos = position of your vehicle
_v0 = speed (m/s)

It could be written more compact, but I havn&#39;t cleaned up the code yet. Also you should change the velocity on the z-axis to suit the helicopter needs (vertical speed is not relevant to a torpedo http://www.flashpoint1985.com/ikonboard301/iB_html/non-cgi/emoticons/wink.gif )
</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">
_tPos = getPos&#40;_target&#41;
_pos = getPos&#40;_vehicle&#41;
_v0 = 35

_dx = &#40;_tPos select 0&#41; - &#40;_pos select 0&#41;
_dy = &#40;_tPos select 1&#41; - &#40;_pos select 1&#41;
_fx= 0
?_dx&#62;0&#58;_fx=1
_uDir = 360 - &#40;atan&#40; _dy/_dx&#41; + 90 +  _fx*180&#41;
_vehicle setDir  &#40;_uDir &#41;
_vBase = &#91;sin&#40;_uDir&#41;,cos&#40;_uDir&#41;, 0&#93;
_vehicle setVelocity  &#91;&#40;_vBase select 0&#41;*_v0,&#40;_vBase select 1&#41; * _v0, -1&#93;
[/QUOTE]<span id='postcolor'>

Edit: Here you have an addition for the z-axis angle:
</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_dx = &#40;_tPos select 0&#41; - &#40;_pos select 0&#41;
_norm = sqrt&#40;_dx^2 + _dy^2&#41;
_theta = abs&#40;atan&#40;_dz/_norm&#41;&#41;
[/QUOTE]<span id='postcolor'>

_theta = z angle to x-y plane

Pennywise
Apr 25 2003, 00:29
Here is a small section of some code I wrote for a para drop script.  It flies to an exact location and deploys its cargo (troops).

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">
;=========================================================
;Move to drop area
;=========================================================

_m241 setcombatmode "BLUE"
_m241 setbehaviour "CARELESS"

;_Driver globalChat "Moving Chopper"

_m241 flyInHeight 80

_para_rsx   = _parapos select 0
_para_rsy   = _parapos select 1
_para_rsz   = _parapos select 2

#ParaLoop

_chpprpos     = getpos _m241
~.2
_chppr_rsx= _chpprpos select 0
_chppr_rsy= _chpprpos select 1
_chppr_rsz= _chpprpos select 2
~.2
_diffx     = abs (_chppr_rsx-_para_rsx)
_diffy     = abs (_chppr_rsy-_para_rsy)
~.2

?((_diffx<=_disFromDrop) and (_diffy<=_disFromDrop)):goto "DeployTroops"
[/QUOTE]<span id='postcolor'>

This is another script I wrote to control the speed of the F18 hornet when the AI fly it.  It keeps the AI from flying at insane speeds all the time.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">
_obj       = _this select 0
_Soldier   = _this select 1
_spdMode   = speedMode _obj

;=========================================================
;Speed Offset
;=========================================================
_SpdOffset = 1

;=========================================================
;Fleight Altitude
;=========================================================
_Altitude  = 200

;=========================================================
;Speeds
;=========================================================
_limited = 65
_normal  = 125

;=========================================================
;Speed Control for AI
;=========================================================
_obj flyinheight _Altitude

;=========================================================
;Speed Control for AI
;=========================================================

?(agvdebugCustomAddon):_Soldier globalChat Format["AI Speed Mode: %1", _spdMode]

_tempVel=velocity _obj
_vX=_tempVel select 0; _vY=_tempVel select 1; _vZ=_tempVel select 2

_spdMode=speedMode _obj

?(_spdMode == "LIMITED" and (_vX > _limited)):_vX=(_vX/(abs _vX))*(_vX-_SpdOffset)
?(_spdMode == "LIMITED" and (_vY > _limited)):_vY=(_vY/(abs _vY))*(_vY-_SpdOffset)
?(_spdMode == "LIMITED" and (_vZ > _limited)):_vZ=(_vZ/(abs _vZ))*(_vZ-_SpdOffset)

?(_spdMode == "NORMAL" and (_vX > _normal)):_vX=(_vX/(abs _vX))*(_vX-_SpdOffset)
?(_spdMode == "NORMAL" and (_vY > _normal)):_vY=(_vY/(abs _vY))*(_vY-_SpdOffset)
?(_spdMode == "NORMAL" and (_vZ > _normal)):_vZ=(_vZ/(abs _vZ))*(_vZ-_SpdOffset)

_obj setVelocity [_vX, _vY, _vZ]

?(&#33;(_Soldier in _obj) or &#33;(alive _Soldier)):goto "Exit"

~.03

#Exit
?(agvdebugCustomAddon):_Soldier globalChat "AI Speed Offset Exit"
exit
[/QUOTE]<span id='postcolor'>

If you combine the two, Im sure you will come up exactly with what you need.  I can send you the actual scripts if you want.  You know my contacts:)

Hope it helps ya out.

Kinnon
Apr 27 2003, 18:55
If you don&#39;t mind using someone elses code, snypir of www.ofpec.com has written a script that fully encapsulates everything you need.

I FULLY DONT SUPPORT THE OIL WAR

Apr 28 2003, 00:53
</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Pennywise @<hidden> 25 April 2003,01:29)</td></tr><tr><td id="QUOTE">Here is a small section of some code I wrote for a para drop script.  It flies to an exact location and deploys its cargo (troops).

If you combine the two, Im sure you will come up exactly with what you need.  I can send you the actual scripts if you want.  You know my contacts:)[/QUOTE]<span id='postcolor'>
The paradrop script is from what I understand not what he is looking for since they do not set the direction of the aircraft (setDir) towards the target.

Depending on the relative position of the target both your chopper will fly sideways or even backwards http://www.flashpoint1985.com/ikonboard301/iB_html/non-cgi/emoticons/biggrin.gif

KaRRiLLioN
Apr 28 2003, 14:45
The torpedo calculations seem to work fairly well, although I haven&#39;t incorporated the Z axis into it. The AI seems to get the chopper fairly close to where I want it to go, so what I&#39;m doing now is letting the AI get it close and then I nudge it with a setvelocity and that seems to put it where I want it.

Thanks&#33;