Andrew’s quaternion library didn’t have a slerp function. I’m posting mine here, converted to lua from C# online.
Pass two Quaternions and a temporal value 0…1. Returns Quaternion.
function slerp(qa, qb, t)
qm = Quaternion(0,0,0,0)
-- // Calculate angle between them.
cosHalfTheta = qa.a * qb.a + qa.b * qb.b + qa.c * qb.c + qa.d * qb.d
-- // if qa=qb or qa=-qb then...
if (math.abs(cosHalfTheta) >= 1.0) then
qm.a = qa.a
qm.b = qa.b
qm.c = qa.c
qm.d = qa.d
return qm
end
-- // Calculate temporary values.
halfTheta = math.acos(cosHalfTheta)
sinHalfTheta = math.sqrt(1.0 - cosHalfTheta*cosHalfTheta)
-- // if theta = 180 degrees then result is not fully defined
-- // we could rotate around any axis normal to qa or qb
if (math.abs(sinHalfTheta) < 0.001) then
qm.a = (qa.a * 0.5 + qb.a * 0.5)
qm.b = (qa.b * 0.5 + qb.b * 0.5)
qm.c = (qa.c * 0.5 + qb.c * 0.5)
qm.d = (qa.d * 0.5 + qb.d * 0.5)
return qm
end
ratioA = math.sin((1 - t) * halfTheta) / sinHalfTheta
ratioB = math.sin(t * halfTheta) / sinHalfTheta
-- //calculate Quaternion.
qm.a = (qa.a * ratioA + qb.a * ratioB);
qm.b = (qa.b * ratioA + qb.b * ratioB)
qm.c = (qa.c * ratioA + qb.c * ratioB)
qm.d = (qa.d * ratioA + qb.d * ratioB)
return qm
end