Search code examples
algorithmdegreesradians

On the edge of 0 and 360 degrees (or radians -PI and PI)


I have a little demo in Processing of circular repulsion. It works great except the moment when object and its repulsor (mouse) have degrees close to 360 | 0 zone (or PI | -PI ).

or YouTube video

It's 100% because of this transition, but I don't have any idea how to overcome it. Have already played with modulo.


Solution

  • At first - I hope that you don't mix radians and degrees in comparisons.

    Your calculation

     float angleDist = abs(angle - repulsor.angle);
       and later comparison with 
     inc=45 degrees
    

    works wrong if one angle is 359 and and another is 1, for example.

    You can build some if-conditions or use expression:

    angleDist = arrcos(cos(angle - repulsor.angle)); 
    

    that treats all cases correctly