For a piece of code I am writing to smooth out movements I need to calculate the average angle over the past 5 recorded angles given (used to give directionality to an object)
This can be achieved quite simply by calculating the median of the 5 previous angles.
However, the angles given range from 0 degrees to 360 degrees so we get an immediate issue here.
Once you move over 360 degrees the angle is reset back to 0 so if we was to send 0 to the array of previous angles then the following would happen:
(355 + 359 + 360 + 360 + 1) / 5 = 287
Now obviously 287 is completely the wrong angle and gives an abnormal movement once we cross this 360 degree border.
I've tried checking if the previous values are on the 360 side or the 0 side then adjusting the new value accordingly but we get an issue with 1; the performance (there is a very short update time before it effects the user interface) and 2; when we get to 720 it will have to keep looping around again.
I don't have a very good background with maths so I thought I would ask here as my last resort but is there a way/formula I can calculate the average with the 360 to 0 gap in mind and give a result on the correct side of this instead of just giving a false value?
Thanks for looking, please let me know if i need to provide any more information to help you :)