I need to rotate a character, I would like to add the variation of the angle to its current angle. But my values are between 180 and -180. So if my character is at -179, then he goes right, it arrives at 179. how to compare that it turned by 2 degrees on his right? The goal is to say : "turn only 2 degrees on your right no matter where you are in the world". Same thing from 179 to -179 : 2 degrees to the left.



1 Answer 1


Based on the question, assuming that turning to the right is going a negative angle, and going to the left is going a positive angle.

Here is a small graphical reminder:

enter image description here

To determine what is the difference in the input, you need to constrain the delta to the same range you use:

This is the excel formula I used (row 8, with the "old" value in column A and the "new" value in colum B):

=IF((B8-A8)>180, B8-A8 - 360, IF((B8-A8)<=-180,(B8-A8)+360,B8-A8 ))

This can be translated to pseudo-code:

getInputDelta(oldAngle, newAngle)
  delta = newAngle - oldAngle
  return constrain180Deg( delta )

// Constraint any angle to the range ]-180, 180]
  newAngle = angle

  while newAngle > 180
    newAngle = newAngle - 360

  while newAngle <= -180
    newAngle = newAngle + 360

  return newAngle
  • 1
    \$\begingroup\$ @Paul I updated the answer. \$\endgroup\$
    – Vaillancourt
    Aug 25, 2015 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .