Animating a compass in Animation nodes

I'm new to the stack exchange, not so new to Blender, but new(ish) to animation nodes.

I have built a set of nodes (and some python) that reads GPS info from a CSV file and uses this data to create animated gauges to overlay on my flying videos. I'm having a problem with the compass. Whenever the heading goes through the rollover from (say) 359 deg to (say) 1 degree, the compass rotates all the way backwards through 180 deg to reach 1 deg. It does this also when transiting the rollover in the other direction. What I want to do is to have the compass smoothly transit through the rollover zone in the proper direction. I was wondering if anyone has solved a problem like this (I imagine this may happen when animating bones etc) and what is a useful approach for animation nodes. I though of identifying the quadrants of the compass as 1,2,3,4 going in clockwise fashion starting with the 0-90 quadrant as Q1 and ending with the 270-360 quadrant as Q4. Then somehow detecting the change from Q1 to Q4 or from Q4 to Q1 to change how the next position is calculated. Beyond that I'm a bit stumped and can't get my head around how to implement it in AN. Thanks in advance for any help offered. Edit: For clarification; The problem really lies in my interpolations between GPS reporting points. If one GPS report gives a heading of 359 deg and the next one gives a heading of 1 deg, the maths I'm doing for the interpolation between the two readings is what is taking the long way round the compass, but I'm not sure how to cure this. Scott...

• Hello, without seeing a bit more about the node tree and such, it's hard to troubleshoot. Euler rotations are tricky when approaching full-circle turns, that's where Quaternions can help. Suggest reading about it or posting your node tree for reference – Gorgious Jun 4 at 9:35
• Thanks: I thought of Quaternions and read a bit about them on Wiki but the maths and theory is beyond my tiny brain. I will see about posting my node tree, but I'm a little wary that I'll be subject to ridicule for my clumsy efforts. – FlyingAround Jun 5 at 5:46

if i understood you right, you want the shortest angle (difference) between two angles.

There is a node for that: vector angle

which does this:

Description

This node takes two vectors and computes two things:

The shortest angle between the two vectors in radian. The directed angle between the two vectors in a form of rotation quaternion.

https://docs.animation-nodes.com/documentation/nodes/vector/vector_angle/

• Thank you for that. I am new to AN so this could be helpful. I don't fully understand it yet so I will give it a bit of a run in a test situation. Also, a bit of clarification that I thought of after I posted the above. The reporting interval from the GPS is 1 second, so I do interpolation between the samples. It is in the interpolations where I am coming unstuck. The maths is taking the compass around the long way because the maths is not taking account of the wraparound of the compass from 360 to 0. I will try out the node you suggested . Again, thanks for your help. – FlyingAround Jun 5 at 5:33

If your input data is a sequence of angles, let's say b_1, b_2, b_3 ... b_n and the actual values that your compass has are a_1, a_2, a_3 ... a_n, maybe you can try this:

1. Calculate the difference using this python expression:

DM_n = (b_n - b_(n-1) + 180) % 360 - 360 (% is modulo division)

For small variations this will give you the variation that you should apply respect the previous value

2. Sum DM_n to the previous value of your Blender Compass, a_n

a_n = a_(n-1) + DM_n

The two sequences (a_n and b_n) will point to the same angle but a_n will be larger than 360 or lower than 0 depending on the turns of your data sequence

I don't know if this is what you were asking for.

• Thanks for that. I don't know if it is what will do the job, but I will experiment with that. What you suggest might be the solution as I'm doing interpolations between samples that are 1 sec apart (from the GPS). Thanks for your help. – FlyingAround Jun 5 at 5:39

OK, so I solved this using some boolean logic. I used Compare Numbers nodes to detect if the compass is going to cross the 360/0 boundary. If that is the case, I do different types of calculations to arrive at the number of degrees to rotate and the direction (going up or down in degrees) rather than the numerical difference between the two angles. If I'm not crossing the boundary, I just work with the numerical difference in the angles. The method I use is clunky, but it works. For future reference, if anyone wants to know the nodes setup I used, PM me directly.