
1. Re: Logic error in rotating a bot towards a target (switch from 180 to 180 and vice versa causes error)
kglad Feb 12, 2014 12:08 PM (in response to prodigalmaster)convert all angles to positive angles between 0 and 360 (if degrees) or 2pi (if radians):
if degrees: angle=(angle+360)%360;
if radians: angle+=(angle+2*Math.PI)%(2*Math.PI);
and if efficiency is an issue there are more efficient ways to do this.

2. Re: Logic error in rotating a bot towards a target (switch from 180 to 180 and vice versa causes error)
prodigalmaster Feb 12, 2014 5:49 PM (in response to kglad)Ok I tried this and got the values going between 0 and 360 but the same type of error occurs because it is not to do with negative/positive numbers but the difference between the two angles when one reaches the 360 or 0 threshold.
The following outputs were traced where the first number is the target angle in degrees and the second is the bot angles. Each frame the bot angle is either incremented or decremented depending on whether the target angle is greater than or less than the bot angle.
340, 339
339, 344
339, 339
341, 334
344, 339
350, 344
359, 349
10, 354
23, 349
36, 344
41, 339
44, 334
46, 329
47, 324
47, 319
The problem is when the target angle changes from ~360 to ~0 as you can see the bot angle then begins to decrement towards the 10 and then 23 and then 36 instead of increasing to 360 and then increasing to 10 and 23.... this means the bot turns in the wrong direction when the other direction is actually a shorter amount of degrees to turn.
there is likely a way but I can't figure it out.

3. Re: Logic error in rotating a bot towards a target (switch from 180 to 180 and vice versa causes error)
kglad Feb 12, 2014 8:52 PM (in response to prodigalmaster)use:
public function rotateTo(xTarget, yTarget):Void { targetRotation = Math.atan2(yTargetthis.yPos, xTargetthis.xPos)*180/Math.PI; if (this.angleRottargetRotation<0) { turnBot(5); } else { turnBot(5); } }

4. Re: Logic error in rotating a bot towards a target (switch from 180 to 180 and vice versa causes error)
prodigalmaster Feb 12, 2014 10:12 PM (in response to kglad)Yep that is slightly more efficient but is the same code as what I used.
It still gives the logic error.
It doesn't fully work. I've explained the problem and cause a couple of times in my above posts.
It's a pretty difficult problem to solve yet seems so simple. Let's say angleRot = 345 and targetRotation = 355, this would make 345355 == 10 which is less than 0 therefore making it turnBot(5). But if targetRotation went above 360 and therefore to >=0 it would make 3450 == 345 which is greater than 0 therefore making it turnBot(5) which basically means angleRot = 5 but instead it should keep increasing (unless angleRot < targetRotation+180 ????) let me check.
It might work if the angles were not constrained to 360 degrees or the radian equilavent and could be incremented/decremented infinitly but obviously this would cause lots of bugs.
Thanks.

5. Re: Logic error in rotating a bot towards a target (switch from 180 to 180 and vice versa causes error)
prodigalmaster Feb 12, 2014 10:26 PM (in response to prodigalmaster)Hey I got it to work finally:
public function rotateTo(xTarget, yTarget):Void {
targetRotation = Math.atan2((yTargetthis.yPos), xTargetthis.xPos)*180/Math.PI;
if (this.angleRottargetRotation>180) {
this.angleRot = 360;
} else if (this.angleRottargetRotation<180) {
this.angleRot += 360;
}
if (this.angleRottargetRotation<0) {
turnBot(5);
} else {
turnBot(5);
}
}
so basically I added a couple of if statements before the main business.... I don't really have a coherent explanation for the fix but it works!