5 Replies Latest reply on Feb 12, 2014 10:26 PM by prodigalmaster

    Logic error in rotating a bot towards a target (switch from -180 to 180 and vice versa causes error)

    prodigalmaster Level 2

      Hi,

      I am making a Bot class. This function rotateTo makes the bot rotate towards some coordinates, xTarget and yTarget.

       

      There are two targetRadians lines because it is easier to read for me.

       

      The target radians shows the angle of the target from the bot where the bot angle is this.angleRot.

       

      if the bot-angle minus the target-angle (it says in radians but is actually in degrees) is less than 0 it turns right and if it is greater than or equal to 0 then it turns left.

       

      public function rotateTo(xTarget, yTarget):Void {
                          targetRadians = Math.atan2(yTarget-this.yPos, xTarget-this.xPos);
                          targetRadians = Math.round(radiansToDegrees(targetRadians));
                          trace(targetRadians);
                                    if (this.angleRot-targetRadians<0) {
                                              turnBot(5);
                                    } else {
                                              turnBot(-5);
                                    }
                }
      

       

      This basically works. The bot rotates towards the target at 5 degrees per frame.

       

      The angle setup is that if the target x, y is below the bot x, y, the target angle is about 90, if it's to the right its about 0, if its above it's about -90 but as the angle is decreased to -180 from above to the left of the bot, when it goes below the bot (and to the left) the target angle switches to 180.

       

      So something like: with the bot in the middle and the target being located at one of the numbers;

                -90
      -180               -1
       180               1
                 90
      

       

      The fact the target angle switches from -180 to 180 or 180 to -180 confuses the bot and the bot decides to turn ALL the way the opposite direction BECAUSE;

      if the bot angle is 160 and target angle is 175, the bot angle increases because target is greater than bot angle. If the target angle changes to -180, bot angle decreases even though it's more efficient to change from 160 to -180 through increasing the bot angle as at 180 it changes to -180. I just cannot work out the logic to use to make this work though.

       

      Any suggestions? Thanks.

        • 1. Re: Logic error in rotating a bot towards a target (switch from -180 to 180 and vice versa causes error)
          kglad Adobe Community Professional & MVP

          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 Level 2

            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 Adobe Community Professional & MVP

              use:

               

               

              public function rotateTo(xTarget, yTarget):Void {
                                  targetRotation = Math.atan2(yTarget-this.yPos, xTarget-this.xPos)*180/Math.PI;
                              
                                            if (this.angleRot-targetRotation<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 Level 2

                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 345-355 == -10 which is less than 0 therefore making it turnBot(5). But if targetRotation went above 360 and therefore to >=0  it would make 345-0 == 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 Level 2

                  Hey I got it to work finally:

                   

                   

                  public function rotateTo(xTarget, yTarget):Void {

                                      targetRotation = Math.atan2((yTarget-this.yPos), xTarget-this.xPos)*180/Math.PI;

                                      if (this.angleRot-targetRotation>180) {

                                                this.angleRot -= 360;

                                      } else if (this.angleRot-targetRotation<-180) {

                                                this.angleRot += 360;

                                      }

                                      if (this.angleRot-targetRotation<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!