Getting a bodies angle in degrees

Hi,

Having a problem with some box2d returns for a game on the iPhone.

For the actors in the game world, I need to know their degree of rotation (0-360) and am using

Code: Select all

   float angle = RADIANS_TO_DEGREES(Body->GetAngle());


(the RADIANS_TO_DEGREES is #define RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) / M_PI * 180.0) )

This is returning unexpected results. I start at 0, and as the body rotates clockwise, the values increase to 360 with one rotation. From then I pass this and get 361,362,363, etc. Anticlockwise I get -1, -2, and so forth.

How to I return a strict value from 0-360, am I missing something obvious?

Thanks in advance.
the angle box2d pass aren't -2*PI<=X<=2*PI, so or you normalize this value or you normalize your elaborated angle.
a bruteforce algorithm can be:

Code: Select all

float angle = RADIANS_TO_DEGREES(Body->GetAngle());
while (angle <= 0){
   angle += 360;
}
while (angle > 360){
   angle -= 360;
}
printf("The normalized angle is: %d\n", angle)

This will give you value from 1 to 360, if you remove the equal form the first while and put it in the second you will get value form 0 to 359, because 360°==0°
Thanks for the reply. It works a treat.

It would be better still if it was possible to use a formula rather than loops, though I am sure the loops are ok now, if you keep rotating an object, the degrees returned can become rather large, increasing the loops.
this works with radians and is in java, but i think is extra-optimized because is used in a game (robocode). I think it's easy translate to degree or C.
Here the link to the full class (it is under the terms of the Common Public License v1.0)
http://www.java2s.com/Code/Java/Development-Class/Normalizesanangletoarelativeangle.htm

Code: Select all

import static java.lang.Math.PI;
import java.util.Random;


/**
 * Utility class that provide methods for normalizing angles.
 *
 * @author Mathew A. Nelson (original)
 * @author Flemming N. Larsen (contributor)
 */

public class Utils {
  private final static double TWO_PI = 2 * PI;
  /**
   * Normalizes an angle to a relative angle.
   * The normalized angle will be in the range from -PI to PI, where PI
   * itself is not included.
   *
   * @param angle the angle to normalize
   * @return the normalized angle that will be in the range of [-PI,PI[
   */
  public static double normalRelativeAngle(double angle) {
    return (angle %= TWO_PI) >= 0 ? (angle < PI) ? angle : angle - TWO_PI : (angle >= -PI) ? angle : angle + TWO_PI;
  }
}
Just to save any other noob a few minutes this is that snippet in Actionscript 3 for the Box2DFlash version.

Code: Select all

     private  static var TWO_PI:Number = 2 * Math.PI;
  /**
   * Normalizes an angle to a relative angle.
   * The normalized angle will be in the range from -PI to PI, where PI
   * itself is not included.
   *
   * @param angle the angle to normalize
   * @return the normalized angle that will be in the range of [-PI,PI[
   */
  public static function normalRelativeAngle( angle:Number) {
    return ((angle %= TWO_PI) >= 0 ? (angle < Math.PI) ? angle : angle - TWO_PI : (angle >= -Math.PI) ? angle : angle + TWO_PI)* (180 / Math.PI);
  }
   
   
   



I made it so it returns values between -180 and 180 to keep things simple and call it like:

Code: Select all

numAngleRight = yourb2body.GetAngle(); //get its angle
   numAngleRight = normalRelativeAngle(numAngleRight);
         


Dunno if I did it perfectly but it's working great and just wanted to thank lestofante for taking the trouble as my game would be pretty jittery otherwise. : )

Original thread: https://box2d.org/forum/viewtopic.php?t=4726