The Problem
How would one interpolate between two given angles, given a certain time delta, so that the simulated motion from rotation A or rotation B would take a similar amount of time when the algorithm is ran at different frequencies (without a fixed time step dependency).
Potential Solution
I have been using the following C# code to do this kind of interpolation between two points. It solves the differential for the situation:
Vector3 SmoothLerpVector3(Vector3 x0, Vector3 y0, Vector3 yt, double t, float k)
{
// x0 = current position
// y0 = last target position
// yt = current target position
// t = time delta between last and current target positions
// k = damping
Vector3 value = x0;
if (t > 0)
{
Vector3 f = x0 - y0 + (yt - y0) / (k * (float)t);
value = yt - (yt - y0) / (k * (float)t) + f * (float)Math.Exp(-k * t);
}
return value;
}
This code is usable for 2D coordinates by having the Z
coordinate of the Vector3
set as 0.
The "last" and "current" positions are because the target can move during the interpolation. Not taking this in to account causes motion jitter at moderately high speeds.
I did not write this code and it appears to work. I had trouble altering this for angles because, for example, an interpolation between the angles 350° and 10° would take the 'long' way round instead of going in the direction of the 20° difference in angle.
I've looked into quaternion slerp but haven't been able to find an implementation that takes a time delta into account. Something that I have thought of, but not been able to implement either, is to interpolate between both angles twice, but the second time with a phase difference of 180° on each angle and to output the smaller of the two multiplied by -1.
Would appreciate any help or direction!