Wednesday, January 23, 2019

Linear Interpolation

Linear interpolation or lerp is used to calculate a linear value that is between two values, i.e. lerp could return a value of 0.5 as it's between 0 and 1, which were the two original values given to the function. So in this instance the value is 50% between the two original values.

Lerping can be applied to a wide range of values and not just real world numbers, such as colour, vectors and quaternions. These example are made up of multiple dimensions such as colour usually being made up of 3/4 floats that represent R, G, B and A), so no matter how many dimensions make up something, lerping can still be applied using the generic formula:

Lerp(_a,  _b, _f) = (1 - _f) * _a + (_f * _b)

Where _a and _b are the two points that are being interpolated between and _f is within the fractional range of [0, 1] in terms of _a and _b. It is also important to note that this formula is not frame rate independant and so by using deltaTime * _f will achieve that.

An example use of lerping is moving a platform from point A to point B, where each tick the platform's position is now a percentage between point A and point B using lerp. There are other variations on lerp such as slerp which is spherical lerping, that provide similar effects to lerping.

No comments:

Post a Comment