Ok that was frustrating. After a round of punching through walls, kicking windows, leaping off of rooftops into flaming hoops, (to release stress), I figured it out. ;)
What we were doing before was finding exact values of the angle to rotate to but what we should be doing is find relative angles to add to the current angle and therefore remove the issue of going from 360 to 0 in the process.
The idea is that if you are at angle 30 and you want to go to 70:
There's 2 ways about it.
You could store angle = 70 (absolute assignment)
or
alternatively, you add onto the current angle by another 40. (relative)
Both will give you an angle of 70 but working with relative solves the current problem.
So instead of:
int angle = (atan2f(-dy, dx) * (180/3.14)) -90;
let's find the relative angle like so:
CGPoint dd = target - enemyShape->body(); //handwaving to get CGPoint from body (it's psuedo-code)
float newAngle = ccpAngleSigned(ccpForAngle(angle_), dd);
angle_ += newAngle - M_PI*.5f; //add the relative angle ( note: different than storing actual angle)
What I've done was written angle functions in the latest cocos 99.1 to deal with angles of vectors. We find a new angle that is added onto the current angle. All variables with the '_' suffix are class variables.
Since we've add relative angles onto our angle_ we never work with absolute angle values so we don't run into that problem anymore.
But to smoothly transition we use lerp again
-(void)tick:(ccTime)dt
{
angleTo_ = lerpf(angle_, angleTo_, 0.05f);
}
AngleTo is the actual angle we want to use to store to the body 'enemyShape->body->a'
Ok that's pretty much it. Wheew!