Angle Problems

BlitzMax Forums/BlitzMax Beginners Area/Angle Problems

mothmanbr(Posted 2007) [#1]
I'm working in a single AI for my game, so the enemies will dodge rocks and other obstacles. So I'm making a check to see if the path ahead is blocked. If it is, it opens the angle in 20 degrees and checks in both directions, and goes to the one that is unblocked. If both are blocked, then it picks one randomly. Once it is in the new path, it calculates the angle to the player and starts to turn again.

But there's a problem: sometimes instead of turning like 45 degrees to reach the player, the enemy turns 315 degrees. Since I check if the new angle is smaller or larger than the new angle and adjust it, I believe this is a problem with angles above 360 or under 0. Should I use a mod 360 to adjust the angle? I always hear Mod should be used rarely, so is there any other way to do this?

Thanks in advance.


H&K(Posted 2007) [#2]
When did you hear Mod should be used rarely?

Anyway your problem isnt that the AI is chossing the wrong angle, but that it is turning the wrong way to get there. Its a problem between how your enemy turns, and where it wants to turn to.


mothmanbr(Posted 2007) [#3]
I have been banging my head on this for a while now, and I really can't find out a way to do this.

I'm getting two angles, the current angle, and the new angle. I get the new angle using the ATan2 function, or just adjusting a bit if the enemy finds an obstacle. Anyway, I can't find a way to make him always take the shortest turn. The closest I got was with this:

If NewAngle < 0 then NewAngle = 360 - NewAngle

If CurrentAngle - NewAngle < 0 then
Raises CurrentAngle until it reaches NewAngle
else
Lowers CurrentAngle until it reaches NewAngle
endif

Well... When the enemy is north of the player position, it doesn't work. And sometimes he will take the biggest turn, when the current angle is something 300, for example, and the new angle is 50. 300 - 50 will give you 250, which is larger than 0, so he just lowers his angle until it becomes 50, when it would be way easier to just raise it to 410.

Any suggestions? Or a better way to do this? Thanks.


H&K(Posted 2007) [#4]
If NewAngle < -180 then NewAngle = 180 - NewAngle

etc


Basicly make your angle range -180 to +180


mothmanbr(Posted 2007) [#5]
Thanks H&K, I have to do this in all the angles I use, correct?

That means I have to check if it goes over -180 or 180 everytime I change an angle, right? Should I make a method where I just send the new angle and it puts it between the range for me? Or is there an easier way?