title: How to normalize an angle between -π and π Java
body: <p>I'm having troubles getting a normalized float to smoothly rotate some sprites.  I'm using interpolation to rotate my sprites.  At a certain point in the rotation the sprite will jump, at the same spot every time.  </p>
  
  <pre><code>name.angle = (name.getBody().getTransform().getRotation() * alpha + name.prevAngle * (1.0f - alpha));
  </code></pre>
  
  <p>I've looked online and found a couple ways to normalize an angle between -pi and +pi but I can't get them to work in my situation.</p>
  
  <p>The following doesn't work</p>
  
  <pre><code>if (name.angle &gt; Math.PI)
      name.angle += 2 * Math.PI;
  else if (name.angle &lt; -Math.PI)
      name.angle -= 2 * Math.PI;
  </code></pre>
  
  <p>The following does work</p>
  
  <pre><code>name.angle = name.angle &lt; 0 ? MathUtils.PI2 - (-name.angle % MathUtils.PI2) : name.angle % MathUtils.PI2;
  </code></pre>