Jump to content
Sign in to follow this  
Guest BratZ

Angle question

Recommended Posts

Guest BratZ

Hey I have a question...

Lets say I have 2 soldiers and I want to make one gradually face the other.By rotating the direction manually

Or even I just want them to gradually face the same way?

I have done things like this:

Soldier1 is at 320 degrees

Soldier2 is at 190 degrees

320-190/5 would give me a part of what i needed but there is a bug at 0 degrees and 90,180,320 etc,..

This is a math problem...anyone know?

Share this post


Link to post
Share on other sites

Why not just use the dowatch command?

If for some other reason you don't want to use this, check out toadlifes trig tutorial at the OFPEC.

RED

Share this post


Link to post
Share on other sites
Guest BratZ

Yes I have that tutorial and it helps some. Guess using soldiers wasnt a good example as its for vehicles.

So dowatch wont help.

It is a formula using sin and cos to compare the angles but I get braincramps just thinking about it

But I want to match the angles gradually is the problem

Share this post


Link to post
Share on other sites

Here's the math part of it

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

; WatchHere

; Quantum

; pass in obj and 2 angles, the elevation and azimuth.

; tells the object to dowatch[x,y,z].

; I use a hardcoded magnitude just for the sake of simplicity

; [object, azimuth, elevation] exec "scripts\info\watchhere.sqs"

_obj = _this select 0

_az = _this select 1

_el = _this select 2

?(_el > 90):_el = 90

_x = 1000 * (sin _az) * (cos _el)

_y = 1000 * (cos _az) * (cos _el)

_z = 1000 * (sin _el)

_ox = getpos _obj select 0

_oy = getpos _obj select 1

_oz = getpos _obj select 2

_x = _ox + _x

_y = _oy + _y

_z = _oz + _z

_obj dowatch [_x,_y,_z]

exit

<span id='postcolor'>

You can embed the code into your script or just make this a script and call it with the params.

If you have two guys facing two directions, say Ad and Bd (for Adirection and Bdirection), just figure out how fast you want them to turn to face the direction you want.  

1) Ad = 320 Bd = 190, so guy A is looking ~NW and guy B is looking ~S.  You want them to face each other?  You need to know WHERE they are in relation to each other.  You can do this to find out the bearing angles:

Do this

_frompos  = getpos A

_topos = getpos B

then do this code

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

; break down the position arrays

_srcx = _frompos select 0

_srcy = _frompos select 1

_tgtx = _topos select 0

_tgty = _topos select 1

; some good ol fashion math

_diffx = _tgtx - _srcx

_diffy = _tgty - _srcy

_xsqrd = _diffx ^ 2

_ysqrd = _diffy ^ 2

_RangeToTarget = sqrt(_xsqrd+_ysqrd)

; determine the angle to target as THETA

; THETA = arctan(x/y) for direction

_Theta = 360 + (_diffx atan2 _diffy)

<span id='postcolor'>

_Theta = the angle from source position to target position, in your case, take guy A first as source and guy B as target to get the bearing angle from A to B.  Use B as source and A as target to get the bearing angle from B to A.

Say guy B is at an angle of 120 degrees from A's position.  You want A to turn and face B.  Since in your example you said Ad = 320, you know you need to turn either left 200 degrees or right 160 degrees (320 + 160 = 480 degrees, subtract out 360 since you passed 0 degrees, leaving 120 degrees to the right of 0 degrees).  You can feed in 480 degrees, but I generally clip the range to 0-359 for display purposes.

Ok, say you now want A to turn left those 200 degrees to face B.  Set up your loop to decrement 320 till it hits 120.  The amount you choose and the delay you use will determine how fast A turns to face B.

Feed the new value (originalAngle - decrement) to the code at the top and A will turn to face that direction.  You only have to calculate the bearing angle once, then increment/decrement the value to reach the desired final angle.

If you think this is confusing let me know.

Quantum

Share this post


Link to post
Share on other sites
Guest BratZ

Thank you very much, explaining it like that helps very much !

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  

×