Angles - rotating towards new angle?

BlitzMax Forums/BlitzMax Beginners Area/Angles - rotating towards new angle?

EOF(Posted 2010) [#1]
** EDIT. See new issue below regarding "gradual" catchup to target angle



I'm trying to figure out a simple routine to get an object to take the shortest directional turn towards a new angle (in degrees)



The directions are:

        0
        |
270 ----+---- 90
        |
       180


Lets say, the objects current angle is 20
Now I want it to turn slowly to 40 degrees

My basic turn routine is:
 currang:+(newang-currang)/10.0


However, if the object is pointing say 10 degrees and the new angle is 300 degrees then the object should turn anti-clockwise
The routine however turns the object clockwise


Jesse(Posted 2010) [#2]
Check out my chaser missile demo in my signature download link. It should give you an idea on how to do that.


Warpy(Posted 2010) [#3]
Have a look at my post in this thread, specifically the TurnTo function. (AngleDifference is further up the page, in Streaksy's code)


EOF(Posted 2010) [#4]
Thanks both. I am up and running


EOF(Posted 2010) [#5]
Just to revist this one

What I am now trying to achieve now is a 'gradual' catchup to where the target angle is. See example below.

Move the mouse around and the yellow line will gradually turn to where the target angle is pointing

One problem though, when the target angle sweeps past 0/360 degrees (TOP of screen in the example) the chasing angle "jumps" quickly to the target angle rather than maintaing a gradual catchup

I realise this is because the difference between the two angles is huge when the 0/360 point is past

If target angle=10 and currentangle=20 then the difference is 10. This is fine
If target angle=10 and currentangle=350 I would like the difference to be 20 (instead of 340). Any ideas how to incorporate this?

I am using the TurnTo() function to control the catchup speed:




Warpy(Posted 2010) [#6]
That's what the AngleDifference function was for in my/Streaksy's code.


EOF(Posted 2010) [#7]
*EDIT - Fixed it now (I think!)

Yep, I incorporated that math directly into the TurnTo() function (since I wont be using it elsewhere)

I have added a bit of code to check the diff as well as wrap the ang value after it was returned from the TurnTo() function. The ang was going negative if you allowed the chaser to continually go counter-clockwise. Likewise, it was going past 360 after cycling continuosly clockwise:




Warpy(Posted 2010) [#8]
What I meant was, if you used AngleDifference to work out diff# in the main bit of code, you wouldn't have this problem at all. The 'Mod 360' bit is what gets rid of multiples of turns.


EOF(Posted 2010) [#9]
Ahh, I get you. I will have a play at fine-tuning this beast. Thanks for the help!


Finished article for now: