BETTER Actually forget what I wrote first. You can simply do:
template<typename K>
K lerpAngle(K u, K v, K p) {
return u + p*wrapMP(v - u);
}
where wrapMP
moves an angle to the interval [-pi|+pi]. Both input u
and v
can be any radian angle and the result will also not be in a specific interval.
The idea is actually quite simple. Move your point of view to the point u
such that u=0
. Now v
can be arbitrary as angles are not normalized, but we just wrap the distance v-u = v
to [-pi|+pi] and walk by the given percentage p
into that direction.
OLD (and inefficient) I wrote this code once:
template<typename K>
K lerpAngle(K u, K v, K p) {
K a = wrap2Pi(u);
K b = wrap2Pi(v);
K d = b - a;
if(d < -PI) {
b += 2*PI;
}
if(d > +PI) {
b -= 2*PI;
}
return wrap2Pi(a + p*(b - a));
}
where wrap2i
moves an angle to the interval [0,2*pi[.