add the variation of the angle to its current angle: trouble with 180 and -180 - Game Development Stack Exchange most recent 30 from gamedev.stackexchange.com 2019-09-23T09:50:58Z https://gamedev.stackexchange.com/feeds/question/106232 https://creativecommons.org/licenses/by-sa/4.0/rdf https://gamedev.stackexchange.com/q/106232 1 add the variation of the angle to its current angle: trouble with 180 and -180 Paul https://gamedev.stackexchange.com/users/15588 2015-08-25T11:45:32Z 2015-08-25T14:11:28Z <p>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.</p> <p>Thanks</p> https://gamedev.stackexchange.com/questions/106232/add-the-variation-of-the-angle-to-its-current-angle-trouble-with-180-and-180/106241#106241 0 Answer by Alexandre Vaillancourt for add the variation of the angle to its current angle: trouble with 180 and -180 Alexandre Vaillancourt https://gamedev.stackexchange.com/users/40264 2015-08-25T13:01:24Z 2015-08-25T14:11:28Z <p>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.</p> <p>Here is a small graphical reminder:</p> <p><a href="https://i.stack.imgur.com/mv8pt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mv8pt.jpg" alt="enter image description here"></a></p> <p>To determine what is the difference in the input, you need to constrain the delta to the same range you use:</p> <p>This is the excel formula I used (row 8, with the "old" value in column A and the "new" value in colum B):</p> <pre><code>=IF((B8-A8)&gt;180, B8-A8 - 360, IF((B8-A8)&lt;=-180,(B8-A8)+360,B8-A8 )) </code></pre> <p>This can be translated to pseudo-code:</p> <pre><code>getInputDelta(oldAngle, newAngle) delta = newAngle - oldAngle return constrain180Deg( delta ) // Constraint any angle to the range ]-180, 180] constrain180Deg(angle) newAngle = angle while newAngle &gt; 180 newAngle = newAngle - 360 while newAngle &lt;= -180 newAngle = newAngle + 360 return newAngle </code></pre>