I always use this method to achieve a smooth angle interpolation which also avoids gimbal lock.
Code:
// For you rheader file:
// Used by "ang_lerp" (Lite-C doesn't support static yet)
VECTOR ANG_LERP_VEC;
// For your code file:
//--------------------------------------------------------------------
// ANGLE INTERPOLATION
// Like vec_lerp but for angles. Circumvents the gimbal lock issue.
//
// params: resultVec - vector which will receive the result angle
// srcVec - start angle of interpolation
// destVec - destionation angle of interpolation
// blendFactor - [0..1], interpolation factor
//
// returns: result angle (as it will be copied into reultVec)
//--------------------------------------------------------------------
VECTOR* ang_lerp (VECTOR* resultVec, VECTOR* srcVec, VECTOR* destVec, var blendFactor)
{
vec_set(resultVec, srcVec); // Copy source into result
vec_diff(ANG_LERP_VEC, destVec, resultVec); // Difference vector
ANG_LERP_VEC.x = ang(ANG_LERP_VEC.x); // Adjust angle validity
ANG_LERP_VEC.y = ang(ANG_LERP_VEC.y);
ANG_LERP_VEC.z = ang(ANG_LERP_VEC.z);
vec_scale(ANG_LERP_VEC.x, blendFactor); // Interpolate the rotation
vec_add(resultVec, ANG_LERP_VEC); // Apply interpolated angle
return(resultVec); // Return result angle
}
Do not worry about the fact that all parameters are VECTOR*'s, because Vectors and angles share the same structured data (3 vars in a row). In Lite-C this is no problem. It will be easy for you to convert this piece to C-Script. For speed reasons I use a global here, because Lite-C doesn't allow real statics yet.
Example:
Code:
VECTOR vecTemp;
vec_to_angle(vecTemp, vec_diff(vecTemp, you.x, my.x)); // Angle from me to you
ang_lerp(my.pan, my.pan, vecTemp, 0.1 * time_step); // Turn to you softly