So, after some mulling, I decided to use delta-values instead of actual values. I’m writing this in case anyone else wants to achieve the same visual effect, which is: rotate a node, from one h/p/r to another h/p/r and to consistently use the longest or shortest route within an interpolation between the two sets of h/p/r without suffering from the changes that come with shifting ranges when using euler angles, e.g at times, whenever getH() is invoked, it might return -160, other times, it returns 200, making it unreliable when doing interpolations.
Firstly, the neutral hpr state of the nodepath needs to be saved; this is the hpr state before any interpolation is done on the nodepath:
Then, whenever the h or p or r is changed, maintain a set of deltas that indicates that change. There should be a set for positive changes and a set for negative changes:
Then, whenever you rotate left or right by say, 90 degrees, you just update the values:
#Turned right by 90:
#For reliable results, always clamp the values between 0 and 359:
#Whenever you update the positive, you must updated the negative and vice versa:
That way, at any given time, we get how many degrees the nodepath has turned from its neutral hpr state.
When it comes to interpolation, a snapshot of these deltas will be needed at different time slots. So if you want to interpolate between only two time slots for example, then you’d need two sets of both positive and negative deltas:
#For demonstration purposes, a dummy object that stores time and delta data:
#store the information inside it:
#get the hpr deltas at the starting point:
#Get a snapshot of the data at the next point, where the hpr deltas have changed:
So, now that the data is stored, time to move between it:
#First, set the nodePath to its neutral hpr state:
#then, find out how much time has passed since the interpolation started:
#the rest of the calculations:
#We will need to get the difference between the deltas at the different snapshots,
#Then we pick the least difference to use for turning, two sets of differences, positive and negative:
#Now, pick the lesser delta and indicate whether it was the delta from the positive or negative value picked:
#of course, if you want the long way, just flip and pick the greater delta.
#Now, we have our delta to apply, either one that results in a shorter rotation arch or a longer rotation arch, just apply it, based on the current value of the slider:
#Make sure to add the delta at the first keyframe, since it's the starting point of the interpolation:
The slider-value referenced could just be some variable somehow provided to the interpolation method. The slider-value could be increased periodically within a task or sequence that is called at whatever rate you want it to be called. Or it could be attached to a directSlider object and manipulated by the end-user, etc.
In any case, the above is an example of how to use deltas from a neutral state to turn a nodepath and ensure it always takes either the shortest or longest rotation arch during its transform: store a neutral hpr state, maintain a positive and negative delta-variable that are updated appropriately whenever the nodepath’s hpr changes, store the values from the positive and negative delta-variables at whatever point you want involved in an interpolation, making sure to store some corresponding time value for each stored point, then, move between deltas smoothly and apply the current deltas in the manner indicated above to turn either the short way, or the long way.
I don’t know if this long post will help anyone, but I hope it does in some way. If anything I said is unclear, just tell me and I’ll try to clarify what I mean.