okay, I'm trying to make some chase AI that acts something like a homing missle.
Heres the core code:
int x2=mouse.x*10;
int y2=mouse.y*10;
int a=x2-missle.x;
int b=y2-missle.y;
int c=sqrt(pow(a,2)+pow(b,2));
int ang=abs(atan2(b,a));
if(b<0) ang=0-ang;
missleAng-=missleAng-ang/10;
missle.x= 100 *cos(missleAng) /65536 +missle.x;
missle.y= 100 *sin(missleAng) /65536 +missle.y;
which is kept in the main game loop.
the setup should produce a predator following prey:
but, when the prey moves... I want it to do this:
which is the reason for the /10 in the missleAng-=missleAng-ang/10; equation. The idea is that we have the angle of the prey to predator stored in ang and the angle at which the predator is moving is the missleAng.
If they were the same it would be impossible to dodge the predator and it would feel unnatural, so I want the predator to turn its angle into the correct angle, but slowly. (with this in mind, I know that it isnt being controlled by timer yet... I'll get to that later)
this works.
soooooo... the problem with this is....
This is how the verge angle grid is setup as I have it now. Its kinda weird but it works most the time.
What happens when I run the game is that it works just like I want it to until the angle the predator is moving in hits around the 180 degree point because suddenly ang= -170 but missleang is at +170 and in math, -170 is not right next to +170 so it starts going all the way down to 0 and down to -170... but then by this point the desired angle is at +170 and the current angle is -170... so its back teh other way again! over and over untill the predator has run off the screen into oblivion!!!!... ahem...
so that looks like this(taken directly from verge with some of my masterful markups!):
so yeah... I dont know how I'm to fix this... can i get some help?
one idea I had was if when the prey crosses the y-axis, the grid would flip around so that 0 is where the 180 usually is... but such a concept is beyond my comprehension in application right now. >_<