Search code examples
math3danglegarrys-mod

Interpolating 3d angles (in Garry's Mod)


I'm trying to interpolate angles in Expression 2, as such:

local IAng_ = mix( Begin, End, 0.0-1.0 )

mix( v/a, v/a, n ) is a linear-interpolation function that is (probably) most easily described as:

function angle mix( Begin:angle, End:angle, Amount:number ){
    return Begin * Amount + End * (1 - Amount)
}

Problem is: The 3D angle range is -180 to 180, which is causing some glitching. (According to my friend it has to do with Gimbal lock.

I've tried a couple of things, which haven't worked due to the nature of the angles in Source Engine:

function angle angle:to360(){
    local Absolute = ang( abs( This:pitch() ), abs( This:yaw() ), abs( This:roll() ) )
    local Pitch = This:pitch() < 0 ? Absolute:pitch() + 180 : Absolute:pitch()
    local Yaw = This:yaw() < 0 ? Absolute:yaw() + 180 : Absolute:yaw()   
    local Roll = This:roll() < 0 ? Absolute:roll() + 180 : Absolute:roll()

    return ang( Pitch, Yaw, Roll ) 
}

Or adding 180 to the angles.

TL;DR Interpolation of 3D angles facing the Gimbal lock problem (angle range -180 to 180), best solution would be to have a function that converts to 0 - 360 range and back to -180 - 180.


Solution

  • I ended up using the quat( angle ) function to convert the angles to a quaternion, I then used the slerp ( spherical linear interpolation ) function built in like so:

    local Ang = slerp( quat( Angle1 ), quat( Angle2 ), Interp ):toAngle()