Hey there
For sure this is very trivial math stuff for some of you.
But: When I rotate a view, lets say starting at 0 degrees and rotating forward 0.1, 1, 10, 100, 150, 160, 170, 179, 179,999, and keeping on rotating in the same direction, this happens, to say it loud and clear: BANG !!!! BAAAAAAAAANNNNNGGGG!!!! -179,9999, -170, -150, -50, 0, 50, 100, 170, 179,99, and again: BBBBAAAANNNGGG!!! -179,999, -170, -100, and so on... for sure you know what I mean ;-)
Imagine you driving on a road and suddenly your miles-o-meter jumps negative like that. You wold freak out, right?
And you know what? This is VERY bad for my algorithm. I have no idea how to resolve this other than checking in if-else-blocks if my value suddenly swapped over. Something tells me I have to look at some math functions like sinus waves and other stuff. But my math knowledge sucks to the highest degree.
How can I solve this out? I try to calculate distances between angles, i.e. I have two views where one resides on the other, and both are rotated. And I want to calculate the overall rotation of both views. This little but mad thingy destroys my calculations as soon as values swap over suddenly from -179,99999 to 179,99999. Also I don't know if a 180 exists or if things swap somewhere at fabsf(179,999999999999999999999) if you know what I mean.
For example: What would be -170 degrees minus 50 degrees? Well, I think -220 degrees. But instead when I rotate -170 - 50 I end up getting this value: 40 degrees.
How can I receive what I expect, the -220 instead of 40, without any kind of unsecure swapping-if-else logic? Sometimes my selfmade-swapper works, but sometimes it doesnt due to math incprecisions.
Edit: I calculate the angles from the transform.rotation.z property of the view's layers.