I am attempting to write a spherical interpolation algorithm for for the application of smooth 3D animation in a game. The scripting language that the game engine uses is Lua. It is often easier for me to write an algorithm for 2D first and then 3D second, so I came up with the following (untested) algorithm for 2D spherical interpolation:
function slerp( x, y, x1, y1, t ) local rad = t * math.acos( x*x1 + y*y1 ) local newX = x * math.cos( rad ) - y * math.sin( rad ) local newY = x * math.sin( rad ) + y * math.cos( rad ) return newX, newY end
From what I understand, the above formula should calculate a fraction of the radian angle between two 2D unit vectors and then rotate counterclockwise x and y by that fractional angle. For the 3D algorithm, I thought about changing the above code to the following:
function slerp( x, y, z, x1, y1, z1, t ) local rad = t * math.acos( x*x1 + y*y1 + z * z1 ) local newX = x * math.cos( rad ) - y * math.sin( rad ) local newY = x * math.sin( rad ) + y * math.cos( rad ) local newZ = z * math.cos( rad ) - x * math.sin( rad ) return newX, newY, newZ end
My question is the following 2 points:
- Is my algorithm a correct implementation of spherical interpolation?
- Is there a less expensive way of calculating spherical interpolation in 3D (preferably with a thorough explanation )?